/**
 * Share v1.0.0
 * @Class Share
 * @description shopro-share 1.0.0 分享方法
 * @Author lidongtony
 * @Date 2021-04-19
 * @Email lidongtony@qq.com
 */
import {
	API_URL,
	API_ROOT,
	UNIACID
} from '@/common/request/request';
import store from "@/common/store";
import $platform from "@/common/platform";
// #ifdef H5
import wxsdk from "@/common/wechat/sdk";
// #endif
export default {

	// 设置分享信息
	setShareInfo(scene = {
		title: '', // 自定义分享标题
		desc: '',  // 自定义描述
		image: '', // 自定义分享图片
		// path:'',
		params: {} // 自定义分享参数
	}) {
		let that = this;
		let shareInfo = {
			title: '', 		// 分享标题
			desc: '', 		// 描述
			image: '', 		// 分享图片
			path: '', 		// 分享路径
			copyLink: '', 	// 复制链接
			query: ''		// 分享参数
		}
		// let shareConfig = store.getters.initShare;
		// let shopConfig = store.getters.appInfo;
		let shareConfig = store.getters.appInfo;
		// console.log(shareConfig)
		
		// let pages = getCurrentPages();
		// let route = pages[pages.length - 1].route;
		// console.log(route)
		
		// if (shopConfig?.domain === '' || shareConfig.share_title === '' || shareConfig.share_img_path === '') {
		// 	throw '请在商城配置中设置商城域名和分享信息'
		// }

		// 设置自定义分享信息
		shareInfo.title = scene.title !== '' ? scene.title : shareConfig.share_title;
		shareInfo.image = scene.image !== '' ? scene.image : shareConfig.share_img_path;
		shareInfo.desc = scene.desc !== '' ? scene.desc : shareConfig.share_des;
		
		// 自动拼接分享用户参数
		let query = that.setShareQuery(scene.params);
		// let stay = shareConfig.share_img_path.split('public/')[0]+'public/h5?uniacid='+UNIACID;
		// #ifdef H5
		let stay = window.location.href.split('/h5')[0]+'/h5/?uniacid='+UNIACID;
		// console.log(window.location.href);
		// #endif
		// console.log(stay);
		// #ifdef MP
		shareInfo.path = `/pages/index/index?${query}`;
		// console.log(shareInfo);
		// #endif
		// #ifndef MP
		// shareInfo.path = `${store.getters.appInfo?.domain}?${query}`;
		// shareInfo.path = scene.path !== '' ? scene.path :`${stay}&{query}`;
		shareInfo.path = `${stay}&${query}`;
		// console.log(shareInfo.path);
		// #endif //initShop
		// shareInfo.copyLink = `${store.getters.appInfo?.domain}?${query}`;
		// shareInfo.query = query;
		// #ifdef H5
		console.log(shareInfo);
		if($platform.get() === 'wxOfficialAccount') {
			wxsdk.share(shareInfo);
		}
		// #endif
		store.commit("shareInfo", shareInfo);
		return shareInfo;
	},

	// 自定义分享参数拼接: 由于小程序码长度限制(B码最大32位长度),为了确保分享参数最大可扩展性，使用spm方法拼接 shareUserId.page.pageId.platform.from ----例 spm=88888888.3.1666666.3.2 即为ID为88888888用户通过微信网页平台生成了拼团ID为1666666的拼团分享海报
	setShareQuery(params) {
		let shareUserId = '0'; // 设置分享者用户ID
		if (params&&params.shareId) {
			// if (store.getters.isLogin) {
				shareUserId = store.getters.userInfo.id;
			// }
		}
		if (params.shareId === undefined) {
			if (store.getters.isLogin) {
				shareUserId = store.getters.userInfo.id;
			}
		}
		let page = '1'; // 页面类型: 1=首页(默认),2=商品,3=拼团...按需扩展
		if (params.page !== undefined) {
			page = params.page;
		}
		let pageId = '0'; // 设置页面ID: 如商品ID、拼团ID等
		if (params.pageId !== undefined) {
			pageId = params.pageId;
		}
		let platform = ['H5', 'wxOfficialAccount', 'wxMiniProgram', 'App'].indexOf($platform.get()) +
		1; // 设置分享的平台渠道: 1=H5,2=微信公众号网页,3=微信小程序,4=App,...按需扩展
		let from = '1'; // 设置分享方式: 1=直接转发,2=海报,3=链接,...按需扩展
		// if (params.from !== undefined) {
		// 	from = params.from;
		// }
		//spmParam = ...  可按需扩展
		return `spm=${shareUserId}.${page}.${pageId}.${platform}.${from}`;
	},
	getShareQuery(spm) {
		let shareParams = {};
		let shareParamsArray = spm.split('.');
		if(shareParamsArray[0] != '0') {
			shareParams.shareUserId = shareParamsArray[0];
		}
		switch(shareParamsArray[1]) {
			case '1':
			// 默认首页不跳转
			break;
			case '2':
			shareParams.page = '/pages/tasklist/taskdetails';
			break;
			case '3':
			shareParams.page = '/pages/activity/groupon/detail';
			break;
		}
		if(shareParamsArray[2] !== '0') {
			shareParams.pageId = shareParamsArray[2];
		}
		shareParams.platform = shareParamsArray[3];
		shareParams.from = shareParamsArray[4];
		return shareParams;
	}
}
