<template>
	<view class="content">
		<view class="content-logo">
			<view class="logo-cont">
				<u-image src="/static/login_icon_logo.png" width="160" height="160" class="logo"></u-image>
			</view>
			<view class="login-btns">
				<u-button shape="circle" :custom-style="wxButton" class="login-btn" type="success" :hair-line="false" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">
					微信授权登录
				</u-button>
			</view>
			<view class="nologin">
				<u-button shape="circle" size="mini" plain lass="login-btn" @click="cansel" type="info">
					暂不登录
				</u-button>
			</view>
		</view>
		<view @click="toYs" class="yszc">首次登录会自动创建账号且即代表同意<text class="yszc-text">《乐色超人服务协议》</text>及<text class="yszc-text">《隐私保护政策》</text></view>
	</view>
</template>

<script>
	import {login} from "@/api/login"
	export default {
		components: {},
		data() {
			return {
				path: '/pages/index/index',
				lanuch: false,
				code: '',
				wxButton: {
					background: '#0DC55F'
				},
				popupBox:{
					padding:'20'
				},
				cancelBtn:{
					background:'#eee',
					color:"#07C160",
					width:'100px',
					height:'35px',
					border:0
				},
				submitBtn:{
					width:'100px',
					height:'35px'
				}
			}
		},
		onLoad(option) {
			// 开启分享
			uni.showShareMenu({
				withShareTicket: true,
				menus: ['shareAppMessage', 'shareTimeline']
			})
			// 为解决首次登录异常问题 进入页面先调微信登录获取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()
				} 
				console.log(this.code, e)
				if (e.target.errMsg === 'getPhoneNumber:ok') {
					setTimeout(()=>{
						const params={
							code: _this.code,
							encryptedData: e.target.encryptedData,
							iv: e.target.iv,
						}
						login(params).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)
								console.log(getApp().globalData.token,'---------getApp().globalData.token','------登录token')
								uni.$emit("getUserInfo")
								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) {
						console.log(res.code)
						_this.code = res.code
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.content{
		width: 100%;
		height: 100vh;
		padding:0 130upx;
		margin: 0 auto;
		background: #fff;
		display: flex;
		flex-direction: column;
		.content-logo{
			flex: 1;
			.logo-cont{
				display: flex;
				flex-direction: column;
				align-items: center;
				.logo{
					display: inline-block;
					margin:240upx 256upx 120upx;
				}
			}
			.nologin{
				text-align: center;
				margin-top: 70upx;
				.login-btn{
					display: block;
					height: 80upx;
				}
			}
		}
		.yszc{
			font-size: 24upx;
			text-align: center;
			margin-bottom: 40upx;
			.yszc-text{
				color:#1995FF;
			}
			
		}
	}
</style>
