<template>
	<view>
		<view :style="'height:' + height + 'px'" class="d-flex flex-column j-center a-center">
			<image class="logo-img" src="../../static/logoLogin.png" >
			</image>
			<view class="distence-top">
				<button class="button" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信手机号授权登录</button>
			</view>
			
		</view>
			
			
	</view>
</template>

<script>
	export default {

		data() {
			return {
				height:0
			}
		},
		onLoad() {
			let that = this
			// 获取系统的高度
			uni.getSystemInfo({
				success(res) {
					that.height = res.windowHeight
				}
			})
			// this.getUserInfo()
		},
		methods: {
			// 1.调用微信自带登录方法获取code
			getUserInfo(){
				let that = this
				uni.login({
					success(res) {
						that.loginParams.code = res.code
						that.getSessionkeyAndOpenId()
					}
				})
				uni.getUserInfo({
					success(info) {
						that.signature = info.signature
						that.rawData = info.rawData
					}
				})
			},
			
			// 2.获取sessionKey,openId
			async getSessionkeyAndOpenId() {
				const res = await this.$myRequest({
					url:`/xcxLogin?appid=${this.loginParams.appid}&code=${this.loginParams.code}`,
				})
				if(res.data.code == 200){
					let dataObj = JSON.parse(res.data.data)
					this.sessionKey = dataObj.sessionKey
					this.openid = dataObj.openid
				}
			},
			// 3.授权手机号登录拿到encryptedData和iv
			getPhoneNumber(e) {
				// 同意登录
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					const iv = e.detail.iv
					const encryptedData = e.detail.encryptedData
					this.getNumberAndToken(iv,encryptedData)
				} else {
					// 取消授权 清空头像和昵称
					uni.navigateBack({
						delta: 1
					})
				}
			},
			// 4.获取token
			async getNumberAndToken(iv, encryptedData) {
				const res = await this.$myRequest({
					url:'/phone',
					method:'POST',
					data:{
						appid:this.loginParams.appid,
						encryptedData:encryptedData,
						iv:iv,
						sessionKey:this.sessionKey,
						openid:this.openid,
						rawData:this.rawData,
						signature:this.signature
					}
				})
				if(res.data.code == 200){
					console.log(res.data.data.token,'token')
					uni.setStorageSync('token',res.data.data.token)
					uni.setStorageSync('powerIds',res.data.data.powerIds)
					uni.setStorageSync('userInfo',res.data.data.feederUserVo)
					uni.showToast({
						title:'登录成功',
						icon:'none',
						duration:2000,
						success() {
							setTimeout(()=>{
								uni.reLaunch({
									url:'../index/index'
								})
							},1000)
						}
					})
				}
			},
			//使用账号密码
			handleUse(){
				uni.navigateTo({
					url:'../chooseLogin/chooseLogin'
				})
			}
		}
	}
</script>

<style>
	page{
		padding:0;
	}
	.logo-img{
		width: 200rpx;
		height:200rpx;
		border-radius: 50%;
	}
	.button {
		color: #FFFFFF;
		height: 90rpx;
		font-size: 18px;
		text-align: center;
		line-height: 90rpx;
		width: 550rpx;
		border-radius: 8px;
		background: #3476ff;
	}
	.use-text{
		text-align: center;
		font-size: 24rpx;
		color: #3476ff;
		margin: 20rpx auto;
	}
	.distence-top{
		margin-top: 429rpx;
	}
</style>
