<template>
	<view class="content">
		<view>
			<view class="logo">
				<u-image src="/static/logo.png" width="140" height="140" ></u-image>
			</view>
			<view class="login-btns">
				<u-button shape="circle" class="login-btn" type="success" :hair-line="false" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
					微信登陆
				</u-button>
				<u-button shape="circle" class="login-btn" @click="phoneLogin" type="primary">
					手机号码登录
				</u-button>
			</view>
		</view>
		<view>登录即代表同意<text>隐私政策</text>和<text>服务条款</text></view>
	</view>
</template>

<script>
	import {
		login
	} from "@/api/login"
	export default {
		components: {},
		data() {
			return {
				path: '/pages/index/index',
				lanuch: true,
				code: ''
			}
		},
		onShow() {
			
		},
		mounted() {},
		onLoad(option) {
			// 为解决首次登录异常问题 进入页面先调微信登录获取code
			this.wxLogin()
			const {
				path,
				lanuch
			} = option;
			this.lanuch = (lanuch + '') === 'true';
			if (path) {
				this.path = decodeURIComponent(path);
			}
		},
		methods: {
			getPhoneNumber(e) {
				let _this = this
				uni.showLoading({
					mask: true,
					title: '加载中'
				});
				if (!this.code) {
					this.wxLogin()
				} 
				if (e.target.errMsg === 'getPhoneNumber:ok') {
					setTimeout(()=>{
						login({
							code: _this.code,
							encryptedData: e.target.encryptedData,
							iv: e.target.iv
						}).then(res => {
							uni.hideLoading();
							_this.code = ''
							console.log(res,_this.path, 'login data')
							if (res.status == '200') {
								getApp().globalData.token = res.data
								_this.$u.vuex('vuex_token',res.data)
								if (_this.path === '/pages/index/index' || _this.lanuch) {
									uni.reLaunch({
										url: _this.path
									});
								} else {
									uni.redirectTo({
										url: _this.path
									});
								}
							} else {
								uni.showToast({
									title: res.message,
									icon: 'none',
									duration: 2500
								});
							}
						});
					},300)
				} else {
					uni.showToast({
						title: '授权失败',
						icon: 'none',
						duration: 2500
					});
				}
			},
			// 调微信登录 获取code
			wxLogin () {
				let _this = this
				uni.login({
					provider: 'weixin',
					success(res) {
						_this.code = res.code
					}
				})
			},
			// 手机号登录
			phoneLogin () {
			  wx.navigateTo({
			    url: '/pages/login/phoneLogin'
			  })
			}
		}
	}
</script>

<style lang="scss">
	.content {
		width: 100%;
		height: 100%;
		padding: 40upx;
		display: flex;
		flex-direction: column;
		> view{
			text-align: center;
			&:first-child{
				flex-grow: 1;
				height: 80vh;
				padding: 20% 10%;
			}
			&:last-child{
				font-size: 24upx;
				text{
					color: #2979FF;
				}
			}
			.logo{
				text-align: center;
				display: flex;
				justify-content: center;
			}
			.login-btns{
				padding: 30upx 0;
				.login-btn{
					margin: 30upx 0;
				}
			}
		}
	}
</style>
