<template>
	<view class="login-box">
		<image src="https://m2.chinause.cn/mini/baixmini/img/logo.jpg" mode='aspectFit' class="login-logo"></image>
		<view class="login-title">农配领航</view>
		<view class="login-form">
			<input class="login-input" v-model="phone" placeholder-class placeholder="请输入手机号码" />
			<view class="login-input-btn">
				<input class="login-input" v-model="code" placeholder-class placeholder="验证码"/>
				<view class="login-checking" @click="checking" v-if="state===false">获取验证码</view>
				<view class="login-checking login-time" v-if="state===true">倒计时{{ currentTime }}s</view>
			</view>
			<input class="login-input" v-model="psw" placeholder-class password placeholder="请输入新密码"/>
			<input class="login-input" v-model="surepsw" placeholder-class password placeholder="再次输入密码"/>
			<button class="login-btn" @click="dopost()">立即提交</button>
			<navigator url="/pages/user/login" open-type='navigateBack' hover-class="none" class="login-label">已有账号，点此去登录.</navigator>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				state: false,		//是否开启倒计时
				totalTime: 60,		//总时间，单位秒
				recordingTime: 0, 	//记录时间变量
				currentTime: 0, 	//显示时间变量,
				code:'',
				psw:'',
				surepsw:'',
				phone:'',
			}
		},
		onLoad() {
	
		},
		methods: {
			async sendcode(phone) {
				let reinfo=await this.$api.sendcode({action:'sendcode',phone:phone});
				this.getinfo=reinfo.data;
				if(reinfo.code==1){
					//执行倒计时
					this.checkingTime();
				}
				uni.showToast({
					title:reinfo.msg,icon:'none'
				})
				
			},
			async updatepsw(phone,code,psw) {
				let reinfo=await this.$api.updatepsw({action:'updatepsw',phone:phone,psw:psw,code:code});
				this.getinfo=reinfo.data;
				if(reinfo.code==1){
					uni.showToast({
						title:reinfo.msg
					})
					setTimeout(function(e){
						uni.navigateBack({
							delta:1
						})
					},1500)
				}else{
					uni.showToast({
						title:reinfo.msg,icon:'none'
					})
				}
			},
			checking() {
				let phoneReg = /(^1\d{10}$)|(^[0-9]\d{7}$)/;
				if(!phoneReg.test(this.phone)) {
					uni.showToast({title: '请输入合法手机号!',icon: 'none'})
					return false;
				}
				//把显示时间设为总时间
				this.currentTime = this.totalTime;
				//开始倒计时
				this.state = true;
				this.sendcode(this.phone)
			},
			checkingTime(){
				let that = this;
				//判断是否开启
				if(this.state){
					//判断显示时间是否已到0，判断记录时间是否已到总时间
					if(this.currentTime > 0 && this.recordingTime <= this.totalTime){
						//记录时间增加 1
						this.recordingTime = this.recordingTime + 1;
						//显示时间，用总时间 - 记录时间
						this.currentTime = this.totalTime - this.recordingTime;
						//1秒钟后，再次执行本方法
						setTimeout(() => { 	
							//定时器内，this指向外部，找不到vue的方法，所以，需要用that变量。
							that.checkingTime();
						}, 1000)
					}else{
						//时间已完成，还原相关变量
						this.state = false;		//关闭倒计时
						this.recordingTime = 0;	//记录时间为0
						this.currentTime = this.totalTime;	//显示时间为总时间
					}
				}else{
					//倒计时未开启，初始化默认变量
					this.state = false;
					this.recordingTime = 0;
					this.currentTime = this.totalTime;
				}
			},
			dopost(){
				//正则表达式验证手机号
				let phoneReg = /(^1\d{10}$)|(^[0-9]\d{7}$)/;
				if(!phoneReg.test(this.phone)) {
					uni.showToast({title: '请输入合法手机号!',icon: 'none'})
					return false;
				}
				if(this.code==''){
					uni.showToast({title: '请输入验证码!',icon: 'none'})
					return false;
				}
				if(this.pwd==''||this.psw.length<6) {
					uni.showToast({title: '密码不少于6位!',icon: 'none'})
					return false;
				}
				if(this.psw!=this.surepsw) {
					uni.showToast({title: '两次输入密码不一致!',icon: 'none'})
					return false;
				}
				this.updatepsw(this.phone,this.code,this.psw)
			}
		}
	}
</script>

<style>
	.login-box{
		padding: 0 100upx;
		position: relative;
	}
	.login-logo{
		width: 100%;
		width: 100%;
		height: 310upx;
	}
	.login-title{
		position: absolute;
		top: 215upx;
		line-height: 260upx;
		font-size: 35upx;
		color: #a7b6d0;
		text-align: center;
		width: 100%;
		margin-left: -110upx;
	}
	.login-form{
		margin-top: 100upx;
	}
	.login-input{
		background: #e2f5fc;
		margin-top: 30upx;
		border-radius: 100upx;
		padding: 20upx 40upx;
		font-size: 36upx;
		height: auto;
	}
	.input-placeholder, .login-input{
		color: #94afce;
		line-height: 30upx;
	}
	.login-label{
		padding: 60upx 0;
		text-align: center;
		font-size: 30upx;
		color: #a7b6d0;
	}
	.login-btn{
		background: #348143;
		color: #fff;
		border: 0;
		border-radius: 100upx;
		font-size: 36upx;
		margin-top: 80upx;
	}
	.login-btn:after{
		border: 0;
	}
	.login-input{
		background: #e2f5fc;
		margin-top: 30upx;
		border-radius: 100upx;
		padding: 20upx 40upx;
		font-size: 36upx;
	}
	.input-placeholder, .login-input{
		color: #94afce;
	}
	.login-label{
		padding: 60upx 0;
		text-align: center;
		font-size: 30upx;
		color: #a7b6d0;
	}
	.login-btn{
		background: #348143;
		color: #fff;
		border: 0;
		border-radius: 100upx;
		font-size: 36upx;
		margin-top: 60upx;
	}
	.login-btn:after{
		border: 0;
	}
	
	/*验证码输入框*/
	.login-input-btn{
		position: relative;
	}
	.login-input-btn .login-input{
		padding-right: 260upx;
	}
	.login-checking{
		position: absolute;
		right: 0;
		top: 0;
		background: #348143;
		color: #fff;
		border: 0;
		border-radius: 110upx;
		font-size: 36upx;
		margin-left: auto;
		margin-right: auto;
		padding-left: 28upx;
		padding-right: 28upx;
		box-sizing: border-box;
		text-align: center;
		text-decoration: none;
		line-height: 2.22555556;
		-webkit-tap-highlight-color: transparent;
		overflow: hidden;
		padding-top: 2upx;
		padding-bottom: 2upx;
		margin-top: 4upx;
	}
	.login-checking.login-time{
		background: #a7b6d0;
	}
	
	/*按钮点击效果*/
	.login-btn.button-hover{
		transform: translate(1upx, 1upx);
	}
</style>
