| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- <?php
- namespace App\Module\Mex\Logic;
- use App\Module\Mex\Enums\MexConfigGroup;
- use App\Module\Mex\Enums\MexConfigType;
- use App\Module\Mex\Models\MexConfig;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Log;
- /**
- * Mex配置逻辑层
- */
- class MexConfigLogic
- {
- private const CACHE_PREFIX = 'mex_config:';
- private const CACHE_TTL = 3600; // 1小时
- /**
- * 获取配置值
- *
- * @param string $key 配置键名
- * @param mixed $default 默认值
- * @return mixed
- */
- public static function get(string $key, $default = null)
- {
- $cacheKey = self::CACHE_PREFIX . $key;
- return Cache::remember($cacheKey, self::CACHE_TTL, function () use ($key, $default) {
- $config = MexConfig::where('key', $key)->first();
- if (!$config) {
- return $default;
- }
- return $config->getTypedValue();
- });
- }
- /**
- * 设置配置值
- *
- * @param string $key 配置键名
- * @param mixed $value 配置值
- * @return bool
- */
- public static function set(string $key, $value): bool
- {
- try {
- $config = MexConfig::where('key', $key)->first();
- if (!$config) {
- Log::warning("配置项不存在: {$key}");
- return false;
- }
- if ($config->is_readonly) {
- Log::warning("配置项为只读: {$key}");
- return false;
- }
- if (!$config->setTypedValue($value)) {
- Log::warning("配置值类型不匹配: {$key}", ['value' => $value, 'type' => $config->type->value]);
- return false;
- }
- $config->save();
- // 清除缓存
- self::clearCache($key);
- Log::info("配置项已更新: {$key}", ['old_value' => $config->getOriginal('value'), 'new_value' => $value]);
- return true;
- } catch (\Exception $e) {
- Log::error("设置配置失败: {$key}", ['error' => $e->getMessage()]);
- return false;
- }
- }
- /**
- * 获取布尔值配置
- *
- * @param string $key 配置键名
- * @param bool $default 默认值
- * @return bool
- */
- public static function getBool(string $key, bool $default = false): bool
- {
- return (bool) self::get($key, $default);
- }
- /**
- * 获取整数配置
- *
- * @param string $key 配置键名
- * @param int $default 默认值
- * @return int
- */
- public static function getInt(string $key, int $default = 0): int
- {
- return (int) self::get($key, $default);
- }
- /**
- * 获取浮点数配置
- *
- * @param string $key 配置键名
- * @param float $default 默认值
- * @return float
- */
- public static function getFloat(string $key, float $default = 0.0): float
- {
- return (float) self::get($key, $default);
- }
- /**
- * 获取字符串配置
- *
- * @param string $key 配置键名
- * @param string $default 默认值
- * @return string
- */
- public static function getString(string $key, string $default = ''): string
- {
- return (string) self::get($key, $default);
- }
- /**
- * 获取数组配置
- *
- * @param string $key 配置键名
- * @param array $default 默认值
- * @return array
- */
- public static function getArray(string $key, array $default = []): array
- {
- $value = self::get($key, $default);
- return is_array($value) ? $value : $default;
- }
- /**
- * 获取分组配置
- *
- * @param MexConfigGroup $group 配置分组
- * @return array
- */
- public static function getGroupConfigs(MexConfigGroup $group): array
- {
- $cacheKey = self::CACHE_PREFIX . 'group:' . $group->value;
- return Cache::remember($cacheKey, self::CACHE_TTL, function () use ($group) {
- $configs = MexConfig::byGroup($group)
- ->enabled()
- ->ordered()
- ->get();
- $result = [];
- foreach ($configs as $config) {
- $result[$config->key] = $config->getTypedValue();
- }
- return $result;
- });
- }
- /**
- * 批量设置配置
- *
- * @param array $configs 配置数组 [key => value]
- * @return array 设置结果 [key => success]
- */
- public static function setBatch(array $configs): array
- {
- $results = [];
- foreach ($configs as $key => $value) {
- $results[$key] = self::set($key, $value);
- }
- return $results;
- }
- /**
- * 重置配置为默认值
- *
- * @param string $key 配置键名
- * @return bool
- */
- public static function reset(string $key): bool
- {
- try {
- $config = MexConfig::where('key', $key)->first();
- if (!$config) {
- return false;
- }
- if ($config->is_readonly) {
- return false;
- }
- $config->resetToDefault();
- $config->save();
- // 清除缓存
- self::clearCache($key);
- Log::info("配置项已重置: {$key}");
- return true;
- } catch (\Exception $e) {
- Log::error("重置配置失败: {$key}", ['error' => $e->getMessage()]);
- return false;
- }
- }
- /**
- * 检查配置是否存在
- *
- * @param string $key 配置键名
- * @return bool
- */
- public static function exists(string $key): bool
- {
- return MexConfig::where('key', $key)->exists();
- }
- /**
- * 检查配置是否启用
- *
- * @param string $key 配置键名
- * @return bool
- */
- public static function isEnabled(string $key): bool
- {
- $config = MexConfig::where('key', $key)->first();
- return $config && $config->is_enabled;
- }
- /**
- * 启用/禁用配置
- *
- * @param string $key 配置键名
- * @param bool $enabled 是否启用
- * @return bool
- */
- public static function setEnabled(string $key, bool $enabled): bool
- {
- try {
- $config = MexConfig::where('key', $key)->first();
- if (!$config) {
- return false;
- }
- $config->is_enabled = $enabled;
- $config->save();
- // 清除缓存
- self::clearCache($key);
- Log::info("配置项状态已更新: {$key}", ['enabled' => $enabled]);
- return true;
- } catch (\Exception $e) {
- Log::error("更新配置状态失败: {$key}", ['error' => $e->getMessage()]);
- return false;
- }
- }
- /**
- * 清除配置缓存
- *
- * @param string|null $key 配置键名,为空时清除所有缓存
- */
- public static function clearCache(?string $key = null): void
- {
- if ($key) {
- Cache::forget(self::CACHE_PREFIX . $key);
- } else {
- // 清除所有配置缓存
- $keys = Cache::get(self::CACHE_PREFIX . 'all_keys', []);
- foreach ($keys as $cacheKey) {
- Cache::forget($cacheKey);
- }
- Cache::forget(self::CACHE_PREFIX . 'all_keys');
- }
- }
- /**
- * 获取所有配置(用于管理界面)
- *
- * @return \Illuminate\Support\Collection
- */
- public static function getAllConfigs()
- {
- return MexConfig::ordered()->get()->groupBy('group');
- }
- /**
- * 验证配置值
- *
- * @param string $key 配置键名
- * @param mixed $value 配置值
- * @return array
- */
- public static function validateValue(string $key, $value): array
- {
- $config = MexConfig::where('key', $key)->first();
- if (!$config) {
- return ['valid' => false, 'message' => '配置项不存在'];
- }
- if (!$config->type->validateValue($value)) {
- return ['valid' => false, 'message' => '配置值类型不匹配'];
- }
- // 如果有验证规则,进行额外验证
- if ($config->validation_rules) {
- // 这里可以扩展更复杂的验证逻辑
- // 例如:范围检查、正则表达式等
- }
- return ['valid' => true, 'message' => '验证通过'];
- }
- }
|