/**
 * MixinIndex v1.0.0
 * @description shopro-mixin-index 1.0.0 shopro全局入口组件
 * @Author lidongtony
 * @Date 2021-04-18
 * @Email lidongtony@qq.com
 */

import store from '@/common/store'
import share from '@/common/share'
import http from '@/common/request'
import $platform from '@/common/platform'
import {
	router
} from '@/common/router'
export default {
	onLoad(options) {
		let that = this;
		// 后端拼接的具体page页面 直接进入 （如订阅消息场景下直接跳转）
		// #ifdef MP
		console.log(options);
		console.log(options.scene);
		if (options?.scene) {
			let scene = decodeURIComponent(options.scene);
			let sceneObj = scene.split('=');
			options[sceneObj[0]] = sceneObj[1];
		}
		// #endif
		if (options?.page) {
			router.replace({
				path: decodeURIComponent(options.page)
			})
		}
		if (options?.custom_id) {
			router.replace({
				path: '/pages/index/view',
				query: {
					id: options.custom_id
				}
			})
		}
		if (options?.spm) {
			let shareParams = share.getShareQuery(options.spm);
			// 保存推荐信息
			if (shareParams.shareUserId) {
				if (store.getters.isLogin) {
					http('commission.addTeam', {
						spm: options.spm
					});
				} else {
					uni.setStorageSync('spm', options.spm);
				}
			}
			// 跳转分享路径
			let page = {};
			if (shareParams.page) {
				page.path = shareParams.page;
			}
			if (shareParams.pageId) {
				page.query = {
					id: shareParams.pageId
				}
			}
			if (page.path) {
				router.replace(page);
			}
		}
	},
	// #ifdef MP-WEIXIN
	onShareAppMessage(res) {
		let that = this;
		// uni.$emit('ON_WX_SHARE')
		let shareInfo = store.getters.shareInfo;
		// console.log(shareInfo)
		return {
			title: shareInfo.title,
			path: shareInfo.path,
			imageUrl: shareInfo.image,
			// success(res) {
			// 	uni.showToast({
			// 		title: '分享成功'
			// 	})

			// },
			// fail(res) {
			// 	uni.showToast({
			// 		title: '分享失败',
			// 		icon: 'none'
			// 	})
			// },
			complete() {}
		}
	},
	onShareTimeline(res) {
		let that = this;
		let shareInfo = store.getters.shareInfo;
		let query = shareInfo.query;
		// console.log(query);
		//携带当前页面资源ID参数
		let currentPage = getCurrentPages()[getCurrentPages().length - 1];
		let options = currentPage.options;
		if (JSON.stringify(options) != '{}' && options.id) {
			query += `&id=${options.id}`;
		}
        // console.log(query)
		return {
			title: shareInfo.title,
			query: query,
			imageUrl: shareInfo.image,
			// success(res) {
			// 	uni.showToast({
			// 		title: '分享成功'
			// 	})
			// },
			// fail(res) {
			// 	uni.showToast({
			// 		title: '分享失败',
			// 		icon: 'none'
			// 	})
			// },
			complete() {}
		}
	}
	// #endif
}
