<template>
	<view>
		<view class="login-logo">
			<u-image width="120rpx" height="120rpx" src="../../assets/img/logo.png" border-radius="20"></u-image>
			<h3>小鹏管家</h3>
			<p>合伙收益通</p>
		</view>
		<view class="login-form">
			<u-form :model="form" ref="uForm">
				<view class="login-form-row">
					<u-icon name="user-2-fill" custom-prefix="custom-icon" color="#b0b8c8" size="40"></u-icon>
					<u-line color="#d9e0ec" length="20" direction="col" margin="10px"/>
					<u-form-item label="" prop="phone"  :border-bottom="false">
						<u-input  v-model="form.phone" placeholder="请输入手机号"  />
					</u-form-item>
				</view>
				<view class="login-form-row">
					<u-icon name="message-3-fill" custom-prefix="custom-icon" color="#b0b8c8" size="40"></u-icon>
					<u-line color="#d9e0ec" length="20" direction="col" margin="10px"/>
					<u-form-item label="" prop="code"  :border-bottom="false">
						<u-input v-model="form.code"  placeholder="请输入验证码" />
					</u-form-item>
				</view>
				<view class="login-code"  @click="getCode">
					<span>获取验证码</span>
				</view>
			</u-form>
			<u-verification-code :seconds="sendMsgSecond" ref="uCode" @change="codeChange" @end="end" @start="start">
			</u-verification-code>
			<u-button class="login-btn" @click="finish">提交</u-button>
		</view>
	</view>
</template>

<script>
	import * as API from '@/apis/finance.js'
	import {
		checkPhone
	} from '@/utils'
	export default {
		data() {
			return {
				form: {
					name: '',
					intro: '',
				
					phone: '',
					code: '',
				},
				backUrl: "",
				message: "",
				codeTips: '',
				isSendMsgIng: false,
				sendMsgSecond: 60 * 2,
				customStyle: {
					background: '#1677ff'
				}
			}
		},
		onLoad(op) {
		
			this.message = op.message;
			this.backUrl = op.back;
			if (op.phone) {
				this.form.phone = op.phone;
			}
		
		},
		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,
				})
				var headImg = "";
				var userInfo = this.carhelp.get("xpgj_wx_user_info")
				if (userInfo) {
		
					headImg = userInfo.headimgurl;
				}
		
		
				API.validateCode({
					verifyCode: this.form.code,
					telephone: this.form.phone,
					openId: this.carhelp.getOpenId(),
					headImg: headImg
				}).then((response) => {
					this.loginset(response)
		
				}).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 (false && checkPhoneResult !== true) {
					uni.showToast({
						title: checkPhoneResult,
		
					})
					return;
				}
				this.$refs.uCode.start();
			},
			loginset(response){
				var token = response ? response.data.token : '';
				this.carhelp.setToken(token,"merchantUser");
				this.carhelp.setPersonInfo(response.data.regUser,"merchantUser");
				//this.gotoUrl("pages/user/index")
				uni.redirectTo({
					url: '/pagesFinance/statistics/index'
				})
			},
			query(){
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				
				API.findByOpenId({
					
					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();
						}
					}
					
				})
			}
		},
		onReady() {
			this.query()
			
		}
	}
</script>
<style>
	page{
		background: url(../../assets/img/bgbg.png) no-repeat;
		background-size: 100%;
	}
</style>
<style lang="scss" scoped>
	.login-logo{
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 20%;
		h3{
			margin:15px 0;
		}
	}
	/deep/.u-form-item{
		padding: 0;
		flex: 1;
	}
	.login-form{
		width: 300px;
		margin:50px auto 0;
	}
	.login-form-row{
		height: 40px;
		border: 1px solid #c7d1e2;
		background-color: #fff;
		border-radius: 10px;
		display: flex;
		align-items: center;
		padding: 2px 10px;
		margin-bottom: 15px;
	}
	.login-code{
		text-align: right;
		span{
			color:#005AD9;
		}
	}
	.login-btn{
		background-color: #cdd7e8;
		border-color:#cdd7e8 ;
		color:#fff;
		margin-top: 30px;
	}
</style>
