<template>
</template>

<script>
	export default {
		data() {
			return {
			}
		},
		onLoad() {
			var self = this;
			//#ifdef H5
			var code = this.getQueryString("code");
			var backUrl = this.getQueryStringBack("backUrl");
			var openId = this.getQueryString("state");
			self.request({
				action : 'wx/getAccessToken',
				data : {
					code : code,
					openId : openId
				},
				success (res) {
					var domain = window.location.href;
					// 用户已绑定
					if (res.data.code == '200') {
						var userInfo = res.data.data.userInfo;
						uni.setStorageSync('userInfo', userInfo);
						uni.setStorage({
							key: 'Authorization',
							data: res.data.data.wxLoginToken,
							success: function () {
								// 是消息推送页 且 是本人微信打开
								if (!!backUrl && res.data.data.sameOpenId) {
									var checkingUrl = domain.split("?")[0] + "#" + backUrl;
									window.location.href = checkingUrl;
								// 跳转主页
								} else {
									var checkingUrl = domain.split("?")[0] + "#" + '/pages/home/task';
									window.location.href = checkingUrl;
								}
							}
						});
					// 获取到用户unionid,未绑定用户
					} else if (res.data.code == '300') {
						var checkingUrl = domain.split("?")[0] + "#" + '/pages/index/checking';
						window.location.href = checkingUrl + '?openId='+res.data.data.openid + '&unionid='+res.data.data.unionid
					}
				}
			})
			// #endif
		},
		methods:{
			getQueryString(name) {
			　　var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
			　　var reg_rewrite = new RegExp("(^|/)" + name + "/([^/]*)(/|$)", "i");
			　　var r = window.location.search.substr(1).match(reg);
			　　var q = window.location.pathname.substr(1).match(reg_rewrite);
			　　if(r != null){
			　　　　return unescape(r[2]);
			　　}else if(q != null){
			
			　　　　return unescape(q[2]);
			　　}else{
			　　　　return null;
			　　}
			},
			getQueryStringBack(name) {
			    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
			    if(window.location.hash.indexOf("?") < 0){
			            return null;
			    }
			    let r = window.location.hash.split("?")[1].match(reg); 　　
			    if (r != null) return decodeURIComponent(r[2]); 
			　　    return null; 
			}
		}
	}
</script>

<style>
</style>
