<template>
	<view>

	</view>
</template>

<script>
	import {
		loginBySilence,
		getH5AccessToken,
		getH5AccessByOpenId
	} from '../../Utils/Api.js';
	let app = getApp();
	export default {
		data() {
			return {

			}
		},
		async onLoad(options) {
			let that = this;
			// #ifdef MP
			app.globalData.accountId = options.accountId;
			uni.switchTab({
				url: '../tabBar/home/home'
			})
			// #endif
			// #ifdef H5
			var arr1 = window.location.href;
			var arr2 = arr1.split('=');
			
			if (arr2[1] != undefined && arr2[2] == '1#/') {
				app.globalData.h5code = arr2[1];
				await that.getH5AccessToken();
				uni.setStorage({
					key: "h5code",
					data: arr2[1]
				})

				uni.getStorage({
					key: 'accountId',
					success(e) {
						if (e.data) {
							app.globalData.accountId = e.data;
						}
					}
				})
				uni.getStorage({
					key: 'gasKey',
					success(e) {
						if (e.data) {
							app.globalData.gasKey = e.data;
						}
					}
				})
				uni.getStorage({
					key: 'staffCode',
					success(e) {
						if (e.data) {
							if (app.globalData.accountId == '000001&key' || app.globalData.accountId ==
								'000000&gasKey' || app.globalData.accountId == '000009&key' || app.globalData
								.accountId == '0000010&key') {
								app.globalData.staffCode = e.data;
							} else {
								app.globalData.mchId = e.data;
							}
						}
					}
				})
				uni.getStorage({
					key: 'accountType',
					success(e) {
						if (e.data) {
							app.globalData.accountType = e.data;
						}
					}
				})

				// 0011 扫码领取优惠券
				// 0012 分享领取优惠券
				// 0013 分享界面
				// 0014 直领优惠券
				
				// 0016分享的界面跳转进来
				// 0017携带登录的code
				if (app.globalData.accountType == '0000011&key' && app.globalData.h5code) {
					

					uni.navigateTo({
						url: '/pages/goods/externalCoupon/externalCoupon'
					})
					return;
				}
				if ((app.globalData.accountType == '0000012&key' || app.globalData.accountType == '0000014&key') && app
					.globalData.h5code) {
					uni.navigateTo({
						url: '/subPages/shareCounpon/shareCounpon'
					})
					return;
				}
				if (app.globalData.accountType == '0000013&key' && app.globalData.h5code) {
					uni.navigateTo({
						url: '/subPages/shareImgage/shareImgage'
					})
					return;
				}

				// 从分享页进来
				if (app.globalData.accountType == '0000016&key' && app.globalData.h5code) {
					let value = uni.getStorageSync('goUrl');
					/* /physical-merchants/classify/goodsDetail/goodsDetail?goodsId=35#/ */
					value = value.slice(0, value.lastIndexOf("#"));

					
					if (value == "/pages/tabBar/home/home" || value == "/pages/tabBar/category/category" || value ==
						"/pages/tabBar/user/user") {
						uni.switchTab({
							url: value
						})
						return
					}
					if (value == "/") {
						uni.switchTab({
							url: '../tabBar/home/home'
						})
						return
					}


					uni.navigateTo({
						url: value
					})




					return;
				}

				
				// 从页面进来携带参数值跳向登陆页面，goUrl
				if (app.globalData.accountType == '0000017&key' && app.globalData.h5code){
					if(!app.globalData.userInfo||!app.globalData.token){
						uni.navigateTo({
							url: '/pages/login/register'
						})
						return
					}

				}

				uni.switchTab({
					url: '../tabBar/home/home'
				})
			} else {
				// 获取openid
				that.jumpcdx();
				uni.setStorage({
					key: "accountId",
					data: arr2[1]
				})

				uni.setStorage({
					key: "accountType",
					data: arr2[1]
				})
				if (arr2[2] != undefined && arr2[2]) {
					let arr3 = arr2[2].split('&');
					uni.setStorage({
						key: "gasKey",
						data: arr3[0]
					})
					that.loginBySilence(arr3[0]);
				}
				if (arr2[3] != undefined && arr2[3]) {
					let arr4 = arr2[3].split('#');
					uni.setStorage({
						key: "staffCode",
						data: arr4[0]
					})
				}
				
				if(arr1.indexOf("code") != -1){
					let goUrl =arr1.slice(Number(arr1.indexOf("code"))+5) ;
					uni.setStorageSync("goUrl",decodeURIComponent(goUrl));
				}
				
				
			}
			// if (app.globalData.h5code) {
			// 	that.getH5AccessToken();
			// }
			// #endif
		},
		methods: {
			//h5静默登陆
			loginBySilence(item) {
				let datas = {
					phone: item
				}
				loginBySilence(datas).then(res => {
					if (res.return_code == '000000') {
						app.globalData.userInfo = res.return_data
							.object
							.highUser;
						app.globalData.token = res.return_data.uniqueCode;
						uni.setStorage({
							key: "user",
							data: res.return_data
								.object
								.highUser
						})
						uni.setStorage({
							key: "token",
							data: res.return_data.uniqueCode
						})
					}
				})
			},
			//H5 获取openId
			async getH5AccessToken() {
				let params = {
					code: app.globalData.h5code
				}
				
				let res = await getH5AccessToken(params);

				if (res.return_code == '000000' && res.return_data.openid) {
					app.globalData.openId = res.return_data.openid;
					// uni.setStorage({
					// 	key: "openId",
					// 	data: res.return_data.openid
					// })
					
					uni.setStorageSync("openId",res.return_data.openid);
					
					await this.getH5AccessByOpenId();
				} else {
					this.jumpcdx();
				}
				
			},
			//判断手机号
			async getH5AccessByOpenId() {
				let datas = {
					openId: app.globalData.openId
				}
				
				let res = await getH5AccessByOpenId(datas);
				
					
				if(res.return_data.object&&res.return_data.uniqueCode){
					app.globalData.userInfo = res.return_data
						.object
						.highUser;
					app.globalData.token = res.return_data.uniqueCode;
					uni.setStorage({
						key: "user",
						data: res.return_data
							.object
							.highUser
					})
					uni.setStorage({
						key: "token",
						data: res.return_data.uniqueCode
					})
				}else{
					if(uni.getStorageSync("accountId")=="0000011&key"){
						app.globalData.userInfo = '';
						app.globalData.token = '';
						uni.setStorage({
							key: "user",
							data: ''
						})
						uni.setStorage({
							key: "token",
							data: ''
						})
					}
					// app.globalData.userInfo = '';
					// app.globalData.token = '';
					// uni.setStorage({
					// 	key: "user",
					// 	data: ''
					// })
					// uni.setStorage({
					// 	key: "token",
					// 	data: ''
					// })
				}
						
					
					
				
			},
			jumpcdx() {
				let str =encodeURIComponent(app.globalData.h5url+"/index.html#/")
				// console.log(app.globalData.h5url,"11111",str,"str" );
				
				
				location.href =																											
					'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxa075e8509802f826&redirect_uri='+str+'&response_type=code&scope=snsapi_base&state=1#wechat_redirect';
			},
		}
	}
</script>

<style>

</style>
