<template>
	<view class="content" @click="onShowLog">
		<!-- <view class="back-img"><image src="/static/img/img018.png" model="scoll-img"></image></view> -->
		<!-- <button class="weixin"  @click="loadData()">微信用户一键登录</button> -->
		<view class="info">
			<!-- 登录代表你已同意 -->
			<!-- <view class="text">
				<view class="text" @click="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: ''
		};
	},
	computed: {
		...mapState(['urlFile'])
	},
	onReady() {
		// uni.showModal({
		// 	title: 'wxLogin执行onLoad',
		// 	// content: path,
		// })
		history.replaceState({ name: '首页' }, '首页', this.urlFile+'/#/pages/index/home');
		setTimeout((e) => {
			this.loadData();
		},50)
		
	},
	methods: {
		...mapMutations(['login', 'setUserInfo']),
		onShowLog(){
			this.$log.showLog()
			this.$log.delLog()
		},
		// 获取微信登录信息
		loadData() {
			this.$log.addLog('开始登录流程')
			// #ifdef H5
			loginWinxin();
			// #endif
			// #ifdef MP-WEIXIN
			let obj = this;
			wx.login({
				success(e) {
					obj.code = e.code;
				},
				fill: function(e) {}
			});
			// #endif
		},
		// 用户确认授权
		userInfoData(e) {
			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(11);
					console.log(e.message);
				});
		},
		GetUser() {
			// 获取用户基础信息
			getUserInfo({})
				.then(({ data }) => {
					this.setUserInfo(data);
					console.log(uni.getStorageSync('userInfo'),'userInfo')
				})
				.catch(e => {
					console.log(e);
				});
		},
		ToIndex() {
			let obj = this;
			let ur = uni.getStorageSync('present') || '/pages/index/home';
			// 用于处理缓存bug
			if (ur == 'pages/index/home') {
				ur = '/pages/index/home';
			}
			uni.switchTab({
				url: ur,
				fail(e) {
					uni.navigateTo({
						url: ur,
						fail(e) {
							uni.navigateTo({
								url: '/pages/index/home'
							});
						}
					});
				}
			});
		},
		//手机号登录
		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: 524rpx;
		height: 529rpx;
	}
}
.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:#6786FB;
	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: #6786FB;
		display: inline-block;
	}
}
</style>
