- 更新所有文件的导入和导出语法,使用 ES6 模块格式 - 修改 package.json,添加 "type": "module" 配置 - 调整部分代码结构以适应 ES6 模块
127 lines
4.1 KiB
JavaScript
127 lines
4.1 KiB
JavaScript
import { body, validationResult } from 'express-validator'
|
|
import logger from 'morgan'
|
|
import userService from '../services/userService'
|
|
import FetchResult from '../common/web/fetchResult'
|
|
import messages from '../config/messages'
|
|
import { HTTP_STATUS } from '../common/constant/httpStatus'
|
|
import { SearchQuery } from '../models/search'
|
|
|
|
export async function 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')
|
|
}
|
|
}
|
|
|
|
export async function 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')
|
|
}
|
|
}
|
|
|
|
export const 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')
|
|
}
|
|
}
|
|
]
|
|
|
|
export const 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')
|
|
}
|
|
}
|
|
]
|
|
|
|
export async function logout(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')
|
|
}
|
|
}
|
|
|
|
export async function 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')
|
|
}
|
|
}
|