157 lines
4.3 KiB
TypeScript
157 lines
4.3 KiB
TypeScript
/**
|
||
* 用户相关 Mock
|
||
*
|
||
* 认证接口(登录、验证码登录、发送验证码)已接入真实后端 192.168.0.55:8080,
|
||
* 通过 .umirc.ts 的 proxy 代理转发,不再 mock。
|
||
*
|
||
* 以下仅保留尚未对接真实接口的 mock。
|
||
*/
|
||
|
||
const mockUsers = [
|
||
{
|
||
userId: 1,
|
||
username: 'admin',
|
||
realName: '管理员',
|
||
phone: '13800138000',
|
||
avatar: null,
|
||
userType: 1,
|
||
password: '123456',
|
||
},
|
||
{
|
||
userId: 2,
|
||
username: 'teacher001',
|
||
realName: '李老师',
|
||
phone: '13800138002',
|
||
avatar: null,
|
||
userType: 2,
|
||
password: '123456',
|
||
},
|
||
{
|
||
userId: 3,
|
||
username: 'student001',
|
||
realName: '张三',
|
||
phone: '13800138001',
|
||
avatar: null,
|
||
userType: 3,
|
||
password: '123456',
|
||
},
|
||
];
|
||
|
||
let nextUserId = 4;
|
||
|
||
const generateToken = () =>
|
||
'eyJhbGciOiJIUzI1NiJ9.mock-' + Date.now() + '-' + Math.random().toString(36).slice(2);
|
||
|
||
const toUserData = (user: (typeof mockUsers)[0], token: string) => ({
|
||
userId: user.userId,
|
||
username: user.username,
|
||
realName: user.realName,
|
||
phone: user.phone,
|
||
avatar: user.avatar,
|
||
userType: user.userType,
|
||
token,
|
||
});
|
||
|
||
export default {
|
||
// 验证重置密码验证码(暂无真实接口)
|
||
'POST /api/auth/verifyResetCode': (req: any, res: any) => {
|
||
const { phone, code } = req.body;
|
||
|
||
if (!phone || !code) {
|
||
return res.json({ code: 40000, data: null, message: '请求参数错误' });
|
||
}
|
||
|
||
if (!/^1[3-9]\d{9}$/.test(phone)) {
|
||
return res.json({ code: 40000, data: null, message: '手机号格式不正确' });
|
||
}
|
||
|
||
const user = mockUsers.find((u) => u.phone === phone);
|
||
if (!user) {
|
||
return res.json({ code: 40000, data: null, message: '该手机号未注册' });
|
||
}
|
||
|
||
if (code !== '123456') {
|
||
return res.json({ code: 40000, data: null, message: '验证码错误或已过期' });
|
||
}
|
||
|
||
console.log(`[Mock] 验证码验证通过: ${phone}`);
|
||
res.json({ code: 0, data: true, message: 'ok' });
|
||
},
|
||
|
||
// 重置密码(暂无真实接口)
|
||
'POST /api/auth/resetPassword': (req: any, res: any) => {
|
||
const { phone, code, newPassword } = req.body;
|
||
|
||
if (!phone || !code || !newPassword) {
|
||
return res.json({ code: 40000, data: null, message: '请求参数错误' });
|
||
}
|
||
|
||
if (code !== '123456') {
|
||
return res.json({ code: 40000, data: null, message: '验证码错误或已过期' });
|
||
}
|
||
|
||
const user = mockUsers.find((u) => u.phone === phone);
|
||
if (!user) {
|
||
return res.json({ code: 40000, data: null, message: '该手机号未注册' });
|
||
}
|
||
|
||
if (newPassword.length < 6 || newPassword.length > 20) {
|
||
return res.json({ code: 40000, data: null, message: '密码长度需为6-20位' });
|
||
}
|
||
|
||
user.password = newPassword;
|
||
console.log(`[Mock] 用户 ${user.username} 密码已重置`);
|
||
res.json({ code: 0, data: true, message: 'ok' });
|
||
},
|
||
|
||
// 用户注册(暂无真实接口)
|
||
'POST /api/auth/register': (req: any, res: any) => {
|
||
const { username, password, confirmPassword, realName, phone, email, userType } = req.body;
|
||
|
||
if (!username || !password || !confirmPassword || !realName || !phone) {
|
||
return res.json({ code: 40000, data: null, message: '请求参数错误' });
|
||
}
|
||
|
||
if (!/^[a-zA-Z0-9_]{4,20}$/.test(username)) {
|
||
return res.json({ code: 40000, data: null, message: '用户名只能包含字母、数字、下划线' });
|
||
}
|
||
|
||
if (password.length < 6 || password.length > 20) {
|
||
return res.json({ code: 40000, data: null, message: '密码长度需为6-20位' });
|
||
}
|
||
|
||
if (password !== confirmPassword) {
|
||
return res.json({ code: 40000, data: null, message: '两次密码不一致' });
|
||
}
|
||
|
||
if (!/^1[3-9]\d{9}$/.test(phone)) {
|
||
return res.json({ code: 40000, data: null, message: '手机号格式不正确' });
|
||
}
|
||
|
||
if (mockUsers.find((u) => u.username === username)) {
|
||
return res.json({ code: 40000, data: null, message: '用户名已存在' });
|
||
}
|
||
|
||
if (mockUsers.find((u) => u.phone === phone)) {
|
||
return res.json({ code: 40000, data: null, message: '手机号已注册' });
|
||
}
|
||
|
||
const newUser = {
|
||
userId: nextUserId++,
|
||
username,
|
||
realName,
|
||
phone,
|
||
avatar: null,
|
||
userType: userType || 3,
|
||
password,
|
||
};
|
||
mockUsers.push(newUser);
|
||
|
||
res.json({
|
||
code: 0,
|
||
data: toUserData(newUser, generateToken()),
|
||
message: 'ok',
|
||
});
|
||
},
|
||
};
|