<template>
	<view class="container">
		<!-- #ifdef  MP-WEIXIN-->
		<button open-type="getUserInfo" @getuserinfo='mylogin' class="flex-y-center box-pack-between item">
			<view class="flex-center">
				<image class="wxlogon" src="../../static/images/wx.png" mode=""></image>
				<text>微信</text>
			</view>
			<view class="flex-center wxright">
				{{openid?'已绑定':'未绑定'}} <text class="iconfont icon-prev"></text>
			</view>
		</button>
		<view class="Under" v-if="openid">
			<button class="login-btn flex-center" @click="isauth" form-type="submit">账号解绑</button>
		</view>
		<!-- #endif -->

		<!-- #ifdef H5 -->
		<button @click="gzhurl" class="flex-y-center box-pack-between item">
			<view class="flex-center">
				<image class="wxlogon" src="../../static/images/wx.png" mode=""></image>
				<text>微信</text>
			</view>
			<view class="flex-center wxright">
				{{openid?'已绑定':'未绑定'}} <text class="iconfont icon-prev"></text>
			</view>
		</button>

		<view class="Under" v-if="openid">
			<button class="login-btn flex-center" @click="isauth" form-type="submit">账号解绑</button>
		</view>
		<!-- #endif -->

	</view>
</template>

<script>
	export default {
		data() {
			return {
				openid: "",
			}
		},
		onLoad(e) {
			this.openid = e.openid == 'null' ? false : !e.openid ? false : true;
			// #ifdef H5
			if (!this.openid) {
				this.isweb();
			}
			// #endif   
		},

		methods: {
			isweb() {
				function GetRequest() {
					var url = location.search; //获取url中"?"符后的字串
					var theRequest = new Object();
					if (url.indexOf("?") != -1) {
						//判断是否有参数
						var str = url.substr(1); //从第一个字符开始 因为第0个是?号 获取所有除问号的所有符串
						var strs = str.split("&");
						console.log(strs)
						for (var i = 0; i < strs.length; i++) {
							theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
						}
					}
					console.log(theRequest)
					return theRequest;
				}
				if (GetRequest().code) {
					this.request({
						url: "user/gzhopenid",
						data: {
							code: GetRequest().code,
							state: GetRequest().state,
						},
					}).then(res => {
						console.log(res, "user/gzhurl");
						if (res.code == 200) {
							this.openid = true;
						}
					}).catch((res) => {
						console.log("message", res)
					});
				}
			},

			gzhurl(e) {
				this.request({
					url: "user/gzhurl",
					data: {
						token: uni.getStorageSync("token"),
						type: 2,
					},
				}).then(res => {
					console.log(res, "user/gzhurl");
					// return
					if (res.data.url) {
						uni.navigateBack({
							delta:2
						})
						location.href = res.data.url;
					}
					
				}).catch((res) => {
					console.log("message", res)
				});
			},

			isauth(e) {
				var type;
				// #ifdef H5
				type = 2;
				// #endif
				// #ifdef MP-WEIXIN
				type = 1;
				// #endif
				this.request({
					url: "user/del_openid",
					data: {
						token: uni.getStorageSync("token"),
						type,
					},
				}).then(res => {
					console.log(res, "res")
					uni.showToast({
						title: '解绑成功',
						icon: 'none',
					})
					setTimeout(() => {
						uni.navigateBack({
							delta: 1
						})
					}, 2e3)
				})
			},

			mylogin(e) {
				console.log(e)
				if (e.detail.errMsg == "getUserInfo:ok") {
					uni.login({
						success: (res) => {
							console.log(res)
							// return
							if (res.code) {
								//发起网络请求
								this.request({
									url: "user/xcx_openid",
									data: {
										code: res.code,
										token: uni.getStorageSync("token"),
									},
								}).then(res => {
									console.log(res, "res")
									this.openid = true
									uni.showToast({
										title: '绑定成功',
										icon: 'none',
									})
								}).catch((res)=>{
									uni.showToast({
										title:res.message,
										icon: 'none',
									})
								})
							} else {
								console.log('登录失败！' + res.errMsg)
						
							}
						}
					})
				} else {
					uni.showToast({
						title: '请允许授权',
						icon: 'none',
					})
				}
			},
		},
	}
</script>

<style>
	page {
		background: #F1F1F1;
	}

	.wxright {
		color: #999999;
	}

	.item {
		height: 98rpx;
		background: #FFFFFF;
		padding: 0 20rpx;
		border: none;
		border-radius: 0;
	}

	.wxlogon {
		width: 40rpx;
		height: 40rpx;
		margin-right: 10rpx;
	}

	.Under {
		position: fixed;
		width: 100%;
		bottom: 50rpx;
	}

	.login-btn {
		width: 630rpx;
		height: 90rpx;
		background: linear-gradient(132deg, rgb(64, 158, 255) 0%, rgb(64, 158, 255) 100%);
		box-shadow: 0px 2rpx 12rpx rgb(64, 158, 255);
		opacity: 1;
		border-radius: 45rpx;
		margin: 0 auto;
		color: #FFFFFF;
		font-size: 30rpx;
		font-family: Source Han Sans CN;
		font-weight: 500;
		opacity: 1;
	}
</style>
