refactor(user): 重构用户相关代码并统一错误信息格式
- 修改用户控制器、模型和服务中的方法命名,使用驼峰命名法 - 更新错误信息常量,使用驼峰命名法 - 优化部分代码结构,提高可读性
This commit is contained in:
parent
602c0fff4b
commit
04885717c2
@ -8,7 +8,7 @@ const { SearchQuery } = require('../models/search')
|
|||||||
|
|
||||||
exports.getAllUsers = async (res) => {
|
exports.getAllUsers = async (res) => {
|
||||||
try {
|
try {
|
||||||
const users = await userService.user_list()
|
const users = await userService.getUserList()
|
||||||
return FetchResult.formatResult(res, HTTP_STATUS.ACCEPTED, 'success', users)
|
return FetchResult.formatResult(res, HTTP_STATUS.ACCEPTED, 'success', users)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return FetchResult.formatResult(res, HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Internal server error')
|
return FetchResult.formatResult(res, HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Internal server error')
|
||||||
@ -55,8 +55,8 @@ exports.createUser = [
|
|||||||
return FetchResult.formatResult(res, HTTP_STATUS.CREATED, messages.user.created)
|
return FetchResult.formatResult(res, HTTP_STATUS.CREATED, messages.user.created)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger('Error creating user: ', err)
|
logger('Error creating user: ', err)
|
||||||
if (err.message === messages.user.already_exists) {
|
if (err.message === messages.user.alreadyExists) {
|
||||||
return FetchResult.formatResult(res, HTTP_STATUS.CONFLICT, messages.user.already_exists)
|
return FetchResult.formatResult(res, HTTP_STATUS.CONFLICT, messages.user.alreadyExists)
|
||||||
}
|
}
|
||||||
return FetchResult.formatResult(res, HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Internal server error')
|
return FetchResult.formatResult(res, HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Internal server error')
|
||||||
}
|
}
|
||||||
@ -113,7 +113,7 @@ exports.getUserExist = async (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const { account } = req.query
|
const { account } = req.query
|
||||||
|
|
||||||
const exist = await userService.get_user_exist(account)
|
const exist = await userService.getUserExists(account)
|
||||||
|
|
||||||
if (!exist) {
|
if (!exist) {
|
||||||
return FetchResult.formatResult(res, HTTP_STATUS.NOT_FOUND, messages.user.not_found)
|
return FetchResult.formatResult(res, HTTP_STATUS.NOT_FOUND, messages.user.not_found)
|
||||||
|
|||||||
@ -11,8 +11,8 @@ const UserSchema = new Schema({
|
|||||||
password: { type: String, required: true, maxlength: 100 },
|
password: { type: String, required: true, maxlength: 100 },
|
||||||
email: { type: String, maxlength: 255, index: true },
|
email: { type: String, maxlength: 255, index: true },
|
||||||
phone: { type: String, maxlength: 11 },
|
phone: { type: String, maxlength: 11 },
|
||||||
register_date: { type: Date, default: Date.now },
|
registerDate: { type: Date, default: Date.now },
|
||||||
last_login_date: { type: Date, default: Date.now },
|
lastLoginDate: { type: Date, default: Date.now },
|
||||||
status: { type: String, enum: ['active', 'inactive', 'pending'], default: 'pending' }
|
status: { type: String, enum: ['active', 'inactive', 'pending'], default: 'pending' }
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@ -21,12 +21,12 @@ exports.login = async (account, password) => {
|
|||||||
// 密码不匹配
|
// 密码不匹配
|
||||||
const isMatch = await comparePassword(password, user.password)
|
const isMatch = await comparePassword(password, user.password)
|
||||||
if (!isMatch) {
|
if (!isMatch) {
|
||||||
throw new Error(messages.user.password_incorrect)
|
throw new Error(messages.user.passwordIncorrect)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新用户的最后登录时间
|
// 更新用户的最后登录时间
|
||||||
user.last_login_date = new Date()
|
user.lastLoginDate = new Date()
|
||||||
await userMapper.updateUserByLoginDate(user.id, user.last_login_date)
|
await userMapper.updateUserByLoginDate(user.id, user.lastLoginDate)
|
||||||
|
|
||||||
return user
|
return user
|
||||||
}
|
}
|
||||||
@ -36,12 +36,12 @@ exports.login = async (account, password) => {
|
|||||||
* @param {string} account
|
* @param {string} account
|
||||||
* @returns {Promise<boolean>}
|
* @returns {Promise<boolean>}
|
||||||
*/
|
*/
|
||||||
exports.get_user_exist = async (account) => {
|
exports.getUserExists = async (account) => {
|
||||||
return userMapper.selectUserByAccountExist(account)
|
return userMapper.selectUserByAccountExist(account)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removed the unnecessary try-catch block and simplified the function
|
// Removed the unnecessary try-catch block and simplified the function
|
||||||
exports.user_list = async () => {
|
exports.getAllUser = async () => {
|
||||||
return await userMapper.selectAllUser()
|
return await userMapper.selectAllUser()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,7 +63,7 @@ exports.createUser = async (user) => {
|
|||||||
// 检查用户是否已存在
|
// 检查用户是否已存在
|
||||||
const existingUser = await userMapper.selectUserByAccount(account)
|
const existingUser = await userMapper.selectUserByAccount(account)
|
||||||
if (existingUser) {
|
if (existingUser) {
|
||||||
return new Error(messages.user.already_exists)
|
return new Error(messages.user.alreadyExists)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建新用户
|
// 创建新用户
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user