import appConfig from '@/config/appConfig.js'
import {toErrPageFunc} from '@/util/toErrorPageUtil.js'

export function setToken(toPageParams){
	console.log(toPageParams,'toPageParamstoPageParamstoPageParams')
	let env  = ''
	if(toPageParams){
		env = toPageParams.env ? toPageParams.env : '';
	}
	console.log(env,'toPageParamstoPageParamstoPageParams')
	if(env == ''){
	  uni.setStorageSync('payH5','') // 改变存储	
	}
	// 参数token
	let token = toPageParams ? toPageParams[appConfig.tokenKey] : '';
	
	// 参数中包含token  赋值到配置中
	if(token){
		appConfig.tokenValue = token;
		
		// #ifdef H5
			window.sessionStorage.setItem(appConfig.tokenKey, token) // 放置到sessionStorage 刷新浏览器不会消失，关闭浏览器会消失
		// #endif
		
	}
	
	// token不存在
	if(!appConfig.tokenValue){
		return toErrPageFunc('获取二维码参数失败，请通过扫描收款二维码进入小程序支付！');
	}
	
	//放置 pageType
	setPageType()
}

export function setPageType(){
	
	let currentPageType = ''
	
	// #ifdef H5
	
		const userAgent = navigator.userAgent;
		if(userAgent.indexOf("MicroMessenger") >= 0){
			currentPageType = appConfig.PAGE_TYPE_ENUM.WECHAT_H5
		}
		else if(userAgent.indexOf("AlipayClient") >= 0){
			currentPageType = appConfig.PAGE_TYPE_ENUM.ALIPAY_H5
		}
		else if(userAgent.indexOf("UnionPay") >= 0 || userAgent.indexOf("unionpay") >= 0){
			currentPageType = appConfig.PAGE_TYPE_ENUM.YSFPAY_H5
		}
		else{
			appConfig.currentPageType = appConfig.PAGE_TYPE_ENUM.OTHER_H5
			uni.reLaunch({
				url: '/pages/index/index'
			})
			return
		}
		
	// #endif
	
	// #ifdef MP-WEIXIN
	    currentPageType = appConfig.PAGE_TYPE_ENUM.WECHAT_LITE
	// #endif
	
	// #ifdef MP-ALIPAY
	    currentPageType = appConfig.PAGE_TYPE_ENUM.ALIPAY_LITE
	// #endif
	
	if(!currentPageType){
		return toErrPageFunc('当前应用不支持！');
	}
	
	appConfig.currentPageType = currentPageType;
}

// 根据小程序规则，获取并设置token
export function setLiteToken(url = '') {
	
	// token对象
	const tokenObject = {}
	
	if (url) {

		// 截取协议名 + 域名 + 端口号
		if(url.startsWith('http://')) {
			let sUrl = url.substring(7)
			appConfig.env.JEEPAY_PAY_BASE_URL = "http://" + sUrl.substring(0, sUrl.indexOf('/'))
		}else if(url.startsWith('https://')) {
			let sUrl = url.substring(8)
			appConfig.env.JEEPAY_PAY_BASE_URL = "https://" + sUrl.substring(0, sUrl.indexOf('/'))
		}else{
			toErrPageFunc('二维码协议头错误')
		}
		
		// 截取token
		const query = url.substring(url.indexOf('?') +1)
		const vars = query.split("&")
		for (var i=0; i<vars.length; i++) {
			const pair = vars[i].split("=")
			if(pair[0] == appConfig.tokenKey){
				tokenObject[appConfig.tokenKey] = pair[1]
				setToken(tokenObject)
				return
			}
		}
	}
	// else {
	// 	appConfig.env.JEEPAY_PAY_BASE_URL = 'https://pay.s.jeepay.vip'
	// 	tokenObject[appConfig.tokenKey] = 'f86f221f580ef6945206a7a48d0cb05cf450b43f10edfe5603998cd48791a074'
	// 	setToken(tokenObject)
	// 	return
	// }
	
	return toErrPageFunc('获取二维码参数失败，请通过扫描收款二维码进入小程序支付！');
}

//同步取出ext.json对象
export function getExtStoreId(){
	try{
	const extConfig = uni.getExtConfigSync()
	if(extConfig.domain){
		appConfig.env.JEEPAY_BASE_URL = extConfig.domain
	}		 
	// uni.showToast({title: JSON.stringify(extConfig),icon:"none",duration:3000});
		console.log(extConfig,'extJson对象');
		return extConfig;
	}catch(err){
		console.log(err,'getExtStoreId__error')
	}
}