<template>
	<view class="u-page">
		<u--form labelPosition="left" :model="model1" ref="form1">
			<u-form-item label="电话" prop="phone" borderBottom ref="item1">
				<u--input v-model="model1.phone" border="none" placeholder="电话"></u--input>
			</u-form-item>

			<u-form-item label="验证码" prop="code" labelWidth="80" borderBottom>
				<u--input v-model="model1.code" border="none" placeholder="请填写验证码"></u--input>
				<u-button slot="right" @tap="getCode" :text="tips" type="success" size="mini" :disabled="disabled1">
				</u-button>
			</u-form-item>
			<u-code ref="uCode" @change="codeChange" seconds="20" @start="disabled1 = true" @end="disabled1 = false">
			</u-code>
		</u--form>
		<u-button type="primary" text="提交" customStyle="margin-top: 50px" @click="submit"></u-button>
		<u-button  type="primary" @click='goregister'>手机号一键注册</u-button>
		<u-loading-page :loading="isLoading" bg-color="#e8e8e8"></u-loading-page>
	</view>
</template>

<script>
	import helper from '@/common/helper.js';
	export default {
		data() {
			return {
				isLoading: false,
				disabled1: false,
				tips: '',
				showCalendar: false,
				showBirthday: false,
				model1: {
					phone: '13333333333',
					code: '123456'
				},
				rules: {
					code: {
						type: 'string',
						required: true,
						len: 6,
						message: '请填写4位验证码',
						trigger: ['blur']
					},
				}
			}
		},
		onReady() {
			// 如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则
			this.$refs.form1.setRules(this.rules)
		},
		methods: {

			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					setTimeout(() => {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast('验证码已发送');
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},

			submit() {
				let that = this
				// 如果有错误，会在catch中返回报错信息数组，校验通过则在then中返回true
				this.$refs.form1.validate().then(res => {
					uni.$u.toast('校验通过')
					this.isLoading = true
					that.$request.baseRequest('get', '/commonUser/loginVerifyCode', {
							phone: that.model1.phone,
							verifyCode: that.model1.code
						}).then(res => {
							that.$request.TokenRequest('post', '/commonUser/api/loginQuickly', {
								mobilePhone: that.model1.phone,
								veriCode: that.model1.code
							}).then(res1 => {
								uni.setStorageSync('pcUserInfo', res1.data)
								uni.setStorageSync('userInfo', res.data)
								helper.getListByUserId()
								that.$store.commit('login', res.data)
								// that.liangxinLogin()
								uni.switchTab({
									url: '/pages/index/index'
								});
								this.isLoading = false
							})

						})
						.catch(res => {
							uni.showToast({
								title: res.message,
								icon: 'none',
								duration: 2000
							})
						});
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			reset() {
				const validateList = ['userInfo.name', 'userInfo.sex', 'radiovalue1', 'checkboxValue1', 'intro',
					'hotel', 'code', 'userInfo.birthday'
				]
				this.$refs.form1.resetFields()
				this.$refs.form1.clearValidate()
				setTimeout(() => {
					this.$refs.form1.clearValidate(validateList)
					// 或者使用 this.$refs.form1.clearValidate()
				}, 10)
			},
			hideKeyboard() {
				uni.hideKeyboard()
			},
			goregister(){
					uni.$u.route('/pages/public/register');
			},
		},
	}
</script>

<style lang="scss">

</style>
