You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
2.8 KiB
123 lines
2.8 KiB
// pages/register/index.js
|
|
import {AuthModel} from '../../models/auth.js'
|
|
let auth = new AuthModel()
|
|
|
|
Page({
|
|
|
|
/**
|
|
* 页面的初始数据
|
|
*/
|
|
data: {
|
|
disabled: false, // 是否允许点击注册
|
|
rules: [
|
|
{
|
|
name: 'name',
|
|
rules: { required: true, message: '姓名必填' },
|
|
}, { // 多个规则
|
|
name: 'mobile',
|
|
rules: [{ required: true, message: '手机号必填' }, { mobile: true, message: '手机号格式不对' }],
|
|
}, {
|
|
name: 'vcode',
|
|
rules: { required: true, message: '验证码必填' },
|
|
},
|
|
],
|
|
formData: {},
|
|
time: 60,
|
|
fetchCode: true,
|
|
interval: null,
|
|
btnTitle: '获取验证码'
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面加载
|
|
*/
|
|
onLoad: function (options) {
|
|
|
|
},
|
|
onHide () {
|
|
clearInterval(this.data.interval)
|
|
},
|
|
// 重新获取Code
|
|
refetchCodeDownTime () {
|
|
|
|
this.data.interval = setInterval(() => {
|
|
const time = this.data.time - 1
|
|
const title = `${time}s 重新发送`
|
|
this.setData({
|
|
fetchCode: false,
|
|
time: time,
|
|
btnTitle: title
|
|
})
|
|
if (time == 0) {
|
|
this.setData({
|
|
fetchCode: true,
|
|
time: 60,
|
|
btnTitle: '重新发送'
|
|
})
|
|
clearInterval(this.data.interval)
|
|
}
|
|
}, 1000)
|
|
},
|
|
// 调用获取 Code 接口
|
|
onGetMsgCode () {
|
|
this.selectComponent('#form').validate((valid, errors) => {
|
|
if (!valid) {
|
|
let emptyPhone = false
|
|
errors.forEach(item => {
|
|
if (item.name === 'mobile') {
|
|
emptyPhone = true
|
|
wx.showToast({
|
|
title: item.message,
|
|
icon: 'none'
|
|
})
|
|
}
|
|
})
|
|
if (!emptyPhone) {
|
|
|
|
// console.log(this.data.formData)
|
|
this.fetchMsgCodeApi()
|
|
}
|
|
}
|
|
})
|
|
},
|
|
fetchMsgCodeApi () {
|
|
auth.getMsgCode(this.data.formData.mobile, res => {
|
|
console.log(res)
|
|
this.refetchCodeDownTime()
|
|
})
|
|
},
|
|
registerApi () {
|
|
const {mobile, name, vcode} = this.data.formData
|
|
console.log(mobile,name, vcode)
|
|
auth.mobileRegister(mobile, name, vcode, res => {
|
|
console.log(res)
|
|
wx.reLaunch({
|
|
url: '/pages/home/index',
|
|
})
|
|
})
|
|
},
|
|
formInputChange (e) {
|
|
const { field } = e.currentTarget.dataset
|
|
this.setData({
|
|
[`formData.${field}`]: e.detail.value
|
|
})
|
|
},
|
|
submitClick () {
|
|
this.selectComponent('#form').validate((valid, errors) => {
|
|
console.log('valid', valid, errors)
|
|
if (!valid) {
|
|
const firstError = Object.keys(errors)
|
|
console.log(firstError)
|
|
if (firstError.length) {
|
|
const error = errors[firstError[0]].message
|
|
wx.showToast({
|
|
icon: "none",
|
|
title: error
|
|
})
|
|
}
|
|
} else {
|
|
this.registerApi()
|
|
}
|
|
})
|
|
}
|
|
})
|