refactor: 重写系统设置模块,新增维护模式与登录策略功能

- 移除大量无实际效果的冗余配置项
- 新增维护模式拦截逻辑,支持管理员豁免
- 重构登录失败次数限制,从配置动态读取阈值
- 新增站点Logo支持与配置管理
- 优化用户管理页面数据拉取逻辑
- 新增端口批量创建范围模式功能
- 更新依赖包版本与缓存策略
This commit is contained in:
zhang1106
2026-06-12 14:16:29 +08:00
parent 170997ef61
commit 85d6b2c633
19 changed files with 1699 additions and 1600 deletions
+48
View File
@@ -1,7 +1,14 @@
const jwt = require('jsonwebtoken');
const User = require('../models/User');
const UserRole = require('../models/UserRole');
const Role = require('../models/Role');
const SystemSetting = require('../models/SystemSetting');
const logger = require('../utils/logger').module('AuthMiddleware');
// 维护模式缓存,避免每次请求都查数据库
let maintenanceModeCache = { value: false, updatedAt: 0 };
const MAINTENANCE_CACHE_TTL = 30000; // 30秒缓存
function getJwtSecret() {
const envSecret = process.env.JWT_SECRET;
@@ -169,6 +176,41 @@ const authMiddleware = async (req, res, next) => {
});
}
// 维护模式检查:普通用户无法访问,管理员不受影响
const now = Date.now();
if (now - maintenanceModeCache.updatedAt > MAINTENANCE_CACHE_TTL) {
try {
const maintenanceSetting = await SystemSetting.findByPk('maintenance_mode');
maintenanceModeCache = {
value: maintenanceSetting ? JSON.parse(maintenanceSetting.settingValue) : false,
updatedAt: now,
};
} catch (err) {
logger.warn('读取维护模式设置失败', { error: err.message });
}
}
if (maintenanceModeCache.value) {
// 查询用户角色,判断是否为管理员
const userRole = await UserRole.findOne({
where: { UserId: user.userId },
include: [{ model: Role }],
});
const isAdmin = userRole && userRole.Role && userRole.Role.roleCode === 'admin';
if (!isAdmin) {
logger.info('维护模式:拒绝普通用户访问', {
userId: user.userId,
username: user.username,
});
return res.status(503).json({
success: false,
code: 'MAINTENANCE_MODE',
message: '系统维护中,请稍后再试',
});
}
}
req.user = decoded;
req.userModel = user;
next();
@@ -229,6 +271,11 @@ const optionalAuth = async (req, res, next) => {
}
};
/** 清除维护模式缓存,在设置更新时调用 */
const clearMaintenanceCache = () => {
maintenanceModeCache = { value: false, updatedAt: 0 };
};
module.exports = {
generateToken,
verifyToken,
@@ -236,4 +283,5 @@ module.exports = {
optionalAuth,
JWT_SECRET,
TOKEN_EXPIRY,
clearMaintenanceCache,
};