完成了登录模块
This commit is contained in:
194
pages/loginModule/register/register.js
Normal file
194
pages/loginModule/register/register.js
Normal file
@ -0,0 +1,194 @@
|
||||
import { url } from "../../../request";
|
||||
import { requestAsync } from "../../../utils/request";
|
||||
import { validate } from "../../../utils/validate";
|
||||
|
||||
// pages/loginModule/register/register.js
|
||||
Page({
|
||||
|
||||
/**
|
||||
* 页面的初始数据
|
||||
*/
|
||||
data: {
|
||||
nickname: 'cxz',
|
||||
phone: '13888610253',
|
||||
captcha: '111111',
|
||||
inviteCode: '123456',
|
||||
password: '123456',
|
||||
agree: false,
|
||||
sending: false,
|
||||
count: 60,
|
||||
},
|
||||
|
||||
sendSmsCode() {
|
||||
const { phone } = this.data;
|
||||
if (!/^1\d{10}$/.test(phone)) {
|
||||
wx.showToast({ title: '请输入有效的手机号', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
wx.request({
|
||||
url: url + '/userInfo/code',
|
||||
method: 'POST',
|
||||
header: { 'content-type': 'application/json' },
|
||||
data: {
|
||||
templateString: phone
|
||||
},
|
||||
success: res => {
|
||||
// 假设后端返回 { success: true, ... }
|
||||
if (res.data.code === 1) {
|
||||
wx.showToast({ title: '验证码已发送' });
|
||||
this.startCountdown();
|
||||
} else {
|
||||
wx.showToast({
|
||||
title: res.data.message || '发送失败',
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
startCountdown() {
|
||||
this.setData({ sending: true, count: 60 });
|
||||
const timer = setInterval(() => {
|
||||
let { count } = this.data;
|
||||
if (count <= 1) {
|
||||
clearInterval(timer);
|
||||
this.setData({ sending: false });
|
||||
} else {
|
||||
this.setData({ count: count - 1 });
|
||||
}
|
||||
}, 1000);
|
||||
},
|
||||
|
||||
// 通用输入事件处理函数
|
||||
onInput(e) {
|
||||
const field = e.currentTarget.dataset.field; // 获取字段名
|
||||
const value = e.detail.value; // 获取输入内容
|
||||
this.setData({
|
||||
[field]: value // 动态更新对应字段
|
||||
});
|
||||
},
|
||||
|
||||
// 复选框事件处理(如果使用 checkbox-group 推荐用 change)
|
||||
onCheckboxChange(e) {
|
||||
this.setData({
|
||||
agree: e.detail.value.length > 0
|
||||
});
|
||||
},
|
||||
|
||||
// 注册按钮点击
|
||||
async onRegister() {
|
||||
if (!this.data.agree) {
|
||||
wx.showToast({
|
||||
title: '请先勾选同意协议',
|
||||
icon: 'none'
|
||||
});
|
||||
return;
|
||||
}
|
||||
// 调用通用校验,失败时已提示并 return
|
||||
if (!validate(this.data, {
|
||||
agree: '请先勾选同意协议',
|
||||
nickname: '请输入昵称',
|
||||
phone: '请输入手机号',
|
||||
captcha: '请输入验证码',
|
||||
inviteCode: '请输入邀请码',
|
||||
password: '请输入密码'
|
||||
})) {
|
||||
return;
|
||||
}
|
||||
const { nickname, phone, captcha, inviteCode, password } = this.data;
|
||||
const res = await requestAsync({
|
||||
url: url + '/userInfo/register',
|
||||
method: 'POST',
|
||||
header: { 'content-type': 'application/json' },
|
||||
data: {
|
||||
nickName: nickname,
|
||||
phoneNumber: phone,
|
||||
verificationCode: captcha,
|
||||
invitationCode: inviteCode,
|
||||
userPassword: password
|
||||
}
|
||||
});
|
||||
if (res.data.code === 1) {
|
||||
wx.showToast({
|
||||
title: '注册成功',
|
||||
icon: 'success',
|
||||
duration: 1000
|
||||
});
|
||||
setTimeout(() => {
|
||||
wx.navigateTo({
|
||||
url: '/pages/loginModule/pwdLogin/pwdLogin',
|
||||
success: () => {
|
||||
this.setData({ nickname:'', phone:'', captcha:'', inviteCode:'', password:'', agree:false });
|
||||
}
|
||||
});
|
||||
}, 1000);
|
||||
} else {
|
||||
wx.showToast({
|
||||
title: res.data.message || '注册失败',
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
gotoLogin() {
|
||||
wx.navigateTo({
|
||||
url: '/pages/loginModule/pwdLogin/pwdLogin',
|
||||
})
|
||||
},
|
||||
/**
|
||||
* 生命周期函数--监听页面加载
|
||||
*/
|
||||
onLoad(options) {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面初次渲染完成
|
||||
*/
|
||||
onReady() {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面显示
|
||||
*/
|
||||
onShow() {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面隐藏
|
||||
*/
|
||||
onHide() {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面卸载
|
||||
*/
|
||||
onUnload() {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* 页面相关事件处理函数--监听用户下拉动作
|
||||
*/
|
||||
onPullDownRefresh() {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* 页面上拉触底事件的处理函数
|
||||
*/
|
||||
onReachBottom() {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* 用户点击右上角分享
|
||||
*/
|
||||
onShareAppMessage() {
|
||||
|
||||
}
|
||||
})
|
Reference in New Issue
Block a user