sys_config.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. # -*- coding: utf-8 -*-
  2. # @Author : YY
  3. from types import NoneType
  4. from typing import List
  5. from flask import Flask
  6. from ruoyi_common.base.signal import app_completed
  7. from ruoyi_common.utils import StringUtil
  8. from ruoyi_common.constant import Constants, UserConstants
  9. from ruoyi_common.exception import ServiceException
  10. from ruoyi_system.mapper import SysConfigMapper
  11. from ruoyi_system.domain.entity import SysConfig
  12. from ruoyi_admin.ext import redis_cache
  13. from .. import reg
  14. class SysConfigService:
  15. @classmethod
  16. def init(cls):
  17. """
  18. 初始化配置缓存
  19. """
  20. cls.loading_config_cache()
  21. @classmethod
  22. def select_config_by_id(cls, id: int) -> SysConfig|NoneType:
  23. """
  24. 根据id查询配置信息
  25. Args:
  26. id (int): 配置ID
  27. Returns:
  28. SysConfig|NoneType: 配置信息
  29. """
  30. config = SysConfig(config_id=id)
  31. eo = SysConfigMapper.select_config(config)
  32. return eo
  33. @classmethod
  34. def select_config_by_key(cls, key: str) -> str|NoneType:
  35. """
  36. 根据key查询配置值
  37. Args:
  38. key (str): 配置键
  39. Returns:
  40. str|NoneType: 配置值
  41. """
  42. config = SysConfig(config_key=key)
  43. value:bytes = redis_cache.get(cls.get_cache_key(key))
  44. if value:
  45. return value.decode("utf-8")
  46. eo = SysConfigMapper.select_config(config)
  47. if eo is None:
  48. return None
  49. redis_cache.set(cls.get_cache_key(key), eo.config_value.encode("utf-8"))
  50. return eo.config_value
  51. @classmethod
  52. def select_captcha_on_off(cls) -> bool:
  53. """
  54. 查询验证码开关
  55. Returns:
  56. bool: 验证码开关
  57. """
  58. captcha_on_off = cls.select_config_by_key("sys.account.captchaOnOff")
  59. if captcha_on_off is None:
  60. return True
  61. return StringUtil.to_bool(captcha_on_off)
  62. @classmethod
  63. def select_config_list(cls, config: SysConfig|NoneType) -> List[SysConfig]:
  64. """
  65. 查询配置列表
  66. Args:
  67. config (SysConfig|NoneType): 包含查询条件的传输对象
  68. Returns:
  69. List[SysConfig]: 配置列表
  70. """
  71. if config is not None:
  72. eos = SysConfigMapper.select_config_list(config)
  73. else:
  74. config = SysConfig()
  75. eos = SysConfigMapper.select_config_list(config)
  76. return eos
  77. @classmethod
  78. def insert_config(cls, config: SysConfig) -> bool:
  79. """
  80. 新增配置
  81. Args:
  82. config (SysConfig): 新增配置信息
  83. Returns:
  84. bool: 新增成功返回True,否则返回False
  85. """
  86. flag = SysConfigMapper.insert_config(config)
  87. if flag and flag > 0:
  88. redis_cache.set(cls.get_cache_key(config.config_key), config.config_value.encode("utf-8"))
  89. return True
  90. return False
  91. @classmethod
  92. def update_config(cls, config: SysConfig) -> bool:
  93. """
  94. 修改配置
  95. Args:
  96. config (SysConfig): 修改配置信息
  97. Returns:
  98. bool: 修改成功返回True,否则返回False
  99. """
  100. flag = SysConfigMapper.update_config(config)
  101. if flag and flag > 0:
  102. redis_cache.set(cls.get_cache_key(config.config_key), config.config_value.encode("utf-8"))
  103. return True
  104. return False
  105. @classmethod
  106. def delete_config_by_ids(cls, ids: List[int]) -> bool:
  107. """
  108. 根据id删除配置
  109. Args:
  110. ids (List[int]): 配置ID列表
  111. Raises:
  112. bool: 删除成功返回True,否则返回False
  113. """
  114. deleting_ids = []
  115. for id in ids:
  116. config = cls.select_config_by_id(id)
  117. if not config:
  118. return
  119. if UserConstants.YES == config.config_type:
  120. raise ServiceException(f"Built-in parameter【{config.config_key}】cannot be deleted")
  121. redis_cache.delete(cls.get_cache_key(config.config_key))
  122. deleting_ids.append(id)
  123. flag = SysConfigMapper.delete_configs_by_ids(deleting_ids)
  124. return True if flag and flag > 0 else False
  125. @classmethod
  126. def loading_config_cache(cls):
  127. """
  128. 加载配置缓存
  129. """
  130. configsList: List[SysConfig] = cls.select_config_list(None)
  131. for config in configsList:
  132. redis_cache.set(cls.get_cache_key(config.config_key), config.config_value)
  133. @classmethod
  134. def clear_config_cache(cls):
  135. """
  136. 清除配置缓存
  137. """
  138. keys = redis_cache.keys(Constants.SYS_CONFIG_KEY + "*")
  139. # redis-py 的 delete 需要 *names 形式的参数,不能直接传 list
  140. if keys:
  141. redis_cache.delete(*keys)
  142. @classmethod
  143. def reset_config_cache(cls):
  144. """
  145. 重置配置缓存
  146. """
  147. cls.clear_config_cache()
  148. cls.loading_config_cache()
  149. @classmethod
  150. def check_config_key_unique(cls, body: SysConfig) -> str:
  151. """
  152. 检查配置键是否唯一
  153. Args:
  154. body (SysConfig): 新增或修改配置信息
  155. Returns:
  156. str: 唯一返回UNIQUE,否则返回NOT_UNIQUE
  157. """
  158. exist = SysConfigMapper.check_config_key_unique(body.config_key)
  159. if exist and (body.config_id is None or exist.config_id != body.config_id):
  160. return UserConstants.NOT_UNIQUE
  161. return UserConstants.UNIQUE
  162. @classmethod
  163. def get_cache_key(cls, key: str) -> str:
  164. """
  165. 获取缓存key
  166. Args:
  167. key (str): 配置键
  168. Returns:
  169. str: 缓存key
  170. """
  171. return Constants.SYS_CONFIG_KEY + key
  172. @app_completed.connect_via(reg.app)
  173. def init(sender:Flask):
  174. '''
  175. 初始化操作
  176. Args:
  177. sender (Flask): 消息发送者
  178. '''
  179. with sender.app_context():
  180. SysConfigService.init()