MexConfigLogic.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. namespace App\Module\Mex\Logic;
  3. use App\Module\Mex\Enums\MexConfigGroup;
  4. use App\Module\Mex\Enums\MexConfigType;
  5. use App\Module\Mex\Models\MexConfig;
  6. use Illuminate\Support\Facades\Cache;
  7. use Illuminate\Support\Facades\Log;
  8. /**
  9. * Mex配置逻辑层
  10. */
  11. class MexConfigLogic
  12. {
  13. private const CACHE_PREFIX = 'mex_config:';
  14. private const CACHE_TTL = 3600; // 1小时
  15. /**
  16. * 获取配置值
  17. *
  18. * @param string $key 配置键名
  19. * @param mixed $default 默认值
  20. * @return mixed
  21. */
  22. public static function get(string $key, $default = null)
  23. {
  24. $cacheKey = self::CACHE_PREFIX . $key;
  25. return Cache::remember($cacheKey, self::CACHE_TTL, function () use ($key, $default) {
  26. $config = MexConfig::where('key', $key)->first();
  27. if (!$config) {
  28. return $default;
  29. }
  30. return $config->getTypedValue();
  31. });
  32. }
  33. /**
  34. * 设置配置值
  35. *
  36. * @param string $key 配置键名
  37. * @param mixed $value 配置值
  38. * @return bool
  39. */
  40. public static function set(string $key, $value): bool
  41. {
  42. try {
  43. $config = MexConfig::where('key', $key)->first();
  44. if (!$config) {
  45. Log::warning("配置项不存在: {$key}");
  46. return false;
  47. }
  48. if ($config->is_readonly) {
  49. Log::warning("配置项为只读: {$key}");
  50. return false;
  51. }
  52. if (!$config->setTypedValue($value)) {
  53. Log::warning("配置值类型不匹配: {$key}", ['value' => $value, 'type' => $config->type->value]);
  54. return false;
  55. }
  56. $config->save();
  57. // 清除缓存
  58. self::clearCache($key);
  59. Log::info("配置项已更新: {$key}", ['old_value' => $config->getOriginal('value'), 'new_value' => $value]);
  60. return true;
  61. } catch (\Exception $e) {
  62. Log::error("设置配置失败: {$key}", ['error' => $e->getMessage()]);
  63. // 重新抛出异常,不隐藏错误
  64. throw $e;
  65. }
  66. }
  67. /**
  68. * 获取布尔值配置
  69. *
  70. * @param string $key 配置键名
  71. * @param bool $default 默认值
  72. * @return bool
  73. */
  74. public static function getBool(string $key, bool $default = false): bool
  75. {
  76. return (bool) self::get($key, $default);
  77. }
  78. /**
  79. * 获取整数配置
  80. *
  81. * @param string $key 配置键名
  82. * @param int $default 默认值
  83. * @return int
  84. */
  85. public static function getInt(string $key, int $default = 0): int
  86. {
  87. return (int) self::get($key, $default);
  88. }
  89. /**
  90. * 获取浮点数配置
  91. *
  92. * @param string $key 配置键名
  93. * @param float $default 默认值
  94. * @return float
  95. */
  96. public static function getFloat(string $key, float $default = 0.0): float
  97. {
  98. return (float) self::get($key, $default);
  99. }
  100. /**
  101. * 获取字符串配置
  102. *
  103. * @param string $key 配置键名
  104. * @param string $default 默认值
  105. * @return string
  106. */
  107. public static function getString(string $key, string $default = ''): string
  108. {
  109. return (string) self::get($key, $default);
  110. }
  111. /**
  112. * 获取数组配置
  113. *
  114. * @param string $key 配置键名
  115. * @param array $default 默认值
  116. * @return array
  117. */
  118. public static function getArray(string $key, array $default = []): array
  119. {
  120. $value = self::get($key, $default);
  121. return is_array($value) ? $value : $default;
  122. }
  123. /**
  124. * 获取分组配置
  125. *
  126. * @param MexConfigGroup $group 配置分组
  127. * @return array
  128. */
  129. public static function getGroupConfigs(MexConfigGroup $group): array
  130. {
  131. $cacheKey = self::CACHE_PREFIX . 'group:' . $group->value;
  132. return Cache::remember($cacheKey, self::CACHE_TTL, function () use ($group) {
  133. $configs = MexConfig::byGroup($group)
  134. ->enabled()
  135. ->ordered()
  136. ->get();
  137. $result = [];
  138. foreach ($configs as $config) {
  139. $result[$config->key] = $config->getTypedValue();
  140. }
  141. return $result;
  142. });
  143. }
  144. /**
  145. * 批量设置配置
  146. *
  147. * @param array $configs 配置数组 [key => value]
  148. * @return array 设置结果 [key => success]
  149. */
  150. public static function setBatch(array $configs): array
  151. {
  152. $results = [];
  153. foreach ($configs as $key => $value) {
  154. $results[$key] = self::set($key, $value);
  155. }
  156. return $results;
  157. }
  158. /**
  159. * 重置配置为默认值
  160. *
  161. * @param string $key 配置键名
  162. * @return bool
  163. */
  164. public static function reset(string $key): bool
  165. {
  166. try {
  167. $config = MexConfig::where('key', $key)->first();
  168. if (!$config) {
  169. return false;
  170. }
  171. if ($config->is_readonly) {
  172. return false;
  173. }
  174. $config->resetToDefault();
  175. $config->save();
  176. // 清除缓存
  177. self::clearCache($key);
  178. Log::info("配置项已重置: {$key}");
  179. return true;
  180. } catch (\Exception $e) {
  181. Log::error("重置配置失败: {$key}", ['error' => $e->getMessage()]);
  182. return false;
  183. }
  184. }
  185. /**
  186. * 检查配置是否存在
  187. *
  188. * @param string $key 配置键名
  189. * @return bool
  190. */
  191. public static function exists(string $key): bool
  192. {
  193. return MexConfig::where('key', $key)->exists();
  194. }
  195. /**
  196. * 检查配置是否启用
  197. *
  198. * @param string $key 配置键名
  199. * @return bool
  200. */
  201. public static function isEnabled(string $key): bool
  202. {
  203. $config = MexConfig::where('key', $key)->first();
  204. return $config && $config->is_enabled;
  205. }
  206. /**
  207. * 启用/禁用配置
  208. *
  209. * @param string $key 配置键名
  210. * @param bool $enabled 是否启用
  211. * @return bool
  212. */
  213. public static function setEnabled(string $key, bool $enabled): bool
  214. {
  215. try {
  216. $config = MexConfig::where('key', $key)->first();
  217. if (!$config) {
  218. return false;
  219. }
  220. $config->is_enabled = $enabled;
  221. $config->save();
  222. // 清除缓存
  223. self::clearCache($key);
  224. Log::info("配置项状态已更新: {$key}", ['enabled' => $enabled]);
  225. return true;
  226. } catch (\Exception $e) {
  227. Log::error("更新配置状态失败: {$key}", ['error' => $e->getMessage()]);
  228. return false;
  229. }
  230. }
  231. /**
  232. * 清除配置缓存
  233. *
  234. * @param string|null $key 配置键名,为空时清除所有缓存
  235. */
  236. public static function clearCache(?string $key = null): void
  237. {
  238. if ($key) {
  239. Cache::forget(self::CACHE_PREFIX . $key);
  240. } else {
  241. // 清除所有配置缓存
  242. $keys = Cache::get(self::CACHE_PREFIX . 'all_keys', []);
  243. foreach ($keys as $cacheKey) {
  244. Cache::forget($cacheKey);
  245. }
  246. Cache::forget(self::CACHE_PREFIX . 'all_keys');
  247. }
  248. }
  249. /**
  250. * 获取所有配置(用于管理界面)
  251. *
  252. * @return \Illuminate\Support\Collection
  253. */
  254. public static function getAllConfigs()
  255. {
  256. return MexConfig::ordered()->get()->groupBy('group');
  257. }
  258. /**
  259. * 验证配置值
  260. *
  261. * @param string $key 配置键名
  262. * @param mixed $value 配置值
  263. * @return array
  264. */
  265. public static function validateValue(string $key, $value): array
  266. {
  267. $config = MexConfig::where('key', $key)->first();
  268. if (!$config) {
  269. return ['valid' => false, 'message' => '配置项不存在'];
  270. }
  271. if (!$config->type->validateValue($value)) {
  272. return ['valid' => false, 'message' => '配置值类型不匹配'];
  273. }
  274. // 如果有验证规则,进行额外验证
  275. if ($config->validation_rules) {
  276. // 这里可以扩展更复杂的验证逻辑
  277. // 例如:范围检查、正则表达式等
  278. }
  279. return ['valid' => true, 'message' => '验证通过'];
  280. }
  281. }