feat: 创建系统设置服务类
- 实现 getGroupedSettings 方法: 按分组获取配置 - 实现 updateSettings 方法: 批量更新配置 - 实现 clearCache 方法: 清除配置缓存 - 集成 Laravel Cache 提升性能(24小时缓存)
This commit is contained in:
77
app/Services/SystemSettingService.php
Normal file
77
app/Services/SystemSettingService.php
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
use App\Models\SystemSetting;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 系统设置服务类
|
||||||
|
*
|
||||||
|
* 提供系统设置的业务逻辑处理
|
||||||
|
*/
|
||||||
|
class SystemSettingService
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 缓存键名
|
||||||
|
*/
|
||||||
|
private const CACHE_KEY = 'system_settings_grouped';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 缓存时间(秒)- 24小时
|
||||||
|
*/
|
||||||
|
private const CACHE_TTL = 86400;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取按分组组织的所有系统设置
|
||||||
|
*
|
||||||
|
* @return array<string, array> 按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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user