<template>
	<view>

	</view>
</template>

<script>
	import {
		getAccessToken
	} from '../../Utils/Api.js';
	let app = getApp();
	export default {
		data() {
			return {
				userInfo: app.globalData.userInfo, //登录信息
			}
		},
		onLoad(options) {

			uni.reLaunch({
				url: '/pages/login/login'
			})
			return

			
			let that = this;
			/* 
			 重定向前:https://gratia-pay.dctpay.com/cweb?accountId=0001&key=123#/
			 		https://gratia-pay.dctpay.com/cweb?accountId=0001&key=123&code=456#/	
			 重定向后 -微信
			 https://gratia-pay.dctpay.com/cweb/?redirect_uri=https://gratia-pay.dctpay.com/cweb&code=0111BKGa1EvjoG0sm9Ha1u8wG111BKGR&state=1#/
			["https://gratia-pay.dctpay.com/cweb/?redirect_uri", "https://gratia-pay.dctpay.com/cweb&code", "0111BKGa1EvjoG0sm9Ha1u8wG111BKGR&state", "1#/"]
			 */
			// #ifdef H5
			let locationPathStr = window.location.href;
			let locationPathArr = locationPathStr.split('=');
			let codeArr = locationPathArr[2] ? locationPathArr[2].split('&') : [];
			if (locationPathArr[locationPathArr.length-1]=='1#/'&&codeArr[0]) {
					app.globalData.h5code = codeArr[0];
					uni.setStorageSync("h5code",codeArr[0])
					this.getAccessToken();//获取openID
				
			} else {
				
				uni.setStorageSync("accountId","");
				uni.setStorageSync("urlKey","");
				uni.setStorageSync("urlCode","");
				
				if(locationPathArr[1]){
					let accountId = locationPathArr[1].split("&")[0];
					app.globalData.accountId=accountId;
					uni.setStorageSync("accountId",accountId);
				}
				if(locationPathArr[2]){
					let key;
					if(!locationPathArr[3]){
						key = locationPathArr[2].split("#")[0];
					}else{
						key = locationPathArr[2].split("&")[0];
					}
					app.globalData.urlKey = key;
					uni.setStorageSync("urlKey",key);
				}
				if(locationPathArr[3]&&!locationPathArr[4]){
					let code = locationPathArr[3].split("#")[0];
					app.globalData.code = code;
					uni.setStorageSync("urlCode",code);
				}
				
				this.jumpcdx();//网页授权
			}
			// #endif
		},
		methods: {
			//H5 获取openId
			getAccessToken() {
				let params = {
					code: app.globalData.h5code
				}
				getAccessToken(params).then(res => {
					if (res.return_code == '000000' && res.return_data.openid) {
						app.globalData.openId = res.return_data.openid;
						uni.setStorageSync("openId",res.return_data.openid);
						if (this.userInfo) {
							
							if(!this.isGoHomeUrl()) return;
							
							uni.reLaunch({
								url: '../tabBar/home/home'
							})
						} else {
							uni.reLaunch({
								url: '/pages/login/login'
							})
						}

					} else {
						this.jumpcdx();
					}
				});
			},
			isGoHomeUrl(){
				// 0001扫码核销https://gratia-pay.dctpay.com/cweb?accountId=0001&key=123
				if (app.globalData.accountId == '0001' && app.globalData.h5code){
					let orderKey = uni.getStorageSync("urlKey");
					if(orderKey&&this.userInfo.store.id){
						uni.redirectTo({
							url: '/subpackages/groupBuyingConfiguration/groupVerify/groupVerify?orderKey='+orderKey,
						})
						return false;
					}else{
						uni.redirectTo({
							url: '/pages/login/login'
						})
						return false;
					}
				}
				return true;
			},
			jumpcdx() {
				let redirectUrl = app.globalData.redirectUrl;
				location.href =
					`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxa075e8509802f826&redirect_uri=https://hsg.dctpay.com/wechat_authorize/?redirect_uri=${redirectUrl}&response_type=code&scope=snsapi_base&state=1#wechat_redirect`;
			},
		}
	}
</script>

<style>

</style>