//引入微信jssdk
const wx = require('@/js_sdk/jweixin-1.4.0.js')

//配置注册url
const url = '/app/getJsapiTicket';

//http方法
import Request from '@/js_sdk/luch-request/luch-request/index.js';
const $http = new Request();

//系统域名url
// encodeURIComponent
const href = location.href.split('#')[0];
// const href = location.href.split("?")[0];
// console.log('href',href);


let token = uni.getStorageSync("token");

import { config } from './config/config';

//系统域名url
export default {
	...wx,
	isOk: false,
	async config() {
		wx.config({
			debug: false,
			jsApiList: [
				"updateAppMessageShareData",
				"updateTimelineShareData",
				"onMenuShareTimeline",
				"onMenuShareAppMessage",
				"onMenuShareQQ",
				"onMenuShareWeibo",
				"onMenuShareQZone",
				"startRecord",
				"stopRecord",
				"onVoiceRecordEnd",
				"playVoice",
				"pauseVoice",
				"stopVoice",
				"onVoicePlayEnd",
				"uploadVoice",
				"downloadVoice",
				"chooseImage",
				"previewImage",
				"uploadImage",
				"downloadImage",
				"translateVoice",
				"getNetworkType",
				"openLocation",
				"getLocation",
				"hideOptionMenu",
				"showOptionMenu",
				"hideMenuItems",
				"showMenuItems",
				"hideAllNonBaseMenuItem",
				"showAllNonBaseMenuItem",
				"closeWindow",
				"scanQRCode",
				"chooseWXPay",
				"openProductSpecificView",
				"addCard",
				"chooseCard",
				"openCard",
			],
			...(await $http.get(config.apiBaseurl + url, {
				params: {url: href},
				header:{Accept:'application/json',
				Authorization: 'Bearer '+ token},
				dataType: 'json'})
				).data.data,
		});
		wx.ready(e => {
			this.isOk = true;
			this.hideMenu();
		});
		wx.error(e => {
			console.log("失败",e.errMsg);
			this.isOk = false;
		});
	},
	hideMenu() {
		wx.hideAllNonBaseMenuItem();
		wx.hideMenuItems({		
		    menuList: [
				"menuItem:share:appMessage",
				"menuItem:profile",
				"menuItem:addContact",
				"menuItem:dayMode",
				"menuItem:nightMode",
				"menuItem:share:timeline",
				"menuItem:favorite"				
			] // 要隐藏的菜单项,只能隐藏“传播类”和“保护类”按钮,所有menu项见附录3		
		});
	},
	showMenu() {
		wx.showMenuItems({
			menuList: [
				"menuItem:share:appMessage",
				"menuItem:profile",
				"menuItem:addContact",
				"menuItem:dayMode",
				"menuItem:nightMode",
				"menuItem:share:timeline",
				"menuItem:favorite"
			] // 要显示的菜单项,所有menu项见附录3
		});
	},
	/**
	 * 分享配置
	 * @param {Object} info
	 */
	share(data = {}, info = {}) {
		//添加链接时间戳
		// data.v = new Date().getTime();
		// console.log("shareData",data)
		// let search = [];
		// for (const [key, value] of Object.entries(data)) {
		// 	// search.push(`${key}=${encodeURIComponent(value)}`);
		// 	search.push(`${key}=${value}`);
		// }
		// search = "?" + search.join("&");
		
		let afterEndUrl = config.afterEndUrl;
		// let afterEndUrl = 'http://tanhui.hongweisoft.com/api/wechat/h5/authorize?returnUrl='
		// let afterEndUrl = 'http://gzhjt.gzsdzth.com/api/wechat/h5/authorize?returnUrl='
		
		let fullUrl = afterEndUrl + location.href.split('?')[0] + '?togetherId=' + data.togetherId + '/#/' + data.routeUrl
		const shareInfo = {
			title: data.nickname + '邀请你参与贵州省单株碳汇精准扶贫',
			link: fullUrl,
			desc: "购碳扶贫,你我同行",
			imgUrl: href + "/static/logo.png",
			trigger(res) {},
			success(res) {},
			cancel(res) {},
			fail(res) {},
			...info
		};
		console.log('shareInfo',shareInfo,'fullUrl',fullUrl)
		//自定义“分享给朋友”及“分享到QQ”按钮的分享内容(1.4.0)
		wx.updateAppMessageShareData(shareInfo);
		//自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容(1.4.0)
		wx.updateTimelineShareData(shareInfo);
	},
	//拉起微信浏览器端支付
	JSAPI(res) {
		return new Promise(r => {
			console.log('JSAPI res',res);
			wx.chooseWXPay({
				"appId": res.appId,
				"timestamp": res.timeStamp, //时间戳,自1970年以来的秒数     
				"nonceStr": res.nonceStr, //随机串     
				"package": res.packageValue,
				"signType": res.signType, //微信签名方式:
				"paySign": res.paySign, //微信签名 
				success() {
					r({
						code: 0,
						msg: "成功"
					});
				},
				cancel() {
					r({
						code: 1,
						msg: "取消"
					});
				},
				fail() {
					r({
						code: 2,
						msg: "失败"
					});
				}
			});
		});
	},
}