<template>
	<view class="content">
		<view class="back-img"><image src="https://sos.liuniu946.com/static/img/img002.png" model="scoll-img"></image></view>
		<!-- <view class="name">美天选菜</view>
	   <view class="tip">台州买菜用美天</view> -->
		<button class="weixin" open-type="getUserInfo" @getuserinfo="userInfoData">微信用户一键登录</button>
		<!-- <view class="login" @click="Tologin">手机号码登录</view> -->
		<view class="info" @click="checkboxChange">
			<radio style="transform:scale(0.7)" color="#ff5252" :checked="istype" />
			我已阅读并同意
			<view class="text">
				<!-- <view class="text" @click="agreement">用户协议，</view> -->
				<view class="text" @click.stop="privacy">隐私协议</view>
			</view>
		</view>
		<view class="Mask" v-show="MaskShow">
			<view class="Mask-box">
				<view class="title">申请获取您的手机号</view>
				<view class="text">为了方便更好的服务,需要您的手机授权</view>
				<button class="weixin" open-type="getPhoneNumber" @getphonenumber="PhoneNumber">获取手机号授权</button>
			</view>
		</view>
	</view>
</template>
<script>
import { mapMutations, mapState } from 'vuex';
// #ifdef MP-WEIXIN
import { loginWinxinMp } from '@/utils/wxMinProgram';
import { wechatMpAuth } from '@/api/wx';
// #endif
// #ifdef H5
import { loginWinxin } from '@/utils/wxAuthorized';
// #endif
import { getUserInfo, bangding } from '@/api/login.js';
export default {
	data() {
		return {
			userInfo: {}, //授权用户信息
			code: '', //授权code
			selected: false,
			disabled: false,
			MaskShow: false,
			cache_key: '',
			iv: '',
			encryptedData: '',
			istype:false
		};
	},
	onShow(option) {
		this.loadData();
	},
	methods: {
		...mapMutations(['login', 'setUserInfo']),
		checkboxChange() {
			this.istype = !this.istype;
			console.log(this.istype);
		},
		// 获取微信登录信息
		loadData() {
			console.log(1)
			// #ifdef H5
			console.log(2)
			loginWinxin();
			// #endif
			// #ifdef MP-WEIXIN
			console.log(3)
			let obj = this;
			wx.login({
				success(e) {
					obj.code = e.code;
				},
				fill: function(e) {
					console.log(e);
				}
			});
			// #endif
		},
		// 用户确认授权
		userInfoData(e) {
			if (!this.istype) {
				this.$api.msg('请查看并同意隐私政策');
				return;
			}
			let user = e.detail;
			user.code = this.code;
			this.loadMp(user);
			this.disabled = true;
		},
		// #ifdef MP-WEIXIN
		// 登录
		loadMp(option) {
			let obj = this;
			// 获取登录授权页数据
			wechatMpAuth({
				code: option.code,
				iv: option.iv,
				encryptedData: option.encryptedData
			}).then(({ data }) => {
				obj.wchatAuth(data);
			});
		},
		// #endif
		wchatAuth(data) {
			let obj = this;
			// 保存token
			uni.setStorageSync('token', data.userinfo.token);
			obj.cache_key = data.cache_key;
			obj.login();
			// 获取用户基础信息
			obj.setUserInfo(data.userinfo);
			if (data.userinfo.mobile == null || data.userinfo.mobile == '') {
				obj.MaskShow = true;
			} else {
				obj.ToIndex();
			}
		},
		PhoneNumber(e) {
			let obj = this;
			obj.MaskShow = false;
			(obj.iv = e.detail.iv), (obj.encryptedData = e.detail.encryptedData);
			bangding({
				flag: 1,
				cache_key: obj.cache_key,
				code: obj.code,
				iv: obj.iv,
				encryptedData: obj.encryptedData
			})
				.then(function(e) {
					obj.$api.msg(e.msg);
					// 获取用户基础信息
					obj.GetUser();
					obj.$api.msg(e.msg);
					obj.$nextTick(function() {
						obj.ToIndex();
					});
				})
				.catch(e => {
					console.log(e.message);
				});
		},
		GetUser() {
			// 获取用户基础信息
			getUserInfo({})
				.then(({ data }) => {
					this.setUserInfo(data);
					console.log(uni.getStorageSync('userInfo'), 'userInfo');
				})
				.catch(e => {
					console.log(e);
				});
		},
		ToIndex() {
			uni.switchTab({
				url: '/pages/index/index'
			})
			let obj = this;
			let ur = uni.getStorageSync('present') || '/pages/index/index';
			// 用于处理缓存bug
			if (ur == 'pages/index/index') {
				ur = '/pages/index/index';
			}
			uni.switchTab({
				url: ur,
				fail(e) {
					uni.navigateTo({
						url: ur,
						fail(e) {
							uni.navigateTo({
								url: '/pages/index/index'
							});
						}
					});
				}
			
			});
		},
		//手机号登录
		Tologin() {
			uni.navigateTo({
				url: '/pages/public/login'
			});
		},
		// 菜佬用户协议
		agreement() {
			uni.navigateTo({
				url: '/pages/public/agreement'
			});
		},
		//隐私协议
		privacy() {
			uni.navigateTo({
				url: '/pages/public/privacy'
			});
		}
	}
};
</script>

<style lang="scss">
page {
	background: #ffffff;
	height: 100%;
	.content {
		height: 100%;
		text-align: center;
		.Mask {
			position: fixed;
			top: 0;
			width: 100%;
			height: 100%;
			background-color: rgba(51, 51, 51, 0.7);
			.Mask-box {
				margin: auto;
				margin-top: 320rpx;
				padding: 50rpx 50rpx;
				width: 80%;
				height: 450rpx;
				top: 500rpx;
				left: 10%;
				background-color: #ffffff;
				border-radius: 15rpx;
				.title {
					font-size: 35rpx;
					font-weight: 700;
				}
				.text {
					font-size: 30rpx;
					color: #848484;
					padding-top: 50rpx;
				}
			}
		}
	}
}
.back-img {
	width: 100%;
	padding-top: 150rpx;
	image {
		width: 521rpx;
		height: 537rpx;
	}
}
.back-name {
	margin: 0rpx auto;
	margin-top: 50rpx;
	width: 239rpx;
	height: 98rpx;
	image {
		width: 100%;
		height: 100%;
	}
}
.name {
	color: #48ac34;
	font-size: 55rpx;
	font-weight: bold;
	letter-spacing: 15rpx;
	margin-top: 35rpx;
}
.tip {
	color: #48ac34;
	font-weight: bold;
	font-size: 28rpx;
	letter-spacing: 30rpx;
}
.weixin {
	background: linear-gradient(90deg, rgba(255, 102, 102, 1), rgba(255, 79, 79, 1));
	width: 65%;
	margin: 100rpx auto;
	color: #ffffff;
	border-radius: 50rpx;
	border: none;
}
.login {
	font-size: 26rpx;
	font-weight: 500;
	text-decoration: underline;
}
.info {
	// position: fixed;
	// bottom: 60rpx;
	margin: 0rpx auto;
	width: 100%;
	font-size: 26rpx;
	.text {
		color: #ff6666;
		display: inline-block;
	}
}
</style>
