docs: 更新项目文档,完善需求和技术细节

This commit is contained in:
ylweng
2025-09-01 02:58:34 +08:00
parent ad20888cd4
commit 40460f78d2
6 changed files with 633 additions and 30 deletions

View File

@@ -56,6 +56,113 @@ router.get('/', adminRequired, asyncHandler(async (req, res) => {
});
}));
/**
* 创建用户(管理员权限)
*/
router.post('/', adminRequired, asyncHandler(async (req, res) => {
const { username, phone, email, user_type, password, real_name, avatar_url } = req.body;
// 验证必填字段
if (!username || !phone || !email || !user_type || !password) {
return res.status(400).json({
code: 400,
message: '用户名、手机号、邮箱、用户类型和密码为必填项',
data: null
});
}
// 验证邮箱格式
if (!validator.isEmail(email)) {
return res.status(400).json({
code: 400,
message: '邮箱格式不正确',
data: null
});
}
// 验证手机号格式
if (!validator.isMobilePhone(phone, 'zh-CN')) {
return res.status(400).json({
code: 400,
message: '手机号格式不正确',
data: null
});
}
// 验证密码长度
if (password.length < 6) {
return res.status(400).json({
code: 400,
message: '密码长度不能少于6位',
data: null
});
}
// 检查用户名是否已存在
const existingUsername = await dbConnector.query(
'SELECT id FROM users WHERE username = ?',
[username]
);
if (existingUsername.length > 0) {
return res.status(409).json({
code: 409,
message: '用户名已存在',
data: null
});
}
// 检查邮箱是否已存在
const existingEmail = await dbConnector.query(
'SELECT id FROM users WHERE email = ?',
[email]
);
if (existingEmail.length > 0) {
return res.status(409).json({
code: 409,
message: '邮箱已存在',
data: null
});
}
// 检查手机号是否已存在
const existingPhone = await dbConnector.query(
'SELECT id FROM users WHERE phone = ?',
[phone]
);
if (existingPhone.length > 0) {
return res.status(409).json({
code: 409,
message: '手机号已存在',
data: null
});
}
// 加密密码
const hashedPassword = await bcrypt.hash(password, 12);
// 插入用户数据
const result = await dbConnector.query(
`INSERT INTO users (username, phone, email, user_type, password_hash, real_name, avatar_url)
VALUES (?, ?, ?, ?, ?, ?, ?)`,
[username, phone, email, user_type, hashedPassword, real_name || null, avatar_url || null]
);
// 获取新创建的用户信息
const newUser = await dbConnector.query(
'SELECT id, username, phone, email, user_type, avatar_url, real_name FROM users WHERE id = ?',
[result.insertId]
);
res.status(201).json({
code: 201,
message: '用户创建成功',
data: newUser[0]
});
}));
/**
* 获取用户详情
*/
@@ -89,7 +196,7 @@ router.get('/:id', asyncHandler(async (req, res) => {
*/
router.put('/:id', asyncHandler(async (req, res) => {
const { id } = req.params;
const { email, real_name, avatar_url } = req.body;
const { username, phone, email, user_type, real_name, avatar_url } = req.body;
// 检查用户是否存在
const existingUser = await dbConnector.query(
@@ -114,6 +221,31 @@ router.put('/:id', asyncHandler(async (req, res) => {
});
}
// 验证手机号格式
if (phone && !validator.isMobilePhone(phone, 'zh-CN')) {
return res.status(400).json({
code: 400,
message: '手机号格式不正确',
data: null
});
}
// 检查用户名是否已被其他用户使用
if (username) {
const usernameUser = await dbConnector.query(
'SELECT id FROM users WHERE username = ? AND id != ?',
[username, id]
);
if (usernameUser.length > 0) {
return res.status(409).json({
code: 409,
message: '用户名已被其他用户使用',
data: null
});
}
}
// 检查邮箱是否已被其他用户使用
if (email) {
const emailUser = await dbConnector.query(
@@ -134,11 +266,26 @@ router.put('/:id', asyncHandler(async (req, res) => {
const updateFields = [];
const updateValues = [];
if (username !== undefined) {
updateFields.push('username = ?');
updateValues.push(username);
}
if (phone !== undefined) {
updateFields.push('phone = ?');
updateValues.push(phone);
}
if (email !== undefined) {
updateFields.push('email = ?');
updateValues.push(email);
}
if (user_type !== undefined) {
updateFields.push('user_type = ?');
updateValues.push(user_type);
}
if (real_name !== undefined) {
updateFields.push('real_name = ?');
updateValues.push(real_name);