- 更新导入路径,使用 .js 后缀 -修正用户模型中的密码加密和验证逻辑 - 优化用户路由和控制器 - 重构用户服务和仓库,使用原生 mongoose 方法- 更新错误处理和日志记录
133 lines
4.3 KiB
JavaScript
133 lines
4.3 KiB
JavaScript
import { body, validationResult } from 'express-validator'
|
|
import logger from 'morgan'
|
|
import userService from '../services/userService.js'
|
|
import FetchResult from '../common/web/fetchResult.js'
|
|
import messages from '../config/messages.js'
|
|
import { HTTP_STATUS } from '../common/constant/httpStatus.js'
|
|
import { SearchQuery } from '../models/search.js'
|
|
|
|
const UserController = {
|
|
async getAllUsers(res) {
|
|
try {
|
|
const users = await userService.getUserList()
|
|
return FetchResult.formatResult(res, HTTP_STATUS.ACCEPTED, 'success', users)
|
|
} catch (err) {
|
|
return FetchResult.formatResult(res, HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Internal server error')
|
|
}
|
|
},
|
|
|
|
async findUserList(req, res) {
|
|
try {
|
|
const { page, size, sort } = req.query
|
|
const search = new SearchQuery({ page: page, size: size, sort: sort })
|
|
const result = await userService.getUserList(search)
|
|
return FetchResult.formatResult(res, HTTP_STATUS.ACCEPTED, 'success', result)
|
|
} catch (err) {
|
|
console.log(err)
|
|
|
|
return FetchResult.formatResult(res, HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Internal server error')
|
|
}
|
|
},
|
|
|
|
createUser: [
|
|
body('account').isLength({ min: 3 }).withMessage('Account must be at least 3 characters long'),
|
|
body('account').isEmpty().withMessage('Account is required'),
|
|
body('password').isLength({ min: 6 }).withMessage('Password must be at least 6 characters long'),
|
|
body('password').isEmpty().withMessage('Password is required'),
|
|
async (req, res, next) => {
|
|
const errors = validationResult(req)
|
|
if (!errors.isEmpty()) {
|
|
return FetchResult.formatResult(
|
|
res,
|
|
HTTP_STATUS.BAD_REQUEST,
|
|
errors
|
|
.array()
|
|
.map((err) => err.msg)
|
|
.join(', ')
|
|
)
|
|
}
|
|
|
|
next()
|
|
},
|
|
async (req, res) => {
|
|
try {
|
|
const user = req.body
|
|
await userService.createUser(user)
|
|
return FetchResult.formatResult(res, HTTP_STATUS.CREATED, messages.user.created)
|
|
} catch (err) {
|
|
logger('Error creating user: ', err)
|
|
if (err.message === messages.user.alreadyExists) {
|
|
return FetchResult.formatResult(res, HTTP_STATUS.CONFLICT, messages.user.alreadyExists)
|
|
}
|
|
return FetchResult.formatResult(res, HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Internal server error')
|
|
}
|
|
}
|
|
],
|
|
|
|
login: [
|
|
body('account').notEmpty().withMessage('Account is required'),
|
|
body('password').notEmpty().withMessage('Password is required'),
|
|
async (req, res, next) => {
|
|
const errors = validationResult(req)
|
|
if (!errors.isEmpty()) {
|
|
console.log(errors)
|
|
return FetchResult.formatResult(
|
|
res,
|
|
400,
|
|
errors
|
|
.array()
|
|
.map((err) => err.msg)
|
|
.join(', ')
|
|
)
|
|
}
|
|
|
|
next()
|
|
},
|
|
async (req, res) => {
|
|
try {
|
|
const { account, password } = req.body
|
|
req.session.user = await userService.login(account, password)
|
|
return FetchResult.formatResult(res, HTTP_STATUS.ACCEPTED, messages.user.login)
|
|
} catch (err) {
|
|
if (err.message === messages.user.not_found) {
|
|
return FetchResult.formatResult(res, HTTP_STATUS.NOT_FOUND, messages.user.accountPasswordNotMatch)
|
|
}
|
|
|
|
logger('Error logging in: ', err)
|
|
|
|
return FetchResult.formatResult(res, HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Internal server error')
|
|
}
|
|
}
|
|
],
|
|
|
|
logout: [
|
|
async (req, res) => {
|
|
try {
|
|
req.session.destroy()
|
|
return FetchResult.formatResult(res, HTTP_STATUS.OK, messages.user.logout)
|
|
} catch (err) {
|
|
logger('Error logging out: ', err)
|
|
return FetchResult.formatResult(res, HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Internal server error')
|
|
}
|
|
}
|
|
],
|
|
|
|
async getUserExists(req, res) {
|
|
try {
|
|
const { account } = req.query
|
|
|
|
const exist = await userService.getUserExists(account)
|
|
|
|
if (!exist) {
|
|
return FetchResult.formatResult(res, HTTP_STATUS.NOT_FOUND, messages.user.not_found)
|
|
}
|
|
return FetchResult.formatResult(res, HTTP_STATUS.ACCEPTED, messages.user.exists)
|
|
} catch (err) {
|
|
logger('Error checking user existence: ', err)
|
|
return FetchResult.formatResult(res, HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Internal server error')
|
|
}
|
|
}
|
|
}
|
|
|
|
export default UserController
|