From 4829b5f3257ea3cb4c89a199a1765cb16f655491 Mon Sep 17 00:00:00 2001 From: LingandRX Date: Sun, 29 Dec 2024 14:42:40 +0800 Subject: [PATCH] =?UTF-8?q?feat(user):=20=E9=87=8D=E6=9E=84=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=B6=88=E6=81=AF=E5=A4=84=E7=90=86=EF=BC=8C=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E9=94=99=E8=AF=AF=E5=92=8C=E6=88=90=E5=8A=9F=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E6=A0=BC=E5=BC=8F=EF=BC=8C=E4=BC=98=E5=8C=96=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E7=9B=B8=E5=85=B3=E5=8A=9F=E8=83=BD=E7=9A=84=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 2 +- common/constant/httpStatus.js | 111 +++++++++++++++------------------ config/errorMessages.js | 10 --- config/message.js | 19 ++++++ config/successMessages.js | 10 --- controllers/userController.js | 37 ++++++----- repositories/userRepository.js | 1 - services/userService.js | 19 ++++-- utils/loginUtil.js | 13 ++-- 9 files changed, 116 insertions(+), 106 deletions(-) delete mode 100644 config/errorMessages.js create mode 100644 config/message.js delete mode 100644 config/successMessages.js diff --git a/app.js b/app.js index a015f67..9b487ae 100644 --- a/app.js +++ b/app.js @@ -72,7 +72,7 @@ app.use(function (err, req, res, next) { res.locals.error = req.app.get('env') === 'development' ? err : {} // render the error page - res.status(err.status || 500) + res.status(err.status || HTTP_STATUS.INTERNAL_SERVER_ERROR) res.json({ error: true, message: err.message || 'Something went wrong', diff --git a/common/constant/httpStatus.js b/common/constant/httpStatus.js index 92aeafe..3fdc6cf 100644 --- a/common/constant/httpStatus.js +++ b/common/constant/httpStatus.js @@ -3,73 +3,66 @@ */ export const HTTP_STATUS = { // 信息响应 (100–199) + /** @type {number} 继续 */ CONTINUE: 100, + /** @type {number} 协议切换 */ SWITCHING_PROTOCOLS: 101, - PROCESSING: 102, // 成功响应 (200–299) - OK: 200, // 请求成功 - CREATED: 201, // 请求成功并且服务器创建了新的资源 - ACCEPTED: 202, // 已接受 - NON_AUTHORITATIVE_INFORMATION: 203, // 非权威信息 - NO_CONTENT: 204, // 没有内容 - RESET_CONTENT: 205, // 重置内容 - PARTIAL_CONTENT: 206, // 部分内容 - MULTI_STATUS: 207, // 多状态 - ALREADY_REPORTED: 208, // 已报告 - IM_USED: 226, // 我被使用 + /** @type {number} 请求成功 */ + OK: 200, + /** @type {number} 请求成功并且服务器创建了新的资源 */ + CREATED: 201, + /** @type {number} 已接受 */ + ACCEPTED: 202, + /** @type {number} 没有内容 */ + NO_CONTENT: 204, // 重定向消息 (300–399) - MULTIPLE_CHOICES: 300, // 多种选择 - MOVED_PERMANENTLY: 301, // 永久移动 - FOUND: 302, // 查找 - SEE_OTHER: 303, // 查看其他位置 - NOT_MODIFIED: 304, // 未修改 - USE_PROXY: 305, // 使用代理 - TEMPORARY_REDIRECT: 307, // 临时重定向 - PERMANENT_REDIRECT: 308, // 永久重定向 + /** @type {number} 永久移动 */ + MOVED_PERMANENTLY: 301, + /** @type {number} 找到 */ + FOUND: 302, + /** @type {number} 查看其他位置 */ + SEE_OTHER: 303, + /** @type {number} 临时重定向 */ + TEMPORARY_REDIRECT: 307, // 客户端错误响应 (400–499) - BAD_REQUEST: 400, // 错误请求 - UNAUTHORIZED: 401, // 未授权 - PAYMENT_REQUIRED: 402, // 需要付款 - FORBIDDEN: 403, // 禁止 - NOT_FOUND: 404, // 未找到 - METHOD_NOT_ALLOWED: 405, // 方法禁用 - NOT_ACCEPTABLE: 406, // 请求不接受 - PROXY_AUTHENTICATION_REQUIRED: 407, // 需要代理认证 - REQUEST_TIMEOUT: 408, // 请求超时 - CONFLICT: 409, // 冲突 - GONE: 410, // 已删除 - LENGTH_REQUIRED: 411, // 需要长度 - PRECONDITION_FAILED: 412, // 前提条件失败 - PAYLOAD_TOO_LARGE: 413, // 负载过大 - URI_TOO_LONG: 414, // URI 过长 - UNSUPPORTED_MEDIA_TYPE: 415, // 不支持的媒体类型 - RANGE_NOT_SATISFIABLE: 416, // 请求范围不符合要求 - EXPECTATION_FAILED: 417, // 期望失败 - IM_A_TEAPOT: 418, // 我是一个茶壶 - MISDIRECTED_REQUEST: 421, // 请求错位 - UNPROCESSABLE_ENTITY: 422, // 无法处理的实体 - LOCKED: 423, // 锁定 - FAILED_DEPENDENCY: 424, // 失败依赖 - TOO_EARLY: 425, // 太早 - UPGRADE_REQUIRED: 426, // 升级所需 - PRECONDITION_REQUIRED: 428, // 必须的前提条件 - TOO_MANY_REQUESTS: 429, // 请求过多 - REQUEST_HEADER_FIELDS_TOO_LARGE: 431, // 请求头字段太大 - UNAVAILABLE_FOR_LEGAL_REASONS: 451, // 因法律原因不可用 + /** @type {number} 错误请求 */ + BAD_REQUEST: 400, + /** @type {number} 未授权 */ + UNAUTHORIZED: 401, + /** @type {number} 禁止 */ + FORBIDDEN: 403, + /** @type {number} 未找到 */ + NOT_FOUND: 404, + /** @type {number} 方法禁用 */ + METHOD_NOT_ALLOWED: 405, + /** @type {number} 不接受 */ + NOT_ACCEPTABLE: 406, + /** @type {number} 请求超时 */ + REQUEST_TIMEOUT: 408, + /** @type {number} 冲突 */ + CONFLICT: 409, + /** @type {number} 已删除 */ + GONE: 410, + /** @type {number} 负载过大 */ + PAYLOAD_TOO_LARGE: 413, + /** @type {number} URI 过长 */ + URI_TOO_LONG: 414, + /** @type {number} 不支持的媒体类型 */ + UNSUPPORTED_MEDIA_TYPE: 415, // 服务器错误响应 (500–599) - INTERNAL_SERVER_ERROR: 500, // 内部服务器错误 - NOT_IMPLEMENTED: 501, // 尚未实施 - BAD_GATEWAY: 502, // 错误网关 - SERVICE_UNAVAILABLE: 503, // 服务不可用 - GATEWAY_TIMEOUT: 504, // 网关超时 - HTTP_VERSION_NOT_SUPPORTED: 505, // HTTP 版本不受支持 - VARIANT_ALSO_NEGOTIATES: 506, // 变体也协商 - INSUFFICIENT_STORAGE: 507, // 存储不足 - LOOP_DETECTED: 508, // 检测到循环 - NOT_EXTENDED: 510, // 未扩展 - NETWORK_AUTHENTICATION_REQUIRED: 511 // 需要网络认证 + /** @type {number} 服务器错误 */ + INTERNAL_SERVER_ERROR: 500, + /** @type {number} 未实施 */ + NOT_IMPLEMENTED: 501, + /** @type {number} 错误网关 */ + BAD_GATEWAY: 502, + /** @type {number} 服务不可用 */ + SERVICE_UNAVAILABLE: 503, + /** @type {number} 网关超时 */ + GATEWAY_TIMEOUT: 504 } diff --git a/config/errorMessages.js b/config/errorMessages.js deleted file mode 100644 index 379a4a1..0000000 --- a/config/errorMessages.js +++ /dev/null @@ -1,10 +0,0 @@ -module.exports = { - user: { - not_found: 'User not found', - already_exists: 'User already exists', - account_password_not_match: 'Account and password not match', - account_not_match: 'Account not match', - password_not_match: 'Password not match', - password_incorrect: 'Password incorrect' - } -} diff --git a/config/message.js b/config/message.js new file mode 100644 index 0000000..3b7365c --- /dev/null +++ b/config/message.js @@ -0,0 +1,19 @@ +// messages.js +module.exports = { + user: { + // 成功消息 + created: 'user created', + updated: 'user updated', + deleted: 'user deleted', + login: 'login successful', + logout: 'logout successful', + + // 错误消息 + not_found: 'user not found', + already_exists: 'user already exists', + account_password_not_match: 'account and password not match', + account_not_match: 'account not match', + password_not_match: 'password not match', + password_incorrect: 'password incorrect' + } +} diff --git a/config/successMessages.js b/config/successMessages.js deleted file mode 100644 index aea8311..0000000 --- a/config/successMessages.js +++ /dev/null @@ -1,10 +0,0 @@ -module.exports = { - user: { - exist: 'User exist', - created: 'User created', - updated: 'User updated', - deleted: 'User deleted', - login: 'Login successful', - logout: 'Logout successful' - } -} diff --git a/controllers/userController.js b/controllers/userController.js index 85ced29..a03b92e 100644 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -1,15 +1,15 @@ const { body, validationResult } = require('express-validator') const userService = require('../services/userService') const FetchResult = require('../common/web/fetchResult') -const errorMessages = require('../config/errorMessages') -const successMessages = require('../config/successMessages') +const messages = require('../config/message') +const { HTTP_STATUS } = require('../common/constant/httpStatus') exports.getAllUsers = async (res) => { try { const users = await userService.user_list() return FetchResult.formatResult(res, 200, 'success', users) } catch (err) { - return FetchResult.formatResult(res, 500, 'Internal server error') + return FetchResult.formatResult(res, HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Internal server error') } } @@ -23,7 +23,7 @@ exports.createUser = [ if (!errors.isEmpty()) { return FetchResult.formatResult( res, - 400, + HTTP_STATUS.BAD_REQUEST, errors .array() .map((err) => err.msg) @@ -37,13 +37,17 @@ exports.createUser = [ try { const user = req.body await userService.create_user(user) - return FetchResult.formatResult(res, 201, successMessages.user.created) + return FetchResult.formatResult(res, HTTP_STATUS.CREATED, successMessages.user.created) } catch (err) { logger('Error creating user: ', err) - if (err.message === errorMessages.user.already_exists) { - return FetchResult.formatResult(res, 400, errorMessages.user.already_exists) + if (err.message === messages.user.already_exists) { + return FetchResult.formatResult(res, HTTP_STATUS.CONFLICT, messages.user.already_exists) } - return FetchResult.formatResult(res, 500, 'Internal server error') + return FetchResult.formatResult( + res, + HTTP_STATUS.INTERNAL_SERVER_ERROR, + 'Internal server error' + ) } } ] @@ -75,27 +79,30 @@ exports.login = [ req.session.user = user return FetchResult.formatResult(res, 200, successMessages.user.login) } catch (err) { - if (err.message === errorMessages.user.not_found) { - return FetchResult.formatResult(res, 401, errorMessages.user.account_password_not_match) + if (err.message === messages.user.not_found) { + return FetchResult.formatResult(res, 401, messages.user.account_password_not_match) } - return FetchResult.formatResult(res, 500, 'Internal server error') + return FetchResult.formatResult( + res, + HTTP_STATUS.INTERNAL_SERVER_ERROR, + 'Internal server error' + ) } } ] exports.getUserExist = async (req, res) => { - console.log(req.path) - try { const { account } = req.query const exist = await userService.get_user_exist(account) if (!exist) { - return FetchResult.formatResult(res, 404, errorMessages.user.not_found) + return FetchResult.formatResult(res, 404, messages.user.not_found) } return FetchResult.formatResult(res, 200, successMessages.user.exist) } catch (err) { - return FetchResult.formatResult(res, 500, 'Internal server error') + logger('Error checking user existence: ', err) + return FetchResult.formatResult(res, HTTP_STATUS.INTERNAL_SERVER_ERROR, 'Internal server error') } } diff --git a/repositories/userRepository.js b/repositories/userRepository.js index 65f550f..b8376d7 100644 --- a/repositories/userRepository.js +++ b/repositories/userRepository.js @@ -1,5 +1,4 @@ const UserModel = require('../models/userModel') -const { hashPassword } = require('../utils/hashUtils') exports.startTransaction = async () => { const seesion = await UserModel.startSession() diff --git a/services/userService.js b/services/userService.js index 7c95f86..d31b642 100644 --- a/services/userService.js +++ b/services/userService.js @@ -1,20 +1,25 @@ const userMapper = require('../repositories/userRepository') const logger = require('morgan') -const errorMessages = require('../config/errorMessages') +const messages = require('../config/messages') const { comparePassword } = require('../utils/hashUtils') +/** + * @param {string} account + * @param {string} password + * @returns {Promise} + */ exports.login = async (account, password) => { try { const user = await userMapper.selectUserByAccount(account) if (!user) { - throw new Error(errorMessages.user.not_found) + throw new Error(messages.user.not_found) } const isMatch = await comparePassword(password, user.password) if (!isMatch) { - throw new Error(errorMessages.user.password_incorrect) + throw new Error(messages.user.password_incorrect) } user.last_login_date = new Date() @@ -23,10 +28,16 @@ exports.login = async (account, password) => { return user } catch (err) { + logger('Error logging in: ', err) throw err } } +/** + * + * @param {string} account + * @returns {Promise} + */ exports.get_user_exist = async (account) => { try { return await userMapper.selectUserByAccountExist(account) @@ -54,7 +65,7 @@ exports.create_user = async (user) => { const check_user = await userMapper.selectUserByAccount(account) if (check_user) { - throw new Error(errorMessages.user.already_exists) + throw new Error(messages.user.already_exists) } const result = await userMapper.createUser(user) diff --git a/utils/loginUtil.js b/utils/loginUtil.js index acff66e..5b7c95e 100644 --- a/utils/loginUtil.js +++ b/utils/loginUtil.js @@ -1,8 +1,9 @@ +import FetchResult from '../common/web/fetchResult.js' + exports.authenticateSession = async (req, res, next) => { - if (req.session.user) { - next() - } else { - res.status(401).json({ error: 'Unauthorized' }) - } + if (req.session.user) { + next() + } else { + return FetchResult.formatResult(res, HTTP_STATUS.UNAUTHORIZED, 'Unauthorized') } - \ No newline at end of file +}