<template>
	<view class="container login">
		<view class="login-main">
			<text class="logo-text">您的微信尚未绑定公司账号，填写以下资料进行绑定后，您能通过微信快速登录。</text>
		</view>
		<view class="login-container">
			<view class="login-form clearfix">
				<view class="login-input">
					<input type="text" 
						   v-model="bindLinkName"  
						   maxlength="30" 
						   class="input" 
						   placeholder="请输入姓名"
					/>
				</view>
			</view>
			<view class="login-form clearfix">
				<view class="login-input">
					<input type="number" 
						   v-model="bindLinkPhone"  
						   maxlength="11" 
						   class="input" 
						   placeholder="请输入手机号"
					/>
				</view>
			</view>
			<view class="login-form clearfix">
				<view class="login-input code">
					<input type="text" 
						   v-model="imageCode"  
						   maxlength="4" 
						   class="input" 
						   placeholder="请输入右侧图形验证码"
					/>
				</view>
				<view class="login-input img-btn">
					<view class="vscodeimg">
						<image :src="imageCodeUrl" mode=""></image>
					</view>
					<view class="vscod-refresh"  @click.stop="getVerificationCode">
						<text class="iconfont icon-shuaxin"></text>
						<text class="ref-text">刷新</text>
					</view>
				</view>
			</view>
			<view class="login-form clearfix">
				<view class="login-input code">
					<input type="number" 
						   v-model="smsCode"  
						   maxlength="6" 
						   class="input" 
						   placeholder="请输入短信验证码"
					/>
				</view>
				<view class="login-input btn" :class="[isMobileDisabled  ? 'disabled' : '']" >
					<button type="button"
							@click.stop="getMobileCodeFn"
							:disabled="isMobileDisabled"
						    class="input">
							{{ mobileCodeText }}
					</button>
				</view>
			</view>
			<view class="login-form none clearfix">
				<view class="login-btn"  @click="bindWechatInfo">绑定并登录</view>
			</view>
		</view>
	</view>
</template>

<script>
	import { mapState,mapMutations } from 'vuex'
	import authorize from '@/common/config/authorize.js' 
	import wxLogin from '@/common/config/wxLogin.js'
	export default{
		data() {
			return{
				userIdentity:'',
				userId:'',
				unionId:'',
				bindMobile:'',
				bindLinkName:'',  		//用户姓名
				bindLinkPhone:'',  		//用户手机号
				smsCode:'',  		//手机验证码
				imageCode:'',			//图形验证码
				imageCodeUrl:'',		//图形验证码地址
				imageCodetoken:'',		//图形校验token
				isMobileDisabled:false, //获取手机短信按钮
				count: '',				//倒计时
				mobileCodeText: '获取验证码',
				codeTime: null,
				nickName:'',
				avatarUrl:'',
			}
		},
		onLoad(option) {
			this.getVerificationCode()
		},
		computed: {
			...mapState(['isWxAuthorize','isLoginType','isLoginProductId','isLoginOrderId'])
		},
		methods:{
			...mapMutations(['login','wxLogin']),
			bindWechatInfo(){
				if( this.bindLinkName == ''){
					this.$util.msg('请输入姓名',2000)
					return
				}
				if( this.bindLinkPhone == ''){
					this.$util.msg('请输入手机号',2000)
					return
				}
				if(!this.$reg.isMobile(this.bindLinkPhone)){
					this.$util.msg('请输入正确的手机号',2000)
					return
				}
				if( this.smsCode == ''){
					this.$util.msg('请输入手机验证码',2000)
					return
				}
				if(!this.$reg.isMobileCode(this.smsCode)){
					this.$util.msg('验证码格式不正确',2000)
					return
				}
				this.isUserInfo = false
				this.GetUserProfile()
			},
			GetUserProfile(){//获取用户微信个人信息
				const self = this
				wx.getUserProfile({
					desc: '采美采购商城小程序获取您的信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success(res) {
						console.log('微信获取用户信息新API',res)	
						self.wxLogin(res.userInfo)	
						self.nickName = res.userInfo.nickName
						self.avatarUrl = res.userInfo.avatarUrl
						self.BindingWechat()
					},
					fail() {
						self.$util.msg('授权失败', 2000)
					}
				})
			},
			BindingWechat(){//绑定微信并登陆
				this.UserService.BindingWechat(
					{
						userId:this.userId,
						mobile:this.bindLinkPhone,
						linkName:this.bindLinkName,
						smsCode:this.smsCode,
						unionId:this.unionId,
						nickName:this.nickName,
						avatarUrl:this.avatarUrl,
					}
				)
				.then(response =>{
					if(this.userIdentity === 3){
						this.$api.navigateTo('/pages/supplier/index/index')
					}else{
						switch(this.isLoginType){
							case 9:
								this.$api.navigateTo('/pages/h5/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')
						}
					}
				})
				.catch(error =>{
					this.$util.msg(error.msg,2000)
				})	
			},
			getVerificationCode(){//图形验证
				this.PublicService.GetImgVerifyCode().then(res => {
					this.imageCodeUrl = res.data.baseImage
					this.imageCodetoken = res.data.token
				})
			},
			getMobileCodeFn(){//获取手机验证码
				if( this.bindLinkPhone == ''){
					this.$util.msg('请输入手机号',2000)
					return
				}
				if(!this.$reg.isMobile(this.bindLinkPhone)){
					this.$util.msg('请输入正确的手机号',2000)
					return
				}
				if( this.imageCode == ''){
					this.$util.msg('请输入图形验证码',2000)
					return
				}
				let params = {
						mobile:this.bindLinkPhone,
						bindMobile:this.bindMobile,
						platformType:2,
						isCheckCaptcha:0,
						imgCode:this.imageCode,
						token:this.imageCodetoken,
				}
				this.isMobileDisabled = true
				this.PublicService.GetBindMobileCode(params)
					.then(res =>{
						const TIME_COUNT = 60
						this.$util.msg('验证短信已发送',2000)
				     	if (!this.codeTime) {
				       		this.count = TIME_COUNT
							this.isMobileDisabled = true
				       		this.codeTime = setInterval(() => {
				       			if (this.count > 1 && this.count <= TIME_COUNT) {
				         			this.count--
				         			this.mobileCodeText = this.count +'s重新发送'
				        		} else {
					         		this.isMobileDisabled = false
					         		clearInterval(this.codeTime)
					         		this.codeTime = null
									this.mobileCodeText = '获取验证码'
				        		}
				       		},1000)
				      	}
					})
					.catch( error =>{
						this.$util.msg(error.msg,2000)
						this.isMobileDisabled = false
					})
			},
			//关闭未授权用户授权提示弹窗
			hideModel(){
				this.isUserInfo = false
			}
		},
		onShow() {
			this.$api.getStorage().then((resolve) => {
				console.log(resolve)
				this.unionId = resolve.unionId
				console.log(this.unionId)
				this.userId = resolve.userId
				this.bindMobile = resolve.bindMobile
				this.userIdentity = resolve.userIdentity
			})
		}
	}
</script>

<style lang="scss">
	.login{
		width: 100%;
		height: auto;
		box-sizing: border-box;
		border-top: 1px solid #F7F7F7;
		.model-warp.none{
			display: none;			
		}
		.model-warp.show{
			display: block;
		}
		.login-main{
			width: 100%;
			background: rgba(225, 86, 22, 0.1);
			display: flex;
			flex-direction: column;
			align-items: center;
			height: auto;
			padding: 20rpx 24rpx;
			margin: 24rpx 0 118rpx 0;
			box-sizing: border-box;
			.logo-text{
				font-size: 24rpx;
				line-height: 34rpx;
				color: $color-system;
			}
		}
		.login-container{
			width: 100%;
			height: auto;
			padding: 0 24rpx;
			box-sizing: border-box;
		}
		.login-form{
			width: 100%;
			height: auto;
			box-sizing: border-box;
			border-bottom: 1px solid #E1E1E1;
			&.none{
				border-bottom: none;
			}
			.login-input{
				width: 100%;
				height: 60rpx;
				padding: 20rpx 0;
				background: #FFFFFF;
				display: flex;
				flex-direction: column;
				align-items: center;
				&.code{
					width: 370rpx;
					float: left;
					margin-right: 20rpx;
				}
				&.btn{
					width: 220rpx;
					height: 64rpx;
					padding: 0;
					float: left;
					background: $btn-confirm;
					border-radius: 32rpx;
					margin-top: 15rpx;
					.input{
						width: 220rpx;
						height: 64rpx;
						line-height: 64rpx;
						padding: 0;
						border-radius: 32rpx;
						color: #FFFFFF;
						background: $btn-confirm;
					}
					&.disabled{
						background: #F7F7F7;
						.input{
							background: #F7F7F7;
							color: #999999;
						}
					}
				}
				&.img-btn{
					width: 300rpx;
					height: 64rpx;
					padding: 0;
					float: left;
					background: #FFFFFF;
					display: block;
					border-bottom: none;
					margin-top: 16rpx;
					.vscodeimg{
						width: 160rpx;
						height: 64rpx;
						float: left;
						display: flex;
						flex-direction: column;
						align-items: center;
						border-radius: 6rpx;
						image{
							width: 180rpx;
							height: 88rpx;
							border-radius: 6rpx;
						}
					}
					.vscod-refresh{
						width: 100rpx;
						float: right;
						height: 64rpx;
						.icon-shuaxin{
							font-size: 40rpx;
							color: #999999;
							display: block;
							float: left;
							height: 64rpx;
							line-height: 64rpx;
						}
						.ref-text{
							display: block;
							float: right;
							height: 64rpx;
							line-height: 64rpx;
							font-size: 24rpx;
							color: #999999;
						}
					}
				}
				.input{
					width: 100%;
					height: 100%;
					background: #FFFFFF;
					font-size: $font-size-28;
					line-height: 40rpx;
					color: #333333;
					border-radius: 14rpx;
				}
			}
		}
		.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;
		}
		.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>
