system_config.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. """
  2. 系统配置管理接口
  3. """
  4. from app.utils.timezone import beijing_now
  5. from typing import Optional, Any
  6. from fastapi import APIRouter, Depends, Query, Request
  7. from pydantic import BaseModel
  8. from sqlalchemy import or_
  9. from sqlalchemy.orm import Session
  10. from app.api.deps import get_current_admin
  11. from app.database import get_db
  12. from app.models.system_config import SystemConfig
  13. router = APIRouter()
  14. class SystemConfigCreate(BaseModel):
  15. """创建系统配置请求"""
  16. config_key: str
  17. config_value: str
  18. config_type: str = 'string'
  19. description: Optional[str] = None
  20. is_system: int = 0
  21. is_editable: int = 1
  22. class SystemConfigUpdate(BaseModel):
  23. """更新系统配置请求"""
  24. config_value: Optional[str] = None
  25. config_type: Optional[str] = None
  26. description: Optional[str] = None
  27. @router.get("")
  28. async def get_system_configs(
  29. page: int = Query(1, ge=1),
  30. page_size: int = Query(10, ge=1, le=100),
  31. keyword: Optional[str] = Query(None, description="搜索关键词(配置键、描述)"),
  32. config_type: Optional[str] = Query(None, description="配置类型过滤"),
  33. db: Session = Depends(get_db),
  34. current_admin: Any = Depends(get_current_admin),
  35. ):
  36. """获取系统配置列表(支持检索与分页),按创建时间倒序"""
  37. offset = (page - 1) * page_size
  38. query = db.query(SystemConfig).filter(SystemConfig.is_deleted == 0)
  39. # 关键字搜索:配置键或描述模糊匹配
  40. if keyword:
  41. like_pattern = f"%{keyword}%"
  42. query = query.filter(
  43. or_(
  44. SystemConfig.config_key.like(like_pattern),
  45. SystemConfig.description.like(like_pattern),
  46. )
  47. )
  48. # 配置类型过滤
  49. if config_type:
  50. query = query.filter(SystemConfig.config_type == config_type)
  51. # 按创建时间倒序
  52. query = query.order_by(SystemConfig.created_at.desc())
  53. total = query.count()
  54. configs = query.offset(offset).limit(page_size).all()
  55. return {
  56. "code": 200,
  57. "msg": "成功",
  58. "data": [
  59. {
  60. "id": config.id,
  61. "config_key": config.config_key,
  62. "config_value": config.config_value,
  63. "config_type": config.config_type,
  64. "description": config.description,
  65. "is_system": bool(config.is_system),
  66. "is_editable": bool(config.is_editable),
  67. "created_at": config.created_at.isoformat() if config.created_at else None,
  68. "updated_at": config.updated_at.isoformat() if config.updated_at else None,
  69. }
  70. for config in configs
  71. ],
  72. "total": total,
  73. "page": page,
  74. "page_size": page_size,
  75. }
  76. @router.get("/{config_id}")
  77. async def get_system_config_detail(
  78. config_id: int,
  79. db: Session = Depends(get_db),
  80. current_admin: Any = Depends(get_current_admin)
  81. ):
  82. """获取系统配置详情"""
  83. config = db.query(SystemConfig).filter(SystemConfig.id == config_id).first()
  84. if not config:
  85. return {"code": 404, "msg": "配置不存在"}
  86. return {
  87. "code": 200,
  88. "msg": "成功",
  89. "data": {
  90. "id": config.id,
  91. "config_key": config.config_key,
  92. "config_value": config.config_value,
  93. "config_type": config.config_type,
  94. "description": config.description,
  95. "is_system": bool(config.is_system),
  96. "is_editable": bool(config.is_editable),
  97. "created_at": config.created_at.isoformat() if config.created_at else None,
  98. "updated_at": config.updated_at.isoformat() if config.updated_at else None
  99. }
  100. }
  101. @router.post("")
  102. async def create_system_config(
  103. req: SystemConfigCreate,
  104. request: Request,
  105. db: Session = Depends(get_db),
  106. current_admin: Any = Depends(get_current_admin)
  107. ):
  108. """创建系统配置"""
  109. # 检查配置键是否已存在
  110. existing_config = db.query(SystemConfig).filter(
  111. SystemConfig.config_key == req.config_key,
  112. SystemConfig.is_deleted == 0
  113. ).first()
  114. if existing_config:
  115. return {"code": 400, "msg": "配置键已存在"}
  116. config = SystemConfig(
  117. config_key=req.config_key,
  118. config_value=req.config_value,
  119. config_type=req.config_type,
  120. description=req.description,
  121. is_system=req.is_system,
  122. is_editable=req.is_editable,
  123. created_at=beijing_now(),
  124. updated_at=beijing_now()
  125. )
  126. db.add(config)
  127. # 记录操作日志
  128. from app.utils.history import create_operation_log
  129. client_ip = request.client.host if request.client else None
  130. create_operation_log(
  131. db=db,
  132. admin_id=current_admin.id,
  133. module="系统配置管理",
  134. action="创建配置",
  135. description=f"创建系统配置:{req.config_key}",
  136. new_data={
  137. "config_key": req.config_key,
  138. "config_value": req.config_value,
  139. "config_type": req.config_type,
  140. "description": req.description
  141. },
  142. ip_address=client_ip
  143. )
  144. db.commit()
  145. return {"code": 200, "msg": "创建成功", "data": {"id": config.id}}
  146. @router.put("/{config_id}")
  147. async def update_system_config(
  148. config_id: int,
  149. req: SystemConfigUpdate,
  150. request: Request,
  151. db: Session = Depends(get_db),
  152. current_admin: Any = Depends(get_current_admin)
  153. ):
  154. """更新系统配置"""
  155. config = db.query(SystemConfig).filter(SystemConfig.id == config_id).first()
  156. if not config:
  157. return {"code": 404, "msg": "配置不存在"}
  158. # 检查是否可编辑
  159. if not config.is_editable:
  160. return {"code": 403, "msg": "该配置不可编辑"}
  161. # 记录更新前的状态
  162. old_data = {
  163. "config_key": config.config_key,
  164. "config_value": config.config_value,
  165. "config_type": config.config_type,
  166. "description": config.description
  167. }
  168. # 更新字段
  169. if req.config_value is not None:
  170. config.config_value = req.config_value
  171. if req.config_type is not None:
  172. config.config_type = req.config_type
  173. if req.description is not None:
  174. config.description = req.description
  175. config.updated_at = beijing_now()
  176. # 记录操作日志
  177. from app.utils.history import create_operation_log
  178. client_ip = request.client.host if request.client else None
  179. create_operation_log(
  180. db=db,
  181. admin_id=current_admin.id,
  182. module="系统配置管理",
  183. action="更新配置",
  184. description=f"更新系统配置:{config.config_key}",
  185. old_data=old_data,
  186. new_data={
  187. "config_key": config.config_key,
  188. "config_value": config.config_value,
  189. "config_type": config.config_type,
  190. "description": config.description
  191. },
  192. ip_address=client_ip
  193. )
  194. db.commit()
  195. return {"code": 200, "msg": "更新成功"}
  196. @router.put("/{config_id}/editable")
  197. async def toggle_system_config_editable(
  198. config_id: int,
  199. request: Request,
  200. db: Session = Depends(get_db),
  201. current_admin: Any = Depends(get_current_admin)
  202. ):
  203. """切换系统配置的可编辑状态"""
  204. config = db.query(SystemConfig).filter(SystemConfig.id == config_id).first()
  205. if not config:
  206. return {"code": 404, "msg": "配置不存在"}
  207. # 系统配置不能修改可编辑状态
  208. if config.is_system:
  209. return {"code": 403, "msg": "系统配置的可编辑状态不能修改"}
  210. old_editable = config.is_editable
  211. config.is_editable = 0 if config.is_editable else 1
  212. config.updated_at = beijing_now()
  213. # 记录操作日志
  214. from app.utils.history import create_operation_log
  215. client_ip = request.client.host if request.client else None
  216. create_operation_log(
  217. db=db,
  218. admin_id=current_admin.id,
  219. module="系统配置管理",
  220. action="切换配置可编辑状态",
  221. description=f"{'启用' if config.is_editable else '禁用'}配置编辑:{config.config_key}",
  222. old_data={"is_editable": bool(old_editable)},
  223. new_data={"is_editable": bool(config.is_editable)},
  224. ip_address=client_ip
  225. )
  226. db.commit()
  227. return {"code": 200, "msg": "可编辑状态更新成功", "data": {"is_editable": bool(config.is_editable)}}
  228. @router.delete("/{config_id}")
  229. async def delete_system_config(
  230. config_id: int,
  231. request: Request,
  232. db: Session = Depends(get_db),
  233. current_admin: Any = Depends(get_current_admin)
  234. ):
  235. """删除系统配置(软删除)"""
  236. config = db.query(SystemConfig).filter(
  237. SystemConfig.id == config_id,
  238. SystemConfig.is_deleted == 0
  239. ).first()
  240. if not config:
  241. return {"code": 404, "msg": "配置不存在"}
  242. # 检查是否为系统配置(系统配置不能删除)
  243. if config.is_system:
  244. return {"code": 403, "msg": "系统配置不能删除"}
  245. config.is_deleted = 1
  246. config.updated_at = beijing_now()
  247. # 记录操作日志
  248. from app.utils.history import create_operation_log
  249. client_ip = request.client.host if request.client else None
  250. create_operation_log(
  251. db=db,
  252. admin_id=current_admin.id,
  253. module="系统配置管理",
  254. action="删除配置",
  255. description=f"删除系统配置:{config.config_key}",
  256. old_data={
  257. "config_key": config.config_key,
  258. "config_value": config.config_value,
  259. "config_type": config.config_type,
  260. "description": config.description
  261. },
  262. ip_address=client_ip
  263. )
  264. db.commit()
  265. return {"code": 200, "msg": "删除成功"}