<template>
	<view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
			}
		},
		onLoad() {
			var self = this;
			//#ifdef MP-WEIXIN
			wx.login({
			  success (res) {
				if (res.code) {
				  self.request({
					  action : 'wx/login',
					  data : {
						  jsCode : res.code
					  },
					  success(resData) {
							// 用户已绑定
							if (resData.data.code == "200") {
								var userInfo = resData.data.data.userInfo;
								uni.setStorageSync('userInfo', userInfo);
								uni.setStorage({
									key: 'Authorization',
									data: resData.data.data.wxLoginToken,
									success: function () {
										// 跳转主页
										uni.redirectTo({
											url: '/pages/home/task'
										});
									}
								});
							// 用户未绑定,跳转绑定页
							} else if (resData.data.code == "100") {
								uni.redirectTo({
									url: '/pages/index/checking?openId='+resData.data.data.openId + '&unionid='+resData.data.data.unionid
								});
							// 数据异常
							} else {
								this.$refs.uToast.show({
									title: resData.data.message,
									type: 'error'
								})
							}
					  }
				  })
				} else {
					console.log('登录失败！' + res.errMsg)
				}
			  }
			})
			// #endif
			//#ifdef H5
			let backUrl = this.getQueryString("backUrl");
			let openId = this.getQueryString("openId");
			var domain = window.location.href;
			var checkingUrl = "";
			if(!!backUrl) {
				checkingUrl = domain.split("#")[0] + "#/pages/index/gzhGo?backUrl=" + encodeURIComponent(backUrl);
			} else {
				checkingUrl = domain.split("#")[0] + "#" + '/pages/index/gzhGo';
			}
			window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+this.getWxconfig().appID+'&redirect_uri='+encodeURIComponent(checkingUrl)+'&response_type=code&scope=snsapi_userinfo&state='+openId+'#wechat_redirect';
			// #endif
		},
		methods: {
			getQueryString(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>
