<template>
	<view class="container">
		<view v-if="!wxOpendata&&isload">
			<view class="logoBox">
				<image src="../../static/img/sign_bg.png" mode="" class="euroImg"></image>
			</view>
			
			
			<view class="title2">请完成微信授权以继续使用</view>
			<button class="wxlogin" @click="wxgologin">授权微信用户信息</button>
			<view class="title3" @click="goIndex">取消授权</view>
		</view>

	</view>

</template>

<script>
	export default {
		data() {
			return {

				wxdata: '',
				wxCode: '',
				openID: '',
				session_key: '',
				wxPhoneData: '',
				type: '',
				wxOpendata: '',
				isload: false,
				souquan:true,
			}
		},
		onLoad(opt) {
			this.type = opt.type;
			this.wxOpendata = uni.getStorageSync("wxOpendata");

			if (this.wxOpendata) {
				uni.switchTab({
					url: '../homePage/homePage'
				})
			} else {
				this.isload = true;
			}
		},
		methods: {
			goIndex() {
				uni.switchTab({
					url: '../homePage/homePage'
				})
			},

			//api uni.getUserProfile
			wxgologin() {
				
				var that = this;
				if (that.souquan == false) {
					return
				}
				uni.getUserProfile({
					lang: 'zh_CN',
					desc: '登录',
					success: (res) => {
						console.log(res);
						that.wxdata = res;
						uni.setStorage({
							key: 'wxdata',
							data: res,
							success: function() {
								that.ipLogin()

							}
						});
					},
					fail: (res) => {
						console.log(res)
					}
				});
				
				that.souquan = false;
				setTimeout(function() {
					that.souquan = true;
				}, 2000);
			},
			//api uni.login
			ipLogin() {
				var that = this;
				uni.showLoading({
					title: '加载中'
				});
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						// console.log(loginRes)
						that.wxCode = loginRes.code
						that.getWxOpenID()

					}
				});

			},

			getWxOpenID() {
				var that = this;
				this.$http('worldKeepCar/sys/getWxOpenID', {
					code: this.wxCode,
				}, 'GET').then(res => {
					// console.log(res)
					this.openID = res.data.openid;
					this.session_key = res.data.session_key;
					// console.log(this.openID)
					uni.setStorage({
						key: 'wxOpendata',
						data: res.data,
						success: function() {
							// that.ipLogin()
							uni.hideLoading();
							if (!that.type) {
								uni.switchTab({
									url: '../homePage/homePage'
								})
							} else {
								uni.redirectTo({
									url: 'login'
								})
							}
						}
					});

				})
			},

		}
	}
</script>

<style scoped>
	.euroImg {
		width: 290rpx;
		height: 140rpx;
	}

	.logoBox {
		text-align: center;
		padding-top: 219rpx;
	}

	
	.title2 {
		padding-top: 220rpx;
		font-size: 26rpx;
		text-align: center;
		color: #576B95;
		
	}

	.wxlogin {
		width: 658rpx;
		height: 88rpx;

		background: linear-gradient(135deg, #FD5300 0%, #FF270A 100%) #FF2400;
		border-radius: 10rpx;
		line-height: 88rpx;
		text-align: center;
		color: #FFFFFF;
		font-size: 30rpx;
		margin-top: 20rpx;
	}

	.title3 {
		width: 100vw;
		position: fixed;
		left: 0;
		bottom: 50rpx;
		font-size: 26rpx;
		text-align: center;
		color: #999999;
	}
</style>
