
<template>
	<view class="container login">
		<view class="login-main">
			<image class="logo" src="https://admin-b.caimei365.com/userfiles/1/images/photo/2020/11/logo.png" mode=""></image>
		</view>
		<view class="login-input">
			<input type="number" 
				   v-model="invitationCode"  
				   maxlength="6" 
				   class="input" 
				   placeholder="请输入邀请码"
			/>
		</view>
		<view class="login-row" @click.stop="this.$api.navigateTo('/pages/login/register-select')"><text>免费注册</text></view>
		<view class="login-btn"  @click.stop="goLogin">登录</view>
		<view class="login-btn-last" @click.stop="this.$api.navigateTo('/pages/login/login')">账号登录</view>
	</view>
</template>

<script>
	import authorize from '@/common/config/authorize.js'
	import wxLogin from "@/common/config/wxLogin.js"
	import {mapState,mapMutations } from 'vuex';
	export default{
		data() {
			return{
				invitationCode:'',  //获取用户登录的邀请码
				unionId:'',
			}
		},
		onLoad(option) {

		},
		computed: {
			...mapState(['isWxAuthorize','isLoginType','isLoginProductId','isLoginOrderId'])
		},
		methods:{
			...mapMutations(['login']),
			goLogin() {
				const userInfo = uni.getStorageSync('wechatUserInfo')
				if( this.invitationCode == ''){
					this.$util.msg('请输入邀请码',2000)
					return
				}
				this.isUserInfo = false
				this.UserService.InvitationCodeLogin({
						invitationCode:this.invitationCode,
						nickName:userInfo.nickName,
						avatarUrl:userInfo.avatarUrl,
						unionId:uni.getStorageSync('unionId')
				})
				.then(response =>{
					wxLogin.wxLoginAuthorize()
					if(response.data.userIdentity === 3){
						setTimeout(()=>{
							this.$api.navigateTo('/supplier/pages/index/index')
						},1500)
					}else{
						setTimeout(()=>{
							switch(this.isLoginType){
								case 9:
									this.$api.navigateTo(`/h5/pages/activity/activity_mid`)
									break;
								case 8:
									this.$api.navigateTo(`/pages/goods/product?id=${this.isLoginProductId}`)
									break;
								case 7:
									this.$api.navigateTo(`/pages/user/order/order-details?type=share&orderID=${this.isLoginOrderId}`)
									break;
								default:
									this.$api.switchTabTo('/pages/tabBar/user/user')
							}
						},1500)
					}
				})
				.catch(error =>{
					this.$util.msg(error.msg,2000)
					this.isUserInfo = false
				})
			},
			InitAuthorize(){ //是否已授权 0:为取消授权 1:为已授权 2:为未操作
				authorize.getSetting().then(res =>{
					console.log('用户是否授权过',res)
					if(res == 1){
						wxLogin.wxLoginQuick()
					}
				})
				// if(uni.getStorageSync('_WX_State')){
				// 	wxLogin.wxLoginQuick()
				// }
			}
		},
		onShow() {
			this.$api.getStorage().then((resolve) =>{
				this.params.unionId = resolve.unionId ? resolve.unionId : 0
			})
			this.InitAuthorize()
		}
	}
</script>

<style lang="scss">
	.login{
		width: 100%;
		height: auto;
		.model-warp.none{
			display: none;			
		}
		.model-warp.show{
			display: block;
		}
		.login-main{
			width: 100%;
			display: flex;
			flex-direction: column;
			align-items: center;
			height: 189rpx;
			padding:60rpx 0 40rpx 0;
			margin-bottom: 70rpx;
			.logo{
				width:467rpx;
				height: 189rpx;
				display: block;
			}
		}
		.login-input{
			width: 600rpx;
			height: 88rpx;
			padding: 24rpx 0;
			margin: 0 auto;
			margin-bottom: 30rpx;
			background: #FFFFFF;
			position: relative;
			box-sizing: border-box;
			border-bottom: 1px solid #E1E1E1;
			.input{
				width: 100%;
				height: 100%;
				background: #FFFFFF;
				font-size: $font-size-28;
				line-height: 88rpx;
				color: #333333;
			}
		}
		.login-row{
			padding: 0 75rpx;
			font-size: $font-size-28;
			line-height: 40rpx;
			color: #E15616;
			margin-bottom: 48rpx;
			text-align: right;
		}
		.login-btn{
			width: 600rpx;
			height: 88rpx;
			border-radius: 44rpx;
			font-size: $font-size-28;
			line-height: 88rpx;
			color: #FFFFFF;
			margin: 0 auto;
			text-align: center;
			background: $btn-confirm;
		}
		.login-btn-last{
			width: 600rpx;
			height: 86rpx;
			border-radius: 44rpx;
			font-size: $font-size-28;
			line-height: 88rpx;
			color: $color-system;
			margin: 0 auto;
			text-align: center;
			border: 1px solid $color-system;
			margin-top: 20rpx;
		}
		.model-authorization{
			width: 100%;
			height: 100%;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 999;
			.authorization{
				width: 518rpx;
				height: 320rpx;
				position: absolute;
				background: rgba(255,255,255,.7);
				left: 0;
				right: 0;
				bottom: 0;
				top: 0;
				margin: auto;
				.to-btn{
					position: absolute;
					top: 0;
					left: 0;
					right: 0;
					bottom: 0;
					margin: auto;
					width: 70%;
					height: 88rpx;
					font-size: $font-size-28;
					line-height: 88rpx;
					color: #FFFFFF;
					text-align: center;
					border-radius: 44rpx;
				}
			}
		}
	}
</style>
