<template>
	<view class="container login" :style="{paddingTop:CustomBar+'px'}" v-if="isSeller">
		<cu-custom :navbar-data='nvabarData'  @navigateBack="hanldNavigateBack"></cu-custom>
		<view class="login-main">
			<image class="logo" src="http://static-b.caimei365.com/app/img/icon/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="accountNumber"  maxlength="11" class="input" placeholder="请输入手机号"/>
			</view>
			<view class="login-input">
				<input v-show="isShowEye" type="text" v-model="password"  maxlength="18" class="input" placeholder="请输入密码" autocomplete="new-password"/>
				<input v-show="!isShowEye" type="password" v-model="password"  :password="true" maxlength="18" class="input" placeholder="请输入密码" autocomplete="new-password"/>
				<view class="iconfont" :class="isShowEye ? iconEyen : iconEyes"  @click="passwordClick"></view>
			</view>
		</view>
		<view class="login-btn"  @click="confirmLogin">协销登录</view>
		<!-- 微信用户已绑定供应商账户 -->
		<error-alert v-if="iseErrorAlert"></error-alert>
	</view>
</template>

<script>
	import { mapState,mapMutations } from 'vuex';
	import errorAlert from '@/components/cm-module/modelAlert/errorAlert.vue'
	import authorize from '@/common/config/authorize.js' 
	import { userInfoLogin } from "@/api/use.js"
	var self;
	export default{
		components:{
			errorAlert
		},
		data() {
			return{
				nvabarData: {		//顶部自定义导航
					showCapsule: 0, // 是否显示左上角图标  1表示显示  0表示不显示,
					showSearch: 0,
					title: '登录',  // 导航栏 中间的标题
					haveBack:false,
					textLeft:this.$store.state.isIphone
				},
				isIphoneX:this.$store.state.isIphoneX,
				CustomBar:this.CustomBar,// 顶部导航栏高度
				isShowEye:false,
				isSeller:true,
				iseErrorAlert:false,
				iconEyes:'icon-yanjing_yincang_o',
				iconEyen:'icon-yanjing_xianshi_o',
				accountNumber:'',  //协销用户登录账号
				password:'',	   //协销登录密码	
			}
		},
		onLoad() {
			authorize.getSetting().then(wxResponse =>{// console.log('是否已授权',res);//0:为取消授权 1:为已授权 2:为未操作
				if(wxResponse == 1){
					this.getWxAuthorize()
				}else{
					this.$api.navigateTo('/pages/authorization/authorization')
					this.isSeller= true
				}
			})	
		},
		computed: {
			...mapState(['isWxAuthorize'])
		},
		methods:{
			...mapMutations(['login','logout']),
			getWxAuthorize(){
				authorize.getCode('weixin').then(wechatcode =>{
					// 根据微信的code获取用户登录状态:1已登录过 -1未登录过
					authorize.getUserInfo('weixin').then(wxResponse =>{
						userInfoLogin({code:wechatcode,encryptedData:wxResponse.encryptedData,iv:wxResponse.iv}).then(response =>{
							if(response.code != -6){//判断是否为微信绑定了供应商的账户
								if(response.data.userIdentity !=1){
									this.logout()
									uni.removeStorageSync('sessionid')
									uni.setStorageSync('sessionid','JSESSIONID='+response.data.sessionId)
									this.isSeller= true
								}else{
									this.login(response.data);
									this.$store.commit('updateStatus',response.data)
									uni.setStorageSync('token',response.data.token)
									uni.removeStorageSync('sessionid')
									uni.setStorageSync('sessionid','JSESSIONID='+response.data.sessionId)
									this.$api.navigateTo('/seller/pages/index/index')
								}
							}else{
								this.isSeller= true
								this.iseErrorAlert= true
							}
						}).catch(error =>{
							this.logout()
							uni.removeStorageSync('sessionid')
							uni.setStorageSync('sessionid','JSESSIONID='+error.data)
							this.isSeller= true
						})
					})
				})	
			},	
			confirmLogin(){
				if( this.accountNumber == ''){
					this.$util.msg('请输入账户名',2000)
					return
				}
				if( this.password == ''){
					this.$util.msg('请输入密码',2000)
					return
				}
				authorize.getSetting().then(wxResponse =>{// console.log('是否已授权',res);//0:为取消授权 1:为已授权 2:为未操作
					if(wxResponse == 1){
						this.SellerLogin()
					}else{
						this.$api.navigateTo('/pages/authorization/authorization?type=1')
					}
				})	
			},
			SellerLogin(){
				this.SellerService.SellerLogin({mobile:this.accountNumber,password:this.password}).then(response =>{
					if(response.code == '0' ){
						this.$store.commit('updateStatus',response.data)
						this.login(response.data);
						uni.setStorageSync('token',response.data.token)
						uni.removeStorageSync('sessionid')
						uni.setStorageSync('sessionid','JSESSIONID='+response.data.sessionId)
						this.$api.redirectTo('/seller/pages/index/index')
					}else{
						this.$util.msg(response.msg,2000);
					}
				})
			},
			storeUpdataeStatus(data){
				let user_key = {
						clubID:data.clubID,
						shopID:data.shopID,
						userID:data.userID,
						bindMobile:data.bindMobile,
				}						
				uni.setStorageSync('token',data.token)
				this.$store.commit('updateStatus',user_key)
				this.login(data);
			},
			passwordClick() { //密码显隐操作
				this.isShowEye = !this.isShowEye;
			},
		},
		onShow() {
			
		}
	}
</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: 600rpx;
			height: 40rpx;
			padding: 24rpx 0;
			margin: 0 auto;
			margin-bottom: 20rpx;
			background: #FFFFFF;
			position: relative;
			border-bottom: 1px solid #E1E1E1;
			.input{
				width: 100%;
				height: 100%;
				background: #FFFFFF;
				font-size: $font-size-28;
				line-height: 40rpx;
				color: #333333;
			}
			.iconfont{
				position: absolute;
				right: 0;
				top: 0;
				font-size: 44rpx;
				color: #999999;
				font-weight: bold;
				z-index: 99;
				width: 96rpx;
				height: 96rpx;
				line-height: 96rpx;
				text-align: center;
			}
			&.link{
				background: #FFFFFF;
				margin-bottom: 40rpx;
				padding: 0 24rpx;
				line-height: 40rpx;
				font-size: $font-size-28;
				.login-reg{
					float: left;
					color: $color-system;
				}
				.login-pwd{
					float: right;
					color: $text-color;
				}
			}
		}
		.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;
			margin-top: 100rpx;
		}
		.login-tel{
			width: 702rpx;
			font-size: $font-size-28;
			line-height: 240rpx;
			margin: 0 auto;
			color: $text-color;
			text-align: center;
			margin-top: 100rpx;
		}
	}
</style>
