页面优化,功能修复
This commit is contained in:
76
app/Http/Controllers/Admin/SiteSettingController.php
Normal file
76
app/Http/Controllers/Admin/SiteSettingController.php
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\SiteSetting;
|
||||
use Illuminate\Contracts\View\View;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class SiteSettingController extends Controller
|
||||
{
|
||||
public function index(): View
|
||||
{
|
||||
$defaults = $this->defaultModules();
|
||||
$stored = SiteSetting::query()->where('setting_key', 'home_modules')->value('setting_value');
|
||||
|
||||
$modules = $defaults;
|
||||
if (is_array($stored)) {
|
||||
$storedByKey = collect($stored)->keyBy('key');
|
||||
$modules = array_map(function (array $module) use ($storedByKey): array {
|
||||
$saved = $storedByKey->get($module['key']);
|
||||
if (is_array($saved)) {
|
||||
$module['enabled'] = (bool) ($saved['enabled'] ?? true);
|
||||
$module['limit'] = (int) ($saved['limit'] ?? $module['limit']);
|
||||
}
|
||||
|
||||
return $module;
|
||||
}, $defaults);
|
||||
}
|
||||
|
||||
return view('admin.settings.index', [
|
||||
'modules' => $modules,
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request): RedirectResponse
|
||||
{
|
||||
$payload = [];
|
||||
foreach ($this->defaultModules() as $module) {
|
||||
$key = $module['key'];
|
||||
$payload[] = [
|
||||
'key' => $key,
|
||||
'label' => $module['label'],
|
||||
'enabled' => $request->boolean("modules.{$key}.enabled", true),
|
||||
'limit' => max(1, min(30, (int) $request->input("modules.{$key}.limit", $module['limit']))),
|
||||
];
|
||||
}
|
||||
|
||||
SiteSetting::query()->updateOrCreate(
|
||||
['setting_key' => 'home_modules'],
|
||||
['setting_value' => $payload],
|
||||
);
|
||||
|
||||
return redirect()
|
||||
->route('admin.settings.index')
|
||||
->with('status', '首页模块配置已更新');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, array{key:string,label:string,enabled:bool,limit:int}>
|
||||
*/
|
||||
private function defaultModules(): array
|
||||
{
|
||||
return [
|
||||
['key' => 'hot_tools', 'label' => '热门工具', 'enabled' => true, 'limit' => 18],
|
||||
['key' => 'latest_tools', 'label' => '最新收录', 'enabled' => true, 'limit' => 18],
|
||||
['key' => 'category_sections', 'label' => '分类分块', 'enabled' => true, 'limit' => 18],
|
||||
['key' => 'channel_cards', 'label' => '频道卡片区', 'enabled' => true, 'limit' => 1],
|
||||
['key' => 'promo_banners', 'label' => '横幅推荐区', 'enabled' => true, 'limit' => 1],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user