<template>
	<view class="content">
		<view>
			<image src="https://eshangtech.com/ShopICO/yxcl-register-pht.png" mode="aspectFit"></image>
			<view class="desc" v-if="!user.WeChat_UserId ">
			 <!-- <view class="desc">申请获取你的公开信息(昵称、头像)</view> -->
				<button type="primary" class="btn" open-type="getUserInfo" @getuserinfo="bindGetUserInfo">微信授权</button>
			</view>
			<view class="desc" v-else-if="">
		 
				<button type="primary" class="btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">快速登录账号</button>
			</view>
			
			
		</view>
		<view class="login-tip-text">
			该小程序仅供企业内部人员登录使用，<br/>您的手机号是核实身份的唯一凭证。
		</view>
	</view>
</template>

<script>
	import { mapActions, mapGetters,mapMutations } from 'vuex'
	export default {
		data() {
			return {
				shouldNavback: false,
				
			}
		},
		computed:{
			...mapGetters({
				'user':'getUser',
			})
		},
		onLoad(option) {
			if(option.source=='detail'){
				this.shouldNavback = true
			}
		},
		methods: {
			...mapActions(['updateUser']),
			...mapMutations(['setUser']),
			getPhoneNumber (e) {
        console.log(e)
				uni.showLoading({
					title:'正在加载...',
					mask:true
				})
				let _this = this
				let _data = e.mp.detail
				let user = _this.user
				let arr = {
					encryptedData: encodeURIComponent(_data.encryptedData),
					iv: encodeURIComponent(_data.iv)
				}
				// _this.$request.$webPost('WebAPI_Push/WeChat/BusinessPhone',arr).then(function (rs) {
				_this.$request.$get('WeChat_BusinessPhone',arr).then(function (rs) {
					uni.hideLoading()
					if (rs.Result_Code === 100) {
							_this.setUser(rs.Result_Data)
							_this.updateUserFn(rs.Result_Data)
							_this.$util.toNextRoute('switchTab', '/pages/index/index')
							// uni.switchTab({url: '/pages/index/index'})
						  
						} else {
							uni.showToast({title: rs.Result_Desc || '服务器错误,请联系管理员。', icon: 'none'})
						}
				})
			},
			async updateUserFn(data) {
				this.updateUser(data)
			},
			bindGetUserInfo (e) { // 申请权限
				let _this = this
				let user = _this.user
				if (e.mp.detail) {
					uni.showLoading({
						title:'正在加载...',
						mask:true
					})
					uni.getUserInfo({
						success (res) {
							let arr = {
							  encryptedData: encodeURIComponent(res.encryptedData),
							  iv: encodeURIComponent(res.iv),
							}
							// _this.$request.$webPost('WebAPI_Push/WeChat/BusinessLicensed',arr).then(function (rs) {
							_this.$request.$get('WeChat_BusinessLicensed',arr).then(function (rs) {
								uni.hideLoading()
								 if(rs.Result_Code==100){
									let newUser = rs.Result_Data
									newUser.WeChat_MiniProToken = _this.user.WeChat_MiniProToken
									_this.setUser(newUser)
									if(newUser.Membership_Phone) {
										_this.updateUser(newUser)
										_this.$util.toNextRoute('switchTab', '/pages/index/index')
										// uni.reLaunch({url: '/pages/index/index'})
									}else{
										
									}
									_this.$forceUpdate()
								 }else{
									uni.showToast({
										title: rs.ResultDesc,
										icon: 'none',
										duration:2000
									})
								}
							})
						}
					})
				}
			}
		},
		onUnload() {
			this.$util.addUserBehavior()
		},
	}
</script>

<style scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-between;
		height: 100vh;
		box-sizing: border-box;
		background-color: #fff;
		padding-top: 60rpx;
	}
	image {
		width: 624rpx;
		height: 579rpx;
	}
	.btn {
		margin-top: 84rpx;
		width: 580rpx;
		font-size: 28rpx;
	}
	.login-tip-text {
		position: sticky;
		bottom: 120rpx;
		left: 0;
		width: 580rpx;
		z-index: 99;
		text-align: center;
		font-size: 26rpx;
		color: #888;
		
	}
</style>
