<template>
	<view >
		<u-navbar title="修改手机号"></u-navbar>
		<view class="login-form">
			<view class="login-form-item">
				<view class="input">
					<u-input   type="number"  v-model="form.phone"  placeholder="请输入手机号"/>
				</view>
			</view>
			<view class="login-form-item">
				<view class="input">
					<u-input  v-model="form.code"    type="number" placeholder="请输入验证码" placeholder-style="font-size:16px;color:#ccc;"/>
					<view class="code"  @click="getCode" >{{codeTips}}</view>
				</view>
				<u-verification-code :seconds="sendMsgSecond" ref="uCode" @change="codeChange" @end="end" @start="start">
				</u-verification-code>
			</view>
		</view>
		
		<u-button class="login-btn" @click="finish" type="success" shape="circle" >确认修改</u-button>
		
		
	</view>
</template>

<script>
	
	import * as API from '@/apis/pagejs/user.js'
	import {
		checkPhone
	} from '@/apis/utils'
	export default {
		data() {
			return {
				form: {
					name: '',
					
					phone: '',
					code: '',
				},
				codeTips: '',
				isSendMsgIng: false,
				sendMsgSecond: 60 * 2,
			}
		},
		methods: {
			
			codeChange(text) {
				this.codeTips = text;
			},
			//倒计时
					
			end() {
				this.sendMsgSecond = 2 * 60;
				this.isSendMsgIng = false;
			},
			finish() {
					
				if (!this.carhelp.getOpenId()) {
					// uni.showToast({
					// 	title: "请使用“微信”访问本系统登录"
					// })
					// return
				}
				if (!this.form.phone) {
					uni.showToast({
						title: "请输入手机号"
					})
					return
				}
				if (!this.form.code) {
					uni.showToast({
						title: "请输入验证码"
					})
					return
				}
					
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				
					
					
				API.changePhone({
					verifyCode: this.form.code,
					telephone: this.form.phone,
				
				}).then((response) => {
					
					this.query()
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			},
			start() {
				if (!this.isSendMsgIng) {
					
					
					uni.showLoading({
						title: "加载中",
						mask: true,
					})
					API.getVerifyCode(this.form.phone).then((response) => {
					
					
						uni.hideLoading();
						this.carhelp.set("getvcodetime", new Date().getTime());
					
						if (!"") {
							//倒计时
							uni.showToast({
								title: "发送成功"
							})
						} else {
							uni.showToast({
								title: "您的验证码已经发送[5分钟有效]，请勿重复点击"
							})
						}
					}).catch(error => {
						uni.showToast({
							title: error,
							icon: "none"
						})
					})
					
					
				}
			},
			// 获取验证码
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					
				} else {
					
					uni.showToast({
						title: '倒计时结束后再发送',
						icon: "none"
					})
					return
				}
					
				var checkPhoneResult = checkPhone(this.form.phone);
					
				if (checkPhoneResult !== true) {
					uni.showToast({
						title: checkPhoneResult,
					
					})
					return;
				}
				this.$refs.uCode.start();
			},loginset(response){
				var token = response ? response.data.token : '';
				this.carhelp.setToken(token);
				this.carhelp.setPersonInfo(response.data.regUser);
				this.carhelp.setPersonInfoPlus(response.data)
				//this.gotoUrl("pages/user/index")
				uni.redirectTo({
					url: '/pages/mine/mine'
				})
			},
			query(){
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				
				API.findByOpenId({
					noerror:true,
					openId: this.carhelp.getOpenId(),
					
				}).then((response) => {
					this.loginset(response)
					
				
				}).catch(error => {
					uni.hideLoading();
					// if (!this.carhelp.getOpenId()) {
					// 	uni.showToast({
					// 		title: "请使用“微信”访问本系统登录"
					// 	})
					// 	return
					// }
					var time = this.carhelp.get("getvcodetime");
					if (time) {
						//this.$refs.uCode.start();
						var nowtime = new Date().getTime()
						var differ = (nowtime - time) / 1000
						if (differ < 2 * 60) {
							this.sendMsgSecond = 2 * 60 - parseInt(differ)
							this.isSendMsgIng = true;
							this.$refs.uCode.start();
						}
					}
				})
			}
		
		}
	}
</script>
<style>
	page{
		background-color: #fff;
	}
</style>
<style lang="scss" scoped>
	
	
	
	.login-form{
		background-color: #fff;
		padding: 12px 28px;
		
	}
	.login-form-item{
		
		.title{
			margin-bottom: 8px;
		}
		.input {
			padding: 4px 0;
			border-bottom: 1px solid #cccccc;
			position: relative;
			margin-bottom: 16rpx;
		}
		.code{
			position: absolute;
			right: 0;
			top:12px;
			color:#1F4A99;
		}
		
	}
		
	.login-btn {
		margin: 56rpx ;
		background-color: rgba(31, 74, 153, 1);
		color: rgba(255, 255, 255, 1);
		border-radius: 8px;
	}
</style>
