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); + } +}