<template>
	<view class="container login">
		<view class="login-main">
			<text class="logo-text">您的微信尚未绑定机构账号，填写以下资料进行绑定后，您能通过微信快速登录。</text>
		</view>
		<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="number" 
					   v-model="imageCode"  
					   maxlength="6" 
					   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="mobildeCode"  
					   maxlength="6" 
					   class="input" 
					   placeholder="请输入短信验证码"
				/>
			</view>
			<view class="login-input btn" :class="[isMobileDisabled  ? 'disabled' : '']" @click.stop="getMobileCodeFn">
				<button type="button"
						:disabled="isMobileDisabled"
					    class="input" 
				>{{ mobileCodeText }}</button>
			</view>
		</view>
		<view class="login-form clearfix">
			<view class="login-btn"  @click="bindWechatInfo">绑定</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' 
	import { bindingWechat } from "@/api/use.js"
	import { getImageCode, getbindWechatCode } from "@/api/utils.js"
	var self;
	export default{
		data() {
			return{
				userID:'',
				bindMobile:'',
				bindLinkName:'',  		//用户姓名
				bindLinkPhone:'',  		//用户手机号
				mobildeCode:'',  		//手机验证码
				imageCode:'',			//图形验证码
				imageCodeUrl:'',		//图形验证码地址
				imageCodetoken:'',		//图形校验token
				isMobileDisabled:false, //获取手机短信按钮
				isUserInfo:false,	//控制显示授权弹窗
				count: '',				//倒计时
				mobileCodeText: '获取验证码',
				codeTime: null,
				loginType:'',			//跳转类型
				alertText:'',
				listType: '',
				listVal: '',
				detilType:'',
				id:'',//商品ID
				getOption:''
			}
		},
		onLoad(option) {
			let data = JSON.parse(option.data);
			this.codeType = option.codeType
			this.option = option
			this.loginType = data.type;
			this.id = data.id
			if(data.listType) {
				this.listType = data.listType;
				this.listVal = data.listVal;
			}
			this.getVerificationCode()
		},
		methods:{
			...mapMutations(['login']),
			bindWechatInfo(){
				if( this.bindLinkName == ''){
					uni.showToast({title: '请输入姓名',duration: 2000,mask:true, icon:'none'})
					return
				}
				if( this.bindLinkPhone == ''){
					uni.showToast({title: '请输入手机号',duration: 2000,mask:true,icon:'none'})
					return
				}
				if(this.$reg.isMobile(this.bindLinkPhone)){
					uni.showToast({title: '请输入正确的手机号',duration: 2000,mask:true,icon:'none'})
					return
				}
				if( this.mobildeCode == ''){
					uni.showToast({title: '请输入手机验证码',duration: 2000,mask:true,icon:'none'})
					return
				}
				if(this.$reg.isMobileCode(this.mobildeCode)){
					uni.showToast({title: '验证码格式不正确',duration: 2000,mask:true,icon:'none'})
					return
				}
				//查看此微信用户是否已经授权过
				authorize.getSetting().then(res =>{
					// console.log('是否已授权',res);//0:为取消授权 1:为已授权 2:为未操作
					if(res == 2){
						this.isUserInfo = true
					}else{
						this.isUserInfo = false
						this.wxGetUserInfo()
					}
				})
			},
			getVerificationCode(){//图形验证
				getImageCode().then(res => {
					this.imageCodeUrl = res.data.baseImage
					this.imageCodetoken = res.data.token
				})
			},
			getMobileCodeFn(){//获取手机验证码
				let params = {
						mobile:this.bindLinkPhone,
						mobileOrEmail:this.bindMobile,
						platformType:2,
						imgCode:this.imageCode,
						token:this.imageCodetoken,
				}
				getbindWechatCode(params).then(res =>{
					const TIME_COUNT = 60;
					this.isMobileDisabled = true;
					this.$util.msg('验证短信已发送',2000)
			     	if (!this.codeTime) {
			       		this.count = TIME_COUNT;
			       		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( res =>{
					this.$util.msg(res.msg,2000)
				})
			},
			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'});
				}
			},
			//授权登录
			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 ={
									userID:this.userID,
									mobile:this.bindLinkPhone,
									linkName:this.bindLinkName,
									verificationCode:this.mobildeCode,
									nickName:res.userInfo.nickName,
									headimgurl:res.userInfo.headimgurl,
							}
							bindingWechat(params).then(res =>{
								this.goUserLogininit()
							}).catch(res =>{
								this.$util.msg(res.msg,2000)
							})							
						}
					});	
				})
			}
		},
		onShow() {
			this.$api.getStorage().then((resolve) => {
				this.userID = resolve.userID
				this.bindMobile = resolve.bindMobile
			})
		}
	}
</script>

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