MexConfigLogic.php 8.2 KB

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