<template>
	<view class="content">
		<view class="content-body">
			<view class="login-logo">
				<u-image src="data:image/png;base64,[encoded data, 18344 characters]"  width="382" height="248" 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="tyYd?'getPhoneNumber':''" 
				@click="getCheckYd"
				@getphonenumber="getPhoneNumber">
					手机号快捷登录
				</u-button>
				<u-button shape="circle" :custom-style="phoneButton" class="login-btn phoneBtn" @click="phoneLogin" :hoverClass='none'>
					手机验证码登录
				</u-button>
		    </view>
		</view>
		<view class="xieyi" style="display: flex;align-items: center;padding:50upx 0;justify-content: center;font-size: 24rpx;">
			<u-checkbox v-model="tyYd" :label-size="24">登录代表您已同意</u-checkbox>
			<text style="margin-left: -20rpx;" @click="gotoXieyi()">隐私政策、服务条款</text>
		</view>
	</view>
    
</template>

<script>
	import {wxlogin} from '../../api/login.js'
    import {
        mapState,
        mapMutations
    } from 'vuex'
     
    export default {
        components: {
        },
        data() {
            return {
				path: '/pages/index/index',
				lanuch: false,
				tyYd: false,
				mobile: '',
				code: '',
				codeText: '获取验证码',
				getCodeing: false,
				totalTime: 60,
                positionTop: 0,
				hasFocus: false,
				hasFocus1: false,
				wxButton:{
					background: '#00c800'
				},
				phoneButton:{
					background: '#ffffff'
				}
            }
        },
        computed: mapState(['forcedLogin']),
		mounted() {
			//微信平台登录
			//#ifdef MP-WEIXIN
			 this.$store.dispatch('wxLogin','weixin')
			//#endif
		},
		onLoad(option) {
			// 为解决首次登录异常问题 进入页面先调微信登录获取code
			this.wxLogin()
			const {
				path,
				lanuch,
				q,
				showModal
			} = option;
			this.lanuch = (lanuch + '') === 'true';
			if (path) {
				this.path = decodeURIComponent(path+"?q="+q);
			}
			if(showModal==1){
				uni.showModal({
					title: '提示',
					content: '岗位设置成功，需要重新登录！',
					showCancel: false,
					confirmText: '好的',
					success: (ret) => {
					}
				})
			}
			uni.removeStorageSync('showStoreAuthTips')
		},
        methods: {
			...mapMutations(['login']),
			changeYd(v){
				this.tyYd = v
			},
			getCheckYd(){
				if(!this.tyYd){
					uni.showToast({
						title: "请阅读并同意隐私政策、服务条款",
						icon: "none"
					})
					return
				}
			},
			// 微信授权登录
			getPhoneNumber(e) {
				let _this = this
				if(!this.tyYd){
					uni.showToast({
						title: "请阅读并同意隐私政策、服务条款",
						icon: "none"
					})
					return
				}
				
				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,
						}
						wxlogin(params).then(res => {
							uni.hideLoading();
							_this.code = ''
							console.log(res,_this.path, 'login data')
							if (res.status == '200') {
								this.toMain(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) {
						console.log(res.code)
						_this.code = res.code
					}
				})
			},
			toMain(data) {
			    this.login(data);
			    uni.reLaunch({
			        url: '/pages/index/index',
			    });
			},
			// 手机号登录
			phoneLogin(){
				wx.navigateTo({
					url: '/pages/login/phoneLogin'
				})
			},
            initPosition() {
                /**
                 * 使用 absolute 定位，并且设置 bottom 值进行定位。软键盘弹出时，底部会因为窗口变化而被顶上来。
                 * 反向使用 top 进行定位，可以避免此问题。
                 */
                this.positionTop = uni.getSystemInfoSync().windowHeight - 100;
            },
			gotoXieyi(url){
				uni.navigateTo({
					url: '/pagesA/xieyi/index?id=0'
				})
			}
        },
        onReady() {
            this.initPosition();
        }
    }
</script>

<style lang="less">
	.content{
		width: 100%;
		height: 100vh;
		padding:0 25upx;
		display: flex;
		flex-direction: column;
		text-align: center;
		.content-body{
			padding:0 100upx;
			height: 80%;
			.logo{
				display: inline-block;
				margin:250upx 0 120upx;
			}
			.phoneBtn{
				display: block;
				margin: 30rpx 0 350upx;
			}
		}
		.xieyi{
			text{
				color:#1283d4
			}
		}
	}
</style>
