column('id', 'ID')->sortable(); $grid->column('group', '分组') ->display(function ($group) { return $group->getIcon() . ' ' . $group->getName(); }); $grid->column('key', '配置键')->copyable(); $grid->column('name', '配置名称'); $grid->column('type', '类型') ->display(function ($type) { return $type->getName(); }); $grid->column('value', '当前值')->limit(50); $grid->column('default_value', '默认值')->limit(30); $grid->column('is_enabled', '启用状态') ->display(function ($enabled) { return $enabled ? '启用' : '禁用'; }); $grid->column('is_readonly', '只读') ->display(function ($readonly) { return $readonly ? '只读' : '可编辑'; }); $grid->column('sort_order', '排序')->sortable(); $grid->column('updated_at', '更新时间')->sortable(); // 添加筛选器 $grid->filter(function (Grid\Filter $filter) { // 分组筛选 $filter->equal('group', '分组') ->select(GameConfigGroup::getOptions()) ->placeholder('选择分组'); // 配置键模糊查询 $filter->like('key', '配置键') ->placeholder('输入配置键关键词'); // 配置名称模糊查询 $filter->like('name', '配置名称') ->placeholder('输入配置名称关键词'); // 启用状态筛选 $filter->equal('is_enabled', '启用状态') ->select([ 1 => '启用', 0 => '禁用' ]) ->placeholder('选择启用状态'); // 只读状态筛选 $filter->equal('is_readonly', '只读状态') ->select([ 1 => '只读', 0 => '可编辑' ]) ->placeholder('选择只读状态'); // 配置类型筛选 $filter->equal('type', '配置类型') ->select(GameConfigType::getOptions()) ->placeholder('选择配置类型'); // 重置按钮文本 $filter->resetButton('重置筛选'); $filter->submitButton('应用筛选'); }); // 设置默认排序 $grid->model()->orderBy('group')->orderBy('sort_order'); // 禁用创建和删除 $grid->disableCreateButton(); $grid->disableBatchDelete(); // 设置每页显示数量 $grid->perPage(20); // 设置分页选项 $grid->perPages([10, 20, 50, 100]); }); } /** * 创建详情页面 */ protected function detail($id): Show { return Show::make($id, new GameConfigRepository(), function (Show $show) { $show->field('id', 'ID'); $show->field('key', '配置键'); $show->field('name', '配置名称'); $show->field('description', '配置描述'); $show->field('group', '分组'); $show->field('type', '类型'); $show->field('value', '当前值'); $show->field('default_value', '默认值'); $show->field('is_enabled', '启用状态'); $show->field('is_readonly', '只读'); $show->field('sort_order', '排序权重'); $show->field('remark', '备注说明'); $show->field('created_at', '创建时间'); $show->field('updated_at', '更新时间'); }); } /** * 创建表单 */ protected function form(): Form { return Form::make(new GameConfigRepository(), function (Form $form) { $form->display('id', 'ID'); $form->display('key', '配置键'); $form->display('name', '配置名称'); $form->display('description', '配置描述'); $form->display('group', '分组'); $form->display('type', '类型'); // 简化的值编辑 $form->text('value', '配置值'); $form->display('default_value', '默认值'); $form->switch('is_enabled', '启用状态'); $form->display('is_readonly', '只读'); $form->display('sort_order', '排序权重'); $form->textarea('remark', '备注说明')->rows(3); // 保存后清除缓存 $form->saved(function (Form $form) { GameConfigService::clearCache($form->model()->key); }); }); } /** * 清除配置缓存 */ #[Post('admin/game-system-configs/clear-cache')] public function clearCache() { try { GameConfigService::clearCache(); return response()->json([ 'status' => true, 'message' => '配置缓存已清除' ]); } catch (\Exception $e) { return response()->json([ 'status' => false, 'message' => '清除缓存失败:' . $e->getMessage() ]); } } /** * 重置配置为默认值 */ #[Post('admin/game-system-configs/reset')] public function reset() { try { $key = request('key'); if (!$key) { return response()->json([ 'status' => false, 'message' => '配置键不能为空' ]); } $config = GameConfig::where('key', $key)->first(); if (!$config) { return response()->json([ 'status' => false, 'message' => '配置项不存在' ]); } if ($config->is_readonly) { return response()->json([ 'status' => false, 'message' => '该配置项为只读,不能重置' ]); } $config->resetToDefault(); $config->save(); // 清除缓存 GameConfigService::clearCache($key); return response()->json([ 'status' => true, 'message' => '配置已重置为默认值' ]); } catch (\Exception $e) { return response()->json([ 'status' => false, 'message' => '重置配置失败:' . $e->getMessage() ]); } } }