<template>
	<view class='login-page'>
		<view class='login-top'>
			<image class='top-icon' src='http://ddh.yyhock.com/appStatic/login-logo.png'></image>
			<view class='top-name'>多典花</view>
		</view>
		<view class='login-main'>
			<view class='main-mobile'>
				<input class='login-m-mobile' placeholder="请输入手机号" :value='mobile' maxlength="11" type="number"
				             @input="changeInput($event)"/>
			</view>
			<view class='main-mobile login-m-send'>
				<input class='send-value' placeholder="请输入验证码" :value="sendcode" maxlength="6" type="number"
						                @input="changecode($event)"/>
				<view class='send-text' @click='GetVerifyCode' v-if="canSendCode">获取验证码</view>
				<view class="mobileCode-min" @click='GetVerifyCode' v-else>{{ count }}秒</view>
			</view>
			
		</view>
		<view class='login-btn' @click="loginBtn">登录</view>
		<view class='login-footer'>
			<view :class="clickStatus ? 'footer-click' : 'footer-icon' " @click="btnIcon"></view>
			<view class='footer-text'>
				我已阅读并同意 <text @click="goPro('1')">《注册协议》</text> 和 <text @click="goPro('2')">《隐私政策》</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				clickStatus:false,
				mobile:'',
				sendcode:'',
				canSendCode: true,
				timer: null,//倒计时
				count: '',//倒计时
				smsDownCacheCode:'',
				protocolf:null,
				protocols:null,
				
			}
		},
		onLoad(){
				this.init()
		},
		methods: {
			init(){
				const self=this	
				self.$request('/setting','GET').then(res => {
					console.log(res,'settings');
					const { protocol,phone }=res.result
					if(protocol){
						const protocolList=JSON.parse(protocol)
						protocolList.map((item)=>{
							// console.log(item,'item966')
							if(item.protocolName=='隐私协议'){
								self.protocols=item
							}else if(item.protocolName=='用户服务协议'){
								self.protocolf=item
							}
						})
					}
					
				}).catch(err=>{
				  console.log(err,'请求出错')
				})
			},
			btnIcon(){
				this.clickStatus=!this.clickStatus
			},
			goPro(type){
				if(type=='1'){
					const {protocolUrl,protocolName }=this.protocolf
					const items={
						name:protocolName,
						url:protocolUrl
					}
					uni.navigateTo({
						url:'/pages/webview/webview?mydata='+encodeURIComponent(JSON.stringify(items))
					})
				}else{
					const {protocolUrl ,protocolName}=this.protocols
					const items={
						name:protocolName,
						url:protocolUrl
					}
					uni.navigateTo({
						url:'/pages/webview/webview?mydata='+encodeURIComponent(JSON.stringify(items))
					})
				}
			},
			GetVerifyCode(){
				const self=this
				const params={
					"mobile": self.mobile
				}		
				self.$request('/client/sendCode','POST',params).then(res => {
					console.log(res,'sendCode');
					const { smsBizNo }=res.result
					if(smsBizNo){
						self.countdowns()
						self.smsDownCacheCode=smsBizNo
					}
				}).catch(err=>{
				  console.log(err,'请求出错')
				})
			},
			// 获取验证码倒计时
			countdowns() {
			      const TIME_COUNT = 60;
			      const self = this
			      if (!self.timer) {
			        self.count = TIME_COUNT;
			        self.canSendCode = false;
			        self.timer = setInterval(() => {
			          if (self.count > 0 && self.count <= TIME_COUNT) {
			            self.count--;
			          } else {
			            self.canSendCode = true;
			            clearInterval(self.timer);
			            self.timer = null;
			          }
			        }, 1000)
			      }
			},
			loginBtn(){
				const self=this
				if(self.mobile==''){
					uni.showToast({
					    title: '请先输入手机号',
					    icon: "none",
					    duration: 2000
					
					})
					return false
				}
				if(self.sendcode==''){
					uni.showToast({
					    title: '请先输入验证码',
					    icon: "none",
					    duration: 2000
					
					})
					return false
				}
				if(self.clickStatus==false){
					uni.showToast({
					    title: '请先勾选同意相关协议',
					    icon: "none",
					    duration: 2000
					
					})
					return false
				}
				const params={
					  "mobile": self.mobile,
					  "smsDownCacheCode":self.smsDownCacheCode,
					  "verifyCode": self.sendcode
				}		
				self.$request('/client/login','POST',params).then(res => {
					console.log(res,'login');
					const { userInfo,token}=res.result
					if(userInfo){
						const { deviceId}=userInfo
						uni.setStorageSync('deviceId',deviceId)
					}
					uni.setStorageSync('token',token)
					
					uni.navigateBack({
						delta: 1
					})
				}).catch(err=>{
				  console.log(err,'请求出错')
				})
			},
			changeInput(e){
				this.mobile = e.detail.value
			},
			changecode(e){
				 this.sendcode = e.detail.value
			}
		}
	}
</script>

<style src='./index.less' lang="less" scoped></style>
