From 088a088b8953bd9d9d60bf0681b9475084b1db4e Mon Sep 17 00:00:00 2001 From: lizhuoran <625237490@qq.com> Date: Mon, 9 Mar 2026 10:08:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=88=9B=E5=BB=BA=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E6=9C=8D=E5=8A=A1=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 实现 getGroupedSettings 方法: 按分组获取配置 - 实现 updateSettings 方法: 批量更新配置 - 实现 clearCache 方法: 清除配置缓存 - 集成 Laravel Cache 提升性能(24小时缓存) --- app/Services/SystemSettingService.php | 77 +++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 app/Services/SystemSettingService.php diff --git a/app/Services/SystemSettingService.php b/app/Services/SystemSettingService.php new file mode 100644 index 0000000..7f64fc8 --- /dev/null +++ b/app/Services/SystemSettingService.php @@ -0,0 +1,77 @@ + 按group分组的配置数组 + */ + public function getGroupedSettings(): array + { + return Cache::remember(self::CACHE_KEY, self::CACHE_TTL, function () { + return SystemSetting::all() + ->groupBy('group') + ->map(function (Collection $settings) { + return $settings->map(function (SystemSetting $setting) { + return [ + 'id' => $setting->id, + 'key' => $setting->key, + 'value' => $setting->value, + 'description' => $setting->description, + 'is_public' => $setting->is_public, + 'updated_at' => $setting->updated_at, + ]; + })->values()->toArray(); + }) + ->toArray(); + }); + } + + /** + * 批量更新系统设置 + * + * @param array $settings 配置数组,格式为 ['key' => 'value', ...] + * @return void + */ + public function updateSettings(array $settings): void + { + foreach ($settings as $key => $value) { + SystemSetting::set($key, $value); + } + + // 清除缓存 + $this->clearCache(); + } + + /** + * 清除系统设置缓存 + * + * @return void + */ + public function clearCache(): void + { + Cache::forget(self::CACHE_KEY); + } +}