<template>
	<view class="page">
		<view class="wrap">
			<image class="logo" src="../../static/image/logo.png" mode=""></image>
			<view class="hello">Hi，城市合伙人请登录</view>
			<button v-if="isLogin && !getPhone" class="btn phone" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">
				<image src="../../static/image/phone.png" mode="widthFix"></image>
				绑定手机
			</button>
			<template v-else>
				<view class="btn wechat" @click="login">
					<image src="../../static/image/wechat.png" mode="widthFix"></image>
					微信授权登录
				</view>
				<view class="btn phone">
					<image src="../../static/image/phone.png" mode="widthFix"></image>
					手机账号登录
				</view>
			</template>
			
			<view class="agree">
				<uni-data-checkbox class="check" multiple v-model="agree" :localdata="agreeData"></uni-data-checkbox>
				同意
				<text @click="toAgreement(0)">《用户服务协议》</text>
				<text @click="toAgreement(1)">《用户隐私协议》</text>
			</view>
		</view>
	</view>
</template>

<script>
	import { login } from '@/apis/modules/user.js'
	export default {
		data() {
			return {
				agree: [],
				agreeData: [{
					text: '',
					value: 1
				}],
				isLogin: true, // 是否已登录
				getPhone: true // 是否已授权手机号
			}
		},
		onShow() {
			// 先授权用户信息，再授权手机号
			this.checkLogin()
		},
		methods: {
			// 检查登录状态
			checkLogin() {
				if (uni.getStorageSync('token')) {
					this.$util.to('../index/index')
				} else {
					this.isLogin = false
					this.getPhone = false
				}
			},
			// 授权用户信息
			login() {
				if (this.agree.length) {
					const that = this
					uni.getUserProfile({
						lang: 'zh_CN',
						desc: '登录',
						success: ({ userInfo }) => {
							console.log(33, userInfo)
							uni.setStorageSync('userName', userInfo.nickName)
							uni.setStorageSync('avatar', userInfo.avatarUrl)
							uni.login({
								success: ({ code }) => {
									if (code) {
										this.isLogin = true
										login({ code }).then(({ data }) => {
											this.sessionKey = data.sessionKeyOpenId.session_key
											// uni.setStorageSync('token', userInfo.avatarUrl)
										}).catch(e => {})
									} else {
										that.$util.errMsg('登录失败！')
									}
								}
							})			
						},
						fail(res) {
							that.$util.errMsg('登录授权失败！')
						}
					})
				} else {
					this.$util.errMsg('请先阅读勾选协议！')
				}
			},
			onGetPhoneNumber(e){
				const { encryptedData, iv } = e.detail
				//获取手机号有两种方法，1.前端使用js完成；2.使用接口完成
				const WXBizDataCrypt = require('@/libs/WXBizDataCrypt')
				const accountInfo = uni.getAccountInfoSync() // 获取小程序appid
				const pc = new WXBizDataCrypt(accountInfo.miniProgram.appId , this.sessionKey)
				const data = pc.decryptData(encryptedData , iv)
				console.log(222, data)
			},
			toAgreement(id) {
				this.$util.to('../detail/detail?id=' + id)
			}
		}
	}
</script>

<style scoped lang="scss">
.page {
	min-height: calc(100vh - 170rpx);
	padding-top: 170rpx;
	background: url(../../static/image/login1.png) 0 0/175rpx auto no-repeat,
		url(../../static/image/login2.png) bottom right/123rpx auto no-repeat;
	overflow: hidden;
}
.wrap {
	position: relative;
	height: 60vh;
	padding: 214rpx 74rpx 28rpx;
	margin: 0 61rpx;
	text-align: center;
	background-color: #fff;
	border-radius: 20rpx;
	.logo {
		width: 393rpx;
		height: 93rpx;
	}
	.hello {
		margin: 36rpx 0;
		font-size: 28rpx;
		color: #333;
	}
	.btn {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100%;
		height: 88rpx;
		font-size: 32rpx;
		border-radius: 10rpx;
		image {
			width: 50rpx;
			margin-right: 15rpx;
		}
	}
	.wechat {
		margin-bottom: 38rpx;
		color: #fff;
		background-color: #007EFF;
	}
	.phone {
		color: #007EFF;
		border: 1px solid #007EFF;
		background-color: #fff;
		image {
			width: 40rpx;
		}
	}
	.agree {
		position: absolute;
		bottom: 28rpx;
		left: 0%;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100%;
		text-align: center;
		font-size: 24rpx;
		color: #ccc;
		text {
			color: #007EFF;
		}
	}
	/deep/.check {
		.checklist-box {
			margin: 0;
		}
		.checkbox__inner {
			border-radius: 50% !important;
		}
	}
}
</style>
