refactor: 大规模代码整理与功能优化

1. 重构后端API路由、CRUD与模块结构,整合日志管理,移除废弃demo代码
2. 优化前端组件类型定义、样式与路由配置,修复权限判断逻辑
3. 调整默认排序规则、滚动条样式与工具类函数,更新依赖与配置文件
4. 修复多处类型不匹配与默认值问题,完善表单与菜单验证逻辑
This commit is contained in:
zhangtao
2026-06-17 01:56:31 +08:00
parent 17b3cd0a4c
commit 73f2823692
500 changed files with 40763 additions and 26616 deletions
@@ -4,29 +4,27 @@ from fastapi import APIRouter, Body, Depends, Path
from fastapi.responses import JSONResponse, StreamingResponse
from redis.asyncio.client import Redis
from app.api.v1.module_system.auth.schema import AuthSchema
from app.common.response import ResponseSchema, StreamResponse, SuccessResponse
from app.core.base_params import PaginationQueryParam
from app.core.base_schema import AuthSchema, PageResultSchema
from app.core.dependencies import AuthPermission, redis_getter
from app.core.logger import log
from app.core.router_class import OperationLogRoute
from app.utils.common_util import bytes2file_response
from .schema import ParamsCreateSchema, ParamsOutSchema, ParamsQueryParam, ParamsUpdateSchema
from .service import ParamsService
ParamsRouter = APIRouter(route_class=OperationLogRoute, prefix="/param", tags=["参数管理"])
ParamsRouter = APIRouter(route_class=OperationLogRoute, prefix="/param", tags=["系统管理/参数管理"])
@ParamsRouter.get(
"/detail/{id}",
summary="获取参数详情",
description="获取参数详情",
response_model=ResponseSchema[ParamsOutSchema],
)
async def get_type_detail_controller(
id: Annotated[int, Path(description="参数ID")],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:detail"]))],
auth: Annotated[AuthSchema, Depends(AuthPermission(['module_system:param:detail']))],
) -> JSONResponse:
"""
获取参数详情
@@ -39,19 +37,17 @@ async def get_type_detail_controller(
- JSONResponse: 包含参数详情的 JSON 响应
"""
result_dict = await ParamsService.get_obj_detail_service(id=id, auth=auth)
log.info(f"获取参数详情成功 {id}")
return SuccessResponse(data=result_dict, msg="获取参数详情成功")
@ParamsRouter.get(
"/key/{config_key}",
summary="根据配置键获取参数详情",
description="根据配置键获取参数详情",
response_model=ResponseSchema[ParamsOutSchema],
)
async def get_obj_by_key_controller(
config_key: Annotated[str, Path(description="配置键")],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:query"]))],
auth: Annotated[AuthSchema, Depends(AuthPermission(['module_system:param:query']))],
) -> JSONResponse:
"""
根据配置键获取参数详情
@@ -64,19 +60,17 @@ async def get_obj_by_key_controller(
- JSONResponse: 包含参数详情的 JSON 响应
"""
result_dict = await ParamsService.get_obj_by_key_service(config_key=config_key, auth=auth)
log.info(f"根据配置键获取参数详情成功 {config_key}")
return SuccessResponse(data=result_dict, msg="根据配置键获取参数详情成功")
@ParamsRouter.get(
"/value/{config_key}",
summary="根据配置键获取参数值",
description="根据配置键获取参数值",
response_model=ResponseSchema[ParamsOutSchema],
)
async def get_config_value_by_key_controller(
config_key: Annotated[str, Path(description="配置键")],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:query"]))],
auth: Annotated[AuthSchema, Depends(AuthPermission(['module_system:param:query']))],
) -> JSONResponse:
"""
根据配置键获取参数值
@@ -91,18 +85,16 @@ async def get_config_value_by_key_controller(
result_value = await ParamsService.get_config_value_by_key_service(
config_key=config_key, auth=auth
)
log.info(f"根据配置键获取参数值成功 {config_key}")
return SuccessResponse(data=result_value, msg="根据配置键获取参数值成功")
@ParamsRouter.get(
"/list",
summary="获取参数列表",
description="获取参数列表",
response_model=ResponseSchema[list[ParamsOutSchema]],
response_model=ResponseSchema[PageResultSchema[ParamsOutSchema]],
)
async def get_obj_list_controller(
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:query"]))],
auth: Annotated[AuthSchema, Depends(AuthPermission(['module_system:param:query']))],
page: Annotated[PaginationQueryParam, Depends()],
search: Annotated[ParamsQueryParam, Depends()],
) -> JSONResponse:
@@ -124,20 +116,18 @@ async def get_obj_list_controller(
search=search,
order_by=page.order_by,
)
log.info("获取参数列表成功")
return SuccessResponse(data=result_dict, msg="查询参数列表成功")
@ParamsRouter.post(
"/create",
summary="创建参数",
description="创建参数",
response_model=ResponseSchema[ParamsOutSchema],
)
async def create_obj_controller(
data: ParamsCreateSchema,
redis: Annotated[Redis, Depends(redis_getter)],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:create"]))],
auth: Annotated[AuthSchema, Depends(AuthPermission(['module_system:param:create']))],
) -> JSONResponse:
"""
创建参数
@@ -151,21 +141,19 @@ async def create_obj_controller(
- JSONResponse: 包含创建参数结果的 JSON 响应
"""
result_dict = await ParamsService.create_obj_service(auth=auth, redis=redis, data=data)
log.info(f"创建参数成功: {result_dict}")
return SuccessResponse(data=result_dict, msg="创建参数成功")
@ParamsRouter.put(
"/update/{id}",
summary="修改参数",
description="修改参数",
response_model=ResponseSchema[ParamsOutSchema],
)
async def update_objs_controller(
data: ParamsUpdateSchema,
id: Annotated[int, Path(description="参数ID")],
redis: Annotated[Redis, Depends(redis_getter)],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:update"]))],
auth: Annotated[AuthSchema, Depends(AuthPermission(['module_system:param:update']))],
) -> JSONResponse:
"""
修改参数
@@ -180,20 +168,18 @@ async def update_objs_controller(
- JSONResponse: 包含修改参数结果的 JSON 响应
"""
result_dict = await ParamsService.update_obj_service(auth=auth, redis=redis, id=id, data=data)
log.info(f"更新参数成功 {result_dict}")
return SuccessResponse(data=result_dict, msg="更新参数成功")
@ParamsRouter.delete(
"/delete",
summary="删除参数",
description="删除参数",
response_model=ResponseSchema[ParamsOutSchema],
)
async def delete_obj_controller(
redis: Annotated[Redis, Depends(redis_getter)],
ids: Annotated[list[int], Body(description="ID列表")],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:delete"]))],
auth: Annotated[AuthSchema, Depends(AuthPermission(['module_system:param:delete']))],
) -> JSONResponse:
"""
删除参数
@@ -207,20 +193,18 @@ async def delete_obj_controller(
- JSONResponse: 包含删除参数结果的 JSON 响应
"""
await ParamsService.delete_obj_service(auth=auth, redis=redis, ids=ids)
log.info(f"删除参数成功: {ids}")
return SuccessResponse(msg="删除参数成功")
@ParamsRouter.patch(
"/status/batch",
summary="批量设置参数状态",
description="批量设置参数状态",
response_model=ResponseSchema,
)
async def batch_set_status_controller(
ids: Annotated[list[int], Body(description="参数ID列表")],
status: Annotated[str, Body(description="状态值")],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:patch"]))],
auth: Annotated[AuthSchema, Depends(AuthPermission(['module_system:param:patch']))],
) -> JSONResponse:
"""
批量设置参数状态
@@ -234,19 +218,17 @@ async def batch_set_status_controller(
- JSONResponse: 包含批量设置参数状态结果的 JSON 响应
"""
await ParamsService.batch_set_status_service(auth=auth, ids=ids, status=status)
log.info(f"批量设置参数状态成功: ids={ids}, status={status}")
return SuccessResponse(msg="批量设置参数状态成功")
@ParamsRouter.get(
"/export",
summary="导出参数",
description="导出参数列表",
response_model=ResponseSchema[None],
)
async def export_obj_list_controller(
search: Annotated[ParamsQueryParam, Depends()],
auth: Annotated[AuthSchema, Depends(AuthPermission(["module_system:param:export"]))],
auth: Annotated[AuthSchema, Depends(AuthPermission(['module_system:param:export']))],
) -> StreamingResponse:
"""
导出参数
@@ -259,8 +241,8 @@ async def export_obj_list_controller(
- StreamingResponse: 包含导出参数的 Excel 文件流响应
"""
result_dict_list = await ParamsService.get_obj_list_service(search=search, auth=auth)
export_result = await ParamsService.export_obj_service(data_list=result_dict_list)
log.info("导出参数成功")
export_data = [item.model_dump() for item in result_dict_list]
export_result = await ParamsService.export_obj_service(data_list=export_data)
return StreamResponse(
data=bytes2file_response(export_result),
@@ -272,7 +254,6 @@ async def export_obj_list_controller(
@ParamsRouter.get(
"/info",
summary="获取初始化缓存参数",
description="获取初始化缓存参数",
response_model=ResponseSchema[list[ParamsOutSchema]],
)
async def get_init_obj_controller(
@@ -288,5 +269,4 @@ async def get_init_obj_controller(
- JSONResponse: 获取初始化缓存参数的 JSON 响应
"""
result_dict = await ParamsService.get_init_config_service(redis=redis, tenant_id=1)
log.info(f"获取初始化缓存参数成功 {result_dict}")
return SuccessResponse(data=result_dict, msg="获取初始化缓存参数成功")
@@ -1,7 +1,5 @@
from collections.abc import Sequence
from app.api.v1.module_system.auth.schema import AuthSchema
from app.core.base_crud import CRUDBase
from app.core.base_schema import AuthSchema
from .model import ParamsModel
from .schema import ParamsCreateSchema, ParamsUpdateSchema
@@ -20,89 +18,4 @@ class ParamsCRUD(CRUDBase[ParamsModel, ParamsCreateSchema, ParamsUpdateSchema]):
返回:
- None
"""
self.auth = auth
super().__init__(model=ParamsModel, auth=auth)
async def get_obj_by_id_crud(self, id: int, preload: list | None = None) -> ParamsModel | None:
"""
获取配置管理型详情
参数:
- id (int): 配置管理型ID
- preload (list | None): 预加载关系,未提供时使用模型默认项
返回:
- ParamsModel | None: 配置管理型模型实例
"""
return await self.get(id=id, preload=preload)
async def get_obj_by_key_crud(
self, key: str, preload: list | None = None
) -> ParamsModel | None:
"""
根据key获取配置管理型详情
参数:
- key (str): 配置管理型key
- preload (list | None): 预加载关系,未提供时使用模型默认项
返回:
- ParamsModel | None: 配置管理型模型实例
"""
return await self.get(config_key=key, preload=preload)
async def get_obj_list_crud(
self,
search: dict | None = None,
order_by: list | None = None,
preload: list | None = None,
) -> Sequence[ParamsModel]:
"""
获取配置管理型列表
参数:
- search (dict | None): 查询参数对象。
- order_by (list | None): 排序参数列表。
- preload (list | None): 预加载关系,未提供时使用模型默认项
返回:
- Sequence[ParamsModel]: 配置管理型模型实例列表
"""
return await self.list(search=search, order_by=order_by, preload=preload)
async def create_obj_crud(self, data: ParamsCreateSchema) -> ParamsModel | None:
"""
创建配置管理型
参数:
- data (ParamsCreateSchema): 创建配置管理型负载模型
返回:
- ParamsModel | None: 配置管理型模型实例
"""
return await self.create(data=data)
async def update_obj_crud(self, id: int, data: ParamsUpdateSchema) -> ParamsModel | None:
"""
更新配置管理型
参数:
- id (int): 配置管理型ID
- data (ParamsUpdateSchema): 更新配置管理型负载模型
返回:
- ParamsModel | None: 配置管理型模型实例
"""
return await self.update(id=id, data=data)
async def delete_obj_crud(self, ids: list[int]) -> None:
"""
删除配置管理型
参数:
- ids (list[int]): 配置管理型ID列表
返回:
- None
"""
return await self.delete(ids=ids)
@@ -13,7 +13,7 @@ class ParamsCreateSchema(BaseModel):
config_key: str = Field(..., min_length=1, max_length=500, description="参数键名")
config_value: str | None = Field(default=None, max_length=500, description="参数键值")
config_type: bool = Field(default=False, description="是否系统内置")
status: str = Field(default="0", max_length=1, description="状态(0:正常 1:停用)")
status: int = Field(default=0, ge=0, le=1, description="状态(0:正常 1:停用)")
description: str | None = Field(default=None, max_length=500, description="描述")
@field_validator("config_key")
@@ -27,8 +27,8 @@ class ParamsCreateSchema(BaseModel):
@field_validator("status")
@classmethod
def _validate_status(cls, v: str) -> str:
if v not in {"0", "1"}:
def _validate_status(cls, v: int) -> int:
if v not in {0, 1}:
raise ValueError("状态仅支持 0(正常) 或 1(停用)")
return v
@@ -64,12 +64,11 @@ class ParamsQueryParam:
examples=["2025-01-01 00:00:00", "2025-12-31 23:59:59"],
),
) -> None:
# 模糊查询字段
# 模糊查询字段
self.config_name = (QueueEnum.like.value, config_name)
self.config_key = (QueueEnum.like.value, config_key)
# 精确查询字段
self.config_type = config_type
self.config_type = (QueueEnum.eq.value, config_type)
if description:
self.description = (QueueEnum.like.value, description)
@@ -1,12 +1,13 @@
import json
import time
from redis.asyncio.client import Redis
from app.api.v1.module_system.auth.schema import AuthSchema
from app.common.enums import RedisInitKeyConfig
from app.core.base_schema import AuthSchema
from app.core.database import async_db_session
from app.core.exceptions import CustomException
from app.core.logger import log
from app.core.logger import logger
from app.core.redis_crud import RedisCURD
from app.utils.excel_util import ExcelUtil
@@ -18,6 +19,10 @@ from .schema import (
ParamsUpdateSchema,
)
# 中间件系统配置内存缓存(避免每请求查 Redis)
_MID_CONFIG_TTL: float = 60.0 # 缓存 60 秒
_mid_config_cache: dict = {"ts": 0.0, "data": None}
class ParamsService:
"""
@@ -25,7 +30,7 @@ class ParamsService:
"""
@classmethod
async def get_obj_detail_service(cls, auth: AuthSchema, id: int) -> dict:
async def get_obj_detail_service(cls, auth: AuthSchema, id: int) -> ParamsOutSchema:
"""
获取配置详情
@@ -36,11 +41,13 @@ class ParamsService:
返回:
- dict: 配置管理型模型实例字典表示
"""
obj = await ParamsCRUD(auth).get_obj_by_id_crud(id=id)
return ParamsOutSchema.model_validate(obj).model_dump()
obj = await ParamsCRUD(auth).get(id=id)
if not obj:
raise CustomException(msg="参数不存在")
return ParamsOutSchema.model_validate(obj)
@classmethod
async def get_obj_by_key_service(cls, auth: AuthSchema, config_key: str) -> dict:
async def get_obj_by_key_service(cls, auth: AuthSchema, config_key: str) -> ParamsOutSchema:
"""
根据配置键获取配置详情
@@ -51,10 +58,10 @@ class ParamsService:
返回:
- Dict: 配置管理型模型实例字典表示
"""
obj = await ParamsCRUD(auth).get_obj_by_key_crud(key=config_key)
obj = await ParamsCRUD(auth).get(config_key=config_key)
if not obj:
raise CustomException(msg=f"配置键 {config_key} 不存在")
return ParamsOutSchema.model_validate(obj).model_dump()
return ParamsOutSchema.model_validate(obj)
@classmethod
async def get_config_value_by_key_service(cls, auth: AuthSchema, config_key: str) -> str | None:
@@ -68,7 +75,7 @@ class ParamsService:
返回:
- str | None: 配置值字符串或None
"""
obj = await ParamsCRUD(auth).get_obj_by_key_crud(key=config_key)
obj = await ParamsCRUD(auth).get(config_key=config_key)
if not obj:
raise CustomException(msg=f"配置键 {config_key} 不存在")
return obj.config_value
@@ -79,7 +86,7 @@ class ParamsService:
auth: AuthSchema,
search: ParamsQueryParam | None = None,
order_by: list[dict] | None = None,
) -> list[dict]:
) -> list[ParamsOutSchema]:
"""
获取配置管理型列表
@@ -89,16 +96,12 @@ class ParamsService:
- order_by (list[dict] | None): 排序参数列表
返回:
- list[dict]: 配置管理型模型实例字典列表表示
- list[ParamsOutSchema]: 配置管理型模型实例
"""
obj_list = None
if search:
obj_list = await ParamsCRUD(auth).get_obj_list_crud(
search=search.__dict__, order_by=order_by
)
else:
obj_list = await ParamsCRUD(auth).get_obj_list_crud()
return [ParamsOutSchema.model_validate(obj).model_dump() for obj in obj_list]
obj_list = await ParamsCRUD(auth).list(
search=search.__dict__ if search else {}, order_by=order_by
)
return [ParamsOutSchema.model_validate(obj) for obj in obj_list]
@classmethod
async def get_obj_page_service(
@@ -134,7 +137,7 @@ class ParamsService:
@classmethod
async def create_obj_service(
cls, auth: AuthSchema, redis: Redis, data: ParamsCreateSchema
) -> dict:
) -> ParamsOutSchema:
"""
创建配置管理型
@@ -149,33 +152,35 @@ class ParamsService:
exist_obj = await ParamsCRUD(auth).get(config_key=data.config_key)
if exist_obj:
raise CustomException(msg="创建失败,该配置key已存在")
obj = await ParamsCRUD(auth).create_obj_crud(data=data)
obj = await ParamsCRUD(auth).create(data=data)
new_obj_dict = ParamsOutSchema.model_validate(obj).model_dump()
out = ParamsOutSchema.model_validate(obj)
# 同步redis
redis_key = (
f"{RedisInitKeyConfig.SYSTEM_CONFIG.key}:{auth.user.tenant_id}:{data.config_key}"
)
try:
redis_payload = out.model_dump(mode="json")
value = json.dumps(redis_payload, ensure_ascii=False)
result = await RedisCURD(redis).set(
key=redis_key,
value="",
value=value,
expire=None,
)
if not result:
log.error(f"同步配置到缓存失败: {new_obj_dict}")
logger.error(f"同步配置到缓存失败: {out}")
raise CustomException(msg="同步配置到缓存失败")
except Exception as e:
log.error(f"创建字典类型失败: {e}")
logger.error(f"创建字典类型失败: {e}")
raise CustomException(msg=f"创建字典类型失败 {e}")
return new_obj_dict
return out
@classmethod
async def update_obj_service(
cls, auth: AuthSchema, redis: Redis, id: int, data: ParamsUpdateSchema
) -> dict:
) -> ParamsOutSchema:
"""
更新配置管理型
@@ -188,17 +193,16 @@ class ParamsService:
返回:
- Dict: 更新后的配置管理型模型实例字典表示
"""
exist_obj = await ParamsCRUD(auth).get_obj_by_id_crud(id=id)
exist_obj = await ParamsCRUD(auth).get(id=id)
if not exist_obj:
raise CustomException(msg="更新失败,该数系统配置不存在")
if exist_obj.config_key != data.config_key:
raise CustomException(msg="更新失败,系统配置key不允许修改")
new_obj = await ParamsCRUD(auth).update_obj_crud(id=id, data=data)
new_obj = await ParamsCRUD(auth).update(id=id, data=data)
if not new_obj:
raise CustomException(msg="更新失败,系统配置不存在")
out = ParamsOutSchema.model_validate(new_obj)
new_obj_dict = out.model_dump()
redis_payload = out.model_dump(mode="json")
# 同步redis
@@ -213,13 +217,13 @@ class ParamsService:
expire=None,
)
if not result:
log.error(f"同步配置到缓存失败: {new_obj_dict}")
logger.error(f"同步配置到缓存失败: {out}")
raise CustomException(msg="同步配置到缓存失败")
except Exception as e:
log.error(f"更新系统配置失败: {e}")
logger.error(f"更新系统配置失败: {e}")
raise CustomException(msg="更新系统配置失败")
return new_obj_dict
return out
@classmethod
async def delete_obj_service(cls, auth: AuthSchema, redis: Redis, ids: list[int]) -> None:
@@ -236,30 +240,27 @@ class ParamsService:
"""
if len(ids) < 1:
raise CustomException(msg="删除失败,删除对象不能为空")
for id in ids:
exist_obj = await ParamsCRUD(auth).get_obj_by_id_crud(id=id)
if not exist_obj:
# 批量校验参数存在性
objs = await ParamsCRUD(auth).list(search={"id": ("in", ids)})
obj_map = {o.id: o for o in objs}
for pid in ids:
obj = obj_map.get(pid)
if not obj:
raise CustomException(msg="删除失败,该数据字典类型不存在")
# 检查是否是否初始化类型
if exist_obj.config_type:
# 如果有字典数据,不能删除
if obj.config_type:
raise CustomException(
msg=f"{exist_obj.config_name} 删除失败,系统初始化配置不可以删除"
msg=f"{obj.config_name} 删除失败,系统初始化配置不可以删除"
)
await ParamsCRUD(auth).delete_obj_crud(ids=ids)
await ParamsCRUD(auth).delete(ids=ids)
# 同步删除Redis缓存
for id in ids:
exist_obj = await ParamsCRUD(auth).get_obj_by_id_crud(id=id)
if not exist_obj:
continue
redis_key = f"{RedisInitKeyConfig.SYSTEM_CONFIG.key}:{auth.user.tenant_id}:{exist_obj.config_key}"
# 同步删除Redis缓存(使用删除前已获取的对象信息)
for obj in objs:
redis_key = f"{RedisInitKeyConfig.SYSTEM_CONFIG.key}:{auth.user.tenant_id}:{obj.config_key}"
try:
await RedisCURD(redis).delete(redis_key)
log.info(f"删除系统配置成功: {id}")
except Exception as e:
log.error(f"删除系统配置失败: {e}")
logger.error(f"删除系统配置失败: {e}")
raise CustomException(msg="删除字典类型失败")
@classmethod
@@ -277,12 +278,8 @@ class ParamsService:
"""
if not ids:
raise CustomException(msg="请选择要操作的数据")
await ParamsCRUD(auth).update_obj_crud(
ids=ids,
data={"status": status},
)
log.info(f"批量设置系统参数状态成功: ids={ids}, status={status}")
await ParamsCRUD(auth).set(ids=ids, status=status)
@classmethod
async def export_obj_service(cls, data_list: list[dict]) -> bytes:
@@ -313,11 +310,6 @@ class ParamsService:
for item in data:
# 处理状态
item["config_type"] = "" if item.get("config_type") else ""
item["creator"] = (
item.get("creator", {}).get("name", "未知")
if isinstance(item.get("creator"), dict)
else "未知"
)
return ExcelUtil.export_list2excel(list_data=data, mapping_dict=mapping_dict)
@@ -335,7 +327,7 @@ class ParamsService:
async with async_db_session() as session:
async with session.begin():
auth = AuthSchema(db=session, check_data_scope=False)
config_obj = await ParamsCRUD(auth).get_obj_list_crud()
config_obj = await ParamsCRUD(auth).list()
if not config_obj:
raise CustomException(msg="系统配置不存在")
try:
@@ -343,7 +335,6 @@ class ParamsService:
tenant_id = config.tenant_id
redis_key = f"{RedisInitKeyConfig.SYSTEM_CONFIG.key}:{tenant_id}:{config.config_key}"
out = ParamsOutSchema.model_validate(config)
config_obj_dict = out.model_dump()
redis_payload = out.model_dump(mode="json")
value = json.dumps(redis_payload, ensure_ascii=False)
result = await RedisCURD(redis).set(
@@ -352,10 +343,10 @@ class ParamsService:
expire=None,
)
if not result:
log.error(f"❌️ 初始化系统配置失败: {config_obj_dict}")
logger.error(f"❌️ 初始化系统配置失败: {redis_key}")
raise CustomException(msg="初始化系统配置失败")
except Exception as e:
log.error(f"❌️ 初始化系统配置失败: {e}")
logger.error(f"❌️ 初始化系统配置失败: {e}")
raise CustomException(msg="初始化系统配置失败")
@classmethod
@@ -382,18 +373,17 @@ class ParamsService:
new_config = json.loads(config)
configs.append(new_config)
except Exception as e:
log.error(f"解析系统配置数据失败: {e}")
logger.error(f"解析系统配置数据失败: {e}")
continue
# 如果 Redis 中没有数据,从数据库中加载并缓存
if not configs:
log.info("Redis 中没有系统配置数据,从数据库中加载")
async with async_db_session() as session:
async with session.begin():
from app.api.v1.module_system.auth.schema import AuthSchema
from app.core.base_schema import AuthSchema
auth = AuthSchema(db=session, check_data_scope=False)
config_obj = await ParamsCRUD(auth).get_obj_list_crud()
config_obj = await ParamsCRUD(auth).list()
if config_obj:
try:
for config in config_obj:
@@ -408,18 +398,17 @@ class ParamsService:
expire=None,
)
if not result:
log.error(f"❌️ 缓存系统配置失败: {config_obj_dict}")
logger.error(f"❌️ 缓存系统配置失败: {config_obj_dict}")
configs.append(config_obj_dict)
log.info(f"✅️ 已从数据库加载 {len(configs)} 条系统配置到缓存")
except Exception as e:
log.error(f"❌️ 加载系统配置失败: {e}")
logger.error(f"❌️ 加载系统配置失败: {e}")
return configs
@classmethod
async def get_system_config_for_middleware(cls, redis: Redis) -> dict:
"""
获取中间件所需的系统配置
获取中间件所需的系统配置(带 60 秒内存缓存,避免每请求查 Redis)。
参数:
- redis (Redis): Redis 客户端实例
@@ -427,6 +416,17 @@ class ParamsService:
返回:
- dict: 包含演示模式、IP白名单、API白名单和IP黑名单的配置字典
"""
now = time.monotonic()
if _mid_config_cache["data"] and now - _mid_config_cache["ts"] < _MID_CONFIG_TTL:
return _mid_config_cache["data"]
config_result = await cls._fetch_system_config_for_middleware(redis)
_mid_config_cache["data"] = config_result
_mid_config_cache["ts"] = now
return config_result
@classmethod
async def _fetch_system_config_for_middleware(cls, redis: Redis) -> dict:
# 定义需要获取的配置键
config_keys = [
f"{RedisInitKeyConfig.SYSTEM_CONFIG.key}:1:demo_enable",
@@ -456,7 +456,7 @@ class ParamsService:
else False
)
except json.JSONDecodeError:
log.error("解析演示模式配置失败")
logger.error("解析演示模式配置失败")
# 解析IP白名单配置
if config_values[1]:
@@ -465,7 +465,7 @@ class ParamsService:
# 确保是列表类型
config_result["ip_white_list"] = json.loads(ip_white_config.get("config_value", []))
except json.JSONDecodeError:
log.error("解析IP白名单配置失败")
logger.error("解析IP白名单配置失败")
# 解析IP黑名单
# 解析API路径白名单
if config_values[2]:
@@ -476,7 +476,7 @@ class ParamsService:
white_api_config.get("config_value", [])
)
except json.JSONDecodeError:
log.error("解析API白名单配置失败")
logger.error("解析API白名单配置失败")
# 解析IP黑名单
if config_values[3]:
@@ -485,5 +485,5 @@ class ParamsService:
# 确保是列表类型
config_result["ip_black_list"] = json.loads(black_ip_config.get("config_value", []))
except json.JSONDecodeError:
log.error("解析IP黑名单配置失败")
logger.error("解析IP黑名单配置失败")
return config_result