<template>
	<view class="login-content">
		<view>
			<view>
				<view class="logo">
					<u-image src="/static/login_icon_logo" width="160" height="160" ></u-image>
				</view>
				<view class="login-btns">
					<u-button shape="circle" hover-class="none" :custom-style="wxButton" class="login-btn" type="success" :hair-line="false" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
						微信授权登录
					</u-button>
					<u-gap height="30"></u-gap>
					<u-button shape="circle" hover-class="none" :custom-style="phoneButton" class="login-btn" @click="pwLogin" type="primary">
						账号密码登录
					</u-button>
				</view>
			</view>
		</view>
		<view @click="toYs">登录即代表同意<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: '',
				wxButton: {
					background: '#07c160'
				},
				phoneButton: {
					background: '#00aaff'
				}
			}
		},
		mounted() {},
		onLoad(option) {
			//  隐藏小房子
			uni.hideHomeButton()
			// 为解决首次登录异常问题 进入页面先调微信登录获取code
			this.wxLogin()
			const {
				path,
				lanuch
			} = option;
			this.lanuch = (lanuch + '') === 'true';
			if (path) {
				this.path = decodeURIComponent(path);
			}
		},
		methods: {
			cansel(){
				this.$store.state.vuex_noLogin = true
				uni.reLaunch({
					url: '/pages/index/index'
				})
			},
			toYs(){
				uni.navigateTo({
					url: '/pages/agreement/agreement'
				})
			},
			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 = ''
							if (res.status == 200) {
								getApp().globalData.token = res.data
								_this.$u.vuex('vuex_token',res.data)
								uni.$emit("getUserInfo")
								if (_this.path === '/pages/index/index' || _this.lanuch) {
									uni.reLaunch({
										url: _this.path
									})
								} else {
									if (_this.path === '/pages/cleared/index' || _this.path === '/pages/userCenter/index') {  //  tabbar页面
										uni.switchTab({
											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
					}
				})
			},
			// 手机号登录
			pwLogin () {
			  wx.navigateTo({
			    url: '/pages/login/pwLogin'
			  })
			}
		}
	}
</script>

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