qingcheng-xiaochengxu/pages/loginModule/register/register.js

196 lines
4.2 KiB
JavaScript
Raw Normal View History

2025-05-19 01:08:33 +00:00
import { baseUrl } from "../../../request";
2025-05-17 15:17:14 +00:00
import { requestAsync } from "../../../utils/request";
import { validate } from "../../../utils/validate";
// pages/loginModule/register/register.js
Page({
/**
* 页面的初始数据
*/
data: {
2025-05-19 01:08:33 +00:00
nickname: '',
phone: '',
captcha: '',
inviteCode: '',
password: '',
2025-05-17 15:17:14 +00:00
agree: false,
sending: false,
count: 60,
},
sendSmsCode() {
const { phone } = this.data;
2025-05-19 01:08:33 +00:00
if (!phone.trim()) {
wx.showToast({ title: '请输入手机号', icon: 'none' });
return;
}
2025-05-17 15:17:14 +00:00
if (!/^1\d{10}$/.test(phone)) {
wx.showToast({ title: '请输入有效的手机号', icon: 'none' });
return;
}
wx.request({
2025-05-19 01:08:33 +00:00
url: baseUrl + '/userInfo/code/register',
2025-05-17 15:17:14 +00:00
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'
});
}
}
});
},
2025-05-29 13:31:35 +00:00
// 验证码开始
2025-05-17 15:17:14 +00:00
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() {
// 调用通用校验,失败时已提示并 return
if (!validate(this.data, {
nickname: '请输入昵称',
phone: '请输入手机号',
captcha: '请输入验证码',
inviteCode: '请输入邀请码',
password: '请输入密码'
})) {
return;
}
2025-05-19 01:08:33 +00:00
// 2. 再单独校验协议勾选
if (!this.data.agree) {
wx.showToast({ title: '请先同意用户协议', icon: 'none' });
return;
}
2025-05-17 15:17:14 +00:00
const { nickname, phone, captcha, inviteCode, password } = this.data;
const res = await requestAsync({
2025-05-19 01:08:33 +00:00
url: baseUrl + '/userInfo/register',
2025-05-17 15:17:14 +00:00
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() {
}
})