var request = require('./http.js');
var siteinfo = require('../../siteinfo.js'); //require这个js模块 
 
var wx = require('@/jweixin-module/lib/index.js');
module.exports = function getWXSign(superid) {
	console.log(superid,"superid")
	// if(!superid){
	// 	console.log("ggg")
	// 	if(siteinfo.super_id){
	// 		superid=siteinfo.super_id;
	// 	}
	// }
	return new Promise((resolve, reject) => {
		var route;
		if (/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)) {
			if (navigator.userAgent.indexOf('iPhone') !== -1) {
				route = window.location.href.split('#')[0];
			}else {
				route = window.location.href.split('#')[0];
			}
		}else{
			   route = window.location.href.split('#')[0];
		}
		let url = encodeURIComponent(route);
		request({
			url: "wxjs/wxJstoken",
			data: {
				url,
				superid:superid
			},
			is_agent: "server",
		}).then(e => {
			// console.log(e, "wxJstoken")
			if (e.code === '200') {
				var params = e.data;
				wx.config({
					debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
					appId: params.appid, // 必填，公众号的唯一标识
					timestamp: params.timestamp + "", // 必填，生成签名的时间戳
					nonceStr: params.noncestr, // 必填，生成签名的随机串
					signature: params.signature, // 必填，签名
					jsApiList: [
						"chooseImage",
						"previewImage",
						"uploadImage",
						"downloadImage",
						"openLocation",
						"getLocation",
						"chooseWXPay",
						"scanQRCode",
						"updateTimelineShareData",
						"updateAppMessageShareData",
						"onMenuShareAppMessage",
						"onMenuShareTimeline"
					], // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
					openTagList: ['wx-open-launch-weapp']
				})
				wx.ready((res) => {
					console.log(res, "ready")
					// alert(res)
					resolve(true);
				})
				wx.error((res) => {
					console.log(res, "error")
					// alert(res)
					reject(false);
					// config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
				})
			}
		})
	})
}
