<template>
	<view class="container login">
		<view class="login-main">
			<image class="logo" src="../../static/login-logo@3x.png" mode=""></image>
			<text class="logo-text">生美/医美采购服务平台</text>
		</view>
		<view class="login-input">
			<input type="number" 
				   v-model="invitationCode"  
				   maxlength="6" 
				   class="input" 
				   placeholder="请输入邀请码"
			/>
		</view>
		<view class="login-btn"  @click="goLogin">授权登录</view>
		<!-- 授权按钮 -->
		<view class="model-warp" :class="[isUserInfo ? 'show':'none']">
			<view class="model-alert">
				<view class="alert-content">
					<view class="t-p">采美采购商城需要获取您的微信授权才能正常提供服务</view>
				</view>
				<view class="alert-btn">
					<view class="btn btn-cancel" @click="hideModel">取消</view>
					<button type="primary" size="small" open-type="getUserInfo" lang="zh_CN" @getuserinfo="getuserinfo" class="btn btn-confirm">授权</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {mapState,mapMutations } from 'vuex';
	import authorize from '@/common/config/authorize.js' 
	import { invitationCodeLogin  } from '@/api/use.js'
	export default{
		data() {
			return{
				invitationCode:'',  //获取用户登录的邀请码
				isUserInfo:false,	//控制显示授权弹窗
				nickName:'',		//存储用户名
				userInfo:'',		//存储微信用户授权信息
				isSuccess:false,
				toestText:'',
				telPhone:'',
				loginType:'',		//跳转类型
				alertText:'',
				listType: '',
				listVal: '',
				detilType:'',
				id:''//商品ID
			}
		},
		onLoad(option) {
			console.log(option)
			let data = JSON.parse(option.data);
			this.getOption = data
			this.loginType = option.type;
			this.id = option.id
			if(option.listType) {
				this.listType = option.listType;
				this.listVal = option.listVal;
			}
		},
		methods:{
			...mapMutations(['login']),
			goLogin() {
				if( this.invitationCode == ''){
					this.$util.msg('请输入邀请码',2000)
					return
				}
				
				//查看此微信用户是否已经授权过
				authorize.getSetting().then(res =>{
					// console.log('是否已授权',res);//0:为取消授权 1:为已授权 2:为未操作
					if(res == 2){
						this.isUserInfo = true
					}else{
						this.isUserInfo = false
						this.wxGetUserInfo()
					}
				})				
			},
			//授权登录 
			getuserinfo: function (e) {
			    if (e.detail.userInfo) {
					this.wxGetUserInfo()
			    }else{
					//用户按了拒绝按钮
					uni.showModal({
						content: '获取用户信息失败，请允许授权后才能继续使用小程序哦~',
						showCancel: false,
						confirmText: '授权',
						success: function (res) {
							 if (res.confirm) {
								uni.openSetting({
									 success: res => {
										// console.log(res.authSetting)
									 },
									 fail: res => {
										// console.log(res)
									 }
								})
							 }
						}
					})
			    }
			 },
			wxGetUserInfo(){
				authorize.getCode('weixin').then(wechatcode =>{
					wx.getUserInfo({
						success: res => {
							console.log('useInfo:',res.userInfo)
							this.isUserInfo = false
							this.userInfo = res.userInfo;
							let params ={
									invitationCode:this.invitationCode,
									nickName:res.userInfo.nickName,
									headimgurl:res.userInfo.avatarUrl,
							}							
							invitationCodeLogin(params).then(response =>{
								if(response.code == 0){
									this.login(response.data)
									uni.switchTab({
										url:'/pages/tabBar/user/user'
									})
								}else if(response.code == 4){
									this.login(response.data)
									this.$api.navigateTo(`/pages/user-module/bindemail?data=${JSON.stringify(this.getOption)}&pathType=1`)
								}else{
									this.$util.msg(response.msg,2000)
									this.isUserInfo = false
								}
							})
						}
					});	
				})
			},
			goUserLogininit(){
				let url;
				if(this.loginType) {
					if(this.loginType=='detilType'){
						this.$api.redirectTo(`/pages/goods/product?id=${this.id}&page=2`);
					}else if(this.loginType=='search'){
						this.$api.redirectTo('/pages/search/search');
					}else if(this.loginType == 1){
						url ='/pages/tabBar/cart/cart'
					}else if(this.loginType == 4){
						url ='/pages/tabBar/user/user'
					}else {
						url ='/pages/tabBar/home/home'
					}
					uni.switchTab({
						url
					})
				} else if(this.listType) {
					this.$api.navToListPage({type:this.listType,value:this.listVal,lType:'4'});
				}
			},
			//关闭未授权用户授权提示弹窗
			hideModel(){
				this.isUserInfo = false;
			}
		}
	}
</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: 198rpx;
			padding: 170rpx 0 60rpx 0;
			.logo{
				width: 138rpx;
				height: 118rpx;
				display: block;
			}
			.logo-text{
				font-size: 30rpx;
				line-height: 44rpx;
				color: $color-system;
				font-weight: 600;
				margin-top: 20rpx;
			}
		}
		.login-input{
			width: 654rpx;
			height: 40rpx;
			padding: 24rpx;
			margin: 0 auto;
			margin-bottom: 60rpx;
			background: #F7F7F7;
			border-radius: 14rpx;
			.input{
				width: 100%;
				height: 100%;
				background: #F7F7F7;
				font-size: $font-size-28;
				line-height: 40rpx;
				color: #333333;
				border-radius: 14rpx;
			}
		}
		.login-btn{
			width: 702rpx;
			height: 88rpx;
			border-radius: 14rpx;
			font-size: $font-size-28;
			line-height: 88rpx;
			color: #FFFFFF;
			margin: 0 auto;
			text-align: center;
			background: $btn-confirm;
		}
		.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>
