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

223 lines
5.6 KiB
JavaScript
Raw Normal View History

2025-05-19 09:08:33 +08:00
import { baseUrl } from "../../../request";
2025-05-17 23:17:14 +08:00
import { requestAsync } from "../../../utils/request";
import { validate } from "../../../utils/validate";
// pages/loginModule/register/register.js
Page({
2025-07-20 18:22:59 +08:00
data: {
role: '', // 用户角色
placeholder: '', // 输入框显示文本
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
2025-07-20 18:22:59 +08:00
console.log('options--->',options);
this.setData({
role: options.role
})
2025-08-07 19:56:53 +08:00
const scene = decodeURIComponent(options.scene)
if (scene !== 'undefined') {
let [key, value] = scene.split('=');
console.log(key, value)
if (value === 'manager') value = 'supervisor'
else if (value === 'supervisor') value = 'staff'
else if (value === 'staff') value = 'user'
this.setData({inviteCode: key, role: value})
}
if (this.data.role === 'supervisor') {
2025-07-20 18:22:59 +08:00
this.setData({
placeholder: '请输入学校名称'
})
} else {
this.setData({
placeholder : '请输入昵称'
})
}
},
2025-05-17 23:17:14 +08:00
/**
* 页面的初始数据
*/
data: {
2025-05-19 09:08:33 +08:00
nickname: '',
phone: '',
captcha: '',
inviteCode: '',
password: '',
2025-05-17 23:17:14 +08:00
agree: false,
sending: false,
count: 60,
},
sendSmsCode() {
2025-08-09 22:30:47 +08:00
const { phone, role } = this.data;
2025-05-19 09:08:33 +08:00
if (!phone.trim()) {
wx.showToast({ title: '请输入手机号', icon: 'none' });
return;
}
2025-05-17 23:17:14 +08:00
if (!/^1\d{10}$/.test(phone)) {
wx.showToast({ title: '请输入有效的手机号', icon: 'none' });
return;
}
wx.request({
2025-05-19 09:08:33 +08:00
url: baseUrl + '/userInfo/code/register',
2025-05-17 23:17:14 +08:00
method: 'POST',
header: { 'content-type': 'application/json' },
data: {
2025-08-09 22:30:47 +08:00
phoneNumber: phone,
userRole: role
2025-05-17 23:17:14 +08:00
},
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 21:31:35 +08:00
// 验证码开始
2025-05-17 23:17:14 +08: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
});
},
2025-06-17 18:17:05 +08:00
validatePassword(password) {
// 校验密码长度
if (password.length < 6 || password.length > 10) {
wx.showToast({
title: '密码长度应在6到10个字符之间',
icon: 'none',
duration: 1000
});
return false;
}
// 校验是否包含字母、数字和下划线
const regex = /^(?=.*[a-zA-Z])(?=.*\d)[a-zA-Z0-9]+$/;
if (!regex.test(password)) {
wx.showToast({
title: '密码必须包含字母和数字,且不包含特殊字符',
icon: 'none',
duration: 1000
});
return false;
}
return true;
},
2025-05-17 23:17:14 +08:00
// 注册按钮点击
async onRegister() {
// 调用通用校验,失败时已提示并 return
if (!validate(this.data, {
2025-06-06 13:16:14 +08:00
nickname: '昵称不能为空',
phone: '手机号不能为空',
captcha: '验证码不能为空',
inviteCode: '邀请码不能为空',
password: '密码不能为空'
2025-05-17 23:17:14 +08:00
})) {
return;
}
2025-06-17 18:17:05 +08:00
const { nickname, phone, captcha, inviteCode, password } = this.data;
if (!this.validatePassword(password)) return ;
2025-05-19 09:08:33 +08:00
// 2. 再单独校验协议勾选
if (!this.data.agree) {
wx.showToast({ title: '请先同意用户协议', icon: 'none' });
return;
}
2025-06-06 13:16:14 +08:00
wx.showLoading({
title: '加载中',
mask: true
})
2025-07-20 18:22:59 +08:00
const { role } = this.data;
2025-08-07 19:56:53 +08:00
const res = await requestAsync({
2025-05-19 09:08:33 +08:00
url: baseUrl + '/userInfo/register',
2025-05-17 23:17:14 +08:00
method: 'POST',
header: { 'content-type': 'application/json' },
data: {
nickName: nickname,
phoneNumber: phone,
verificationCode: captcha,
invitationCode: inviteCode,
2025-07-20 18:22:59 +08:00
userPassword: password,
userRole: role
2025-05-17 23:17:14 +08:00
}
});
2025-06-06 13:16:14 +08:00
console.log('注册信息---->',res.data);
2025-05-17 23:17:14 +08:00
if (res.data.code === 1) {
wx.showToast({
title: '注册成功',
icon: 'success',
2025-06-06 13:16:14 +08:00
duration: 1000,
2025-05-17 23:17:14 +08:00
});
setTimeout(() => {
2025-08-07 19:56:53 +08:00
wx.navigateTo({
url: `/pages/loginModule/pwdLogin/pwdLogin?role=${role}`,
2025-05-17 23:17:14 +08:00
success: () => {
this.setData({ nickname:'', phone:'', captcha:'', inviteCode:'', password:'', agree:false });
}
});
}, 1000);
} else {
2025-08-10 11:25:09 +08:00
console.log(res.data)
2025-05-17 23:17:14 +08:00
wx.showToast({
title: res.data.message || '注册失败',
2025-08-07 19:56:53 +08:00
icon: 'none',
duration: 1000
2025-05-17 23:17:14 +08:00
});
2025-08-07 19:56:53 +08:00
// wx.hideLoading()
2025-05-17 23:17:14 +08:00
}
},
gotoLogin() {
2025-08-04 16:27:52 +08:00
wx.navigateBack({
delta: 1
2025-05-17 23:17:14 +08:00
})
},
gotoAgreement() {
wx.navigateTo({
url: '/pages/loginModule/agreement/agreement',
})
},
gotoPolicy() {
wx.navigateTo({
url: '/pages/loginModule/privacyPolicy/privacyPolicy',
})
},
2025-05-17 23:17:14 +08:00
})