<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-form">
			<view class="login-input">
				<input type="number" 
					   v-model="accountCode"  
					   maxlength="30" 
					   class="input" 
					   placeholder="请输入邮箱/手机号"
				/>
			</view>
			<view class="login-input">
				<input :type="inputType"
					   v-model="password"  
					   maxlength="18" 
					   class="input" 
					   placeholder="请输入密码"
				/>
				<view class="iconfont" :class="passIconType"  @click.stop="changePasswordType($event)"></view>
			</view>
			<view class="login-input link">
				<view class="login-reg" @click.stop="this.$api.navigateTo('/pages/user-module/register')">免费注册</view>
				<view class="login-pwd" @click.stop="this.$api.navigateTo('/pages/user-module/password')">忘记密码?</view>
			</view>
		</view>
		<view class="login-btn"  @click="goLogin">登录</view>
		<view class="login-btn-last" @click.stop="this.$api.navigateTo('/pages/user-module/login')">授权登录</view>
		<view v-if="isToast" class="model-warp" >
			<view class="model-alert clearfix">
				<view class="alert-content">
					<view class="t-h1">{{toestText}}</view>
					<view class="t-p">请联系客服获取{{alertText}}后再登录，联系电话{{telPhone}}</view>
				</view>
				<view class="alert-btn">
					<view class="btn btn-confirm" @click="hideToast">确定</view>
				</view>
			</view>
		</view>
		<!-- 授权按钮 -->
		<view class="model-warp" :class="[isUserInfo===false ? 'none':'show']">
			<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 { mapMutations } from 'vuex';
	import authorize from '@/common/config/authorize.js' 
	var self;
	export default{
		data() {
			return{
				inputType:'password',
				passIconType:'icon-yanjing_yincang_o',
				accountCode:'',  //用户登录账号
				password:'',	 //用户登录密码	
				isToast:false,	 //控制显示未输入邀请码提示	
				isUserInfo:false,//控制显示授权弹窗
				nickName:'',	 //存储用户名
				userInfo:'',	 //存储微信用户授权信息
				isSuccess:false,
				toestText:'',
				telPhone:'',
				loginType:'',		//跳转类型
				alertText:'',
				listType: '',
				listVal: '',
				detilType:'',
				id:''//商品ID
			}
		},
		onLoad(option) {
			console.log(option)
			self = this;
			self.loginType = option.type;
			self.id = option.id
			if(option.listType) {
				self.listType = option.listType;
				self.listVal = option.listVal;
			}
		},
		methods:{
			...mapMutations(['login']),
			goLogin() {
				if(this.accountCode === ""){
					this.$util.msg('请输入账户名',3000);
					return
				}
				if(this.password === ""){
					this.$util.msg('请输入密码',3000);
					return
				}
				self.$api.get('/login/isEnabled',{userOrganizeID:self.userOrganizeID,invitationCode:self.invitationCode}, res => {
					if (res.code == "1") {
						//查看此微信用户是否已经授权过
						authorize.getSetting().then(res =>{
							// console.log('是否已授权',res);//0:为取消授权 1:为已授权 2:为未操作
							if(res == 2){
								self.isUserInfo = true
							}else{
								self.isUserInfo = false
								self.wxGetUserInfo()
							}
						})
					}else if(res.code =='0'){
						self.toestText ='请输入邀请码';
						self.alertText ='邀请码';
						self.telPhone = res.msg;
						self.isToast = true;
					}else if(res.code =='-2'){
						self.toestText ='邀请码已失效';
						self.alertText ='新邀请码';
						self.telPhone = res.msg;
						self.isToast = true;
					}else if(res.code =='-3'){
						self.toestText ='邀请码已被使用';
						self.alertText ='新邀请码';
						self.telPhone = res.msg;
						self.isToast = true;
					}else{
						uni.showToast({icon:'none', title: res.msg, duration: 3000});
						self.isUserInfo = false
					}
				})
			},
			//授权登录 
			getuserinfo: function (e) {
			    if (e.detail.userInfo) {
					self.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)
							self.userInfo = res.userInfo;
							let params ={
									code:wechatcode,
									nickName:res.userInfo.nickName,
									invitationCode:self.invitationCode,
									userOrganizeID:self.userOrganizeID,
								}
							self.goUserLogininit(params);
						}
					});	
				})
			},
			goUserLogininit(params){
				self.$api.lodingGet('/login/register',params, response => {
					// console.log(response)
					if (response.code == "1") {
						self.isUserInfo = false;
						self.login(self.userInfo);
						let user_key = {code:response.code,openid:response.data.openid,userID:response.data.userID}
						uni.setStorageSync('cookieKey','JSESSIONID='+response.data.sessionId);
						this.$store.commit('updateStatus',user_key)
						let url;
						if(self.loginType) {
							if(self.loginType=='detilType'){
								self.$api.redirectTo(`/pages/goods/product?id=${self.id}&page=2`);
							}else if(self.loginType=='search'){
								self.$api.redirectTo('/pages/search/search');
							}else if(self.loginType == 1){
								url ='/pages/tabBar/cart/cart'
							}else if(self.loginType == 2){
								url ='/pages/tabBar/user/user'
							}else {
								url ='/pages/tabBar/home/home'
							}
							uni.switchTab({
								url
							})
						} else if(self.listType) {
							self.$api.navToListPage({type:self.listType,value:self.listVal,lType:'4'});
						}
					} else {
						this.$util.msg(response.msg,3000);
					}
				})
			},
			//关闭未填邀请码弹窗
			hideToast(){
				self.isToast = false;
			},
			//关闭未授权用户授权提示弹窗
			hideModel(){
				self.isUserInfo = false;
			},
			changePasswordType(e){
				if(this.inputType ==='password'){
					this.inputType = 'text'
					this.passIconType = 'icon-yanjing_xianshi_o'
				}else{
					this.inputType = 'password'
					this.passIconType = 'icon-yanjing_yincang_o'
					
				}
			}
		}
	}
</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: 20rpx;
			background: #F7F7F7;
			border-radius: 14rpx;
			position: relative;
			.input{
				width: 100%;
				height: 100%;
				background: #F7F7F7;
				font-size: $font-size-base;
				line-height: 40rpx;
				color: #333333;
				border-radius: 14rpx;
			}
			.iconfont{
				position: absolute;
				right: 24rpx;
				top: 20rpx;
				font-size: 46rpx;
				color: $color-system;;
				font-weight: bold;
				z-index: 99;
			}
			&.link{
				background: #FFFFFF;
				margin-bottom: 40rpx;
				padding: 0 24rpx;
				line-height: 40rpx;
				font-size: $font-size-base;
				.login-reg{
					float: left;
					color: $color-system;
				}
				.login-pwd{
					float: right;
					color: $text-color;
				}
			}
		}
		.login-btn{
			width: 702rpx;
			height: 88rpx;
			border-radius: 14rpx;
			font-size: $font-size-base;
			line-height: 88rpx;
			color: #FFFFFF;
			margin: 0 auto;
			text-align: center;
			background: $btn-confirm;
		}
		.login-btn-last{
			width: 702rpx;
			font-size: $font-size-base;
			line-height: 160rpx;
			margin: 0 auto;
			color: $text-color;
			text-align: center;
		}
		.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-base;
					line-height: 88rpx;
					color: #FFFFFF;
					text-align: center;
					border-radius: 44rpx;
				}
			}
		}
	}
</style>
