| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- namespace App\Module\Game\AdminControllers;
- use App\Module\Game\Enums\GameConfigGroup;
- use App\Module\Game\Enums\GameConfigType;
- use App\Module\Game\Models\GameConfig;
- use App\Module\Game\Repositories\GameConfigRepository;
- use App\Module\Game\Services\GameConfigService;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Spatie\RouteAttributes\Attributes\Post;
- use Spatie\RouteAttributes\Attributes\Resource;
- use UCore\DcatAdmin\AdminController;
- /**
- * 游戏系统配置管理控制器
- */
- #[Resource('game-system-configs', names: 'dcat.admin.game-system-configs')]
- class GameSystemConfigController extends AdminController
- {
- /**
- * 页面标题
- */
- protected $title = '游戏系统配置';
- /**
- * 页面描述
- */
- protected $description = '管理游戏系统的各种配置参数';
- /**
- * 创建数据表格
- */
- protected function grid(): Grid
- {
- return Grid::make(new GameConfigRepository(), function (Grid $grid) {
- $grid->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 ? '<span class="badge badge-success">启用</span>' : '<span class="badge badge-danger">禁用</span>';
- });
- $grid->column('is_readonly', '只读')
- ->display(function ($readonly) {
- return $readonly ? '<span class="badge badge-warning">只读</span>' : '<span class="badge badge-info">可编辑</span>';
- });
- $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()
- ]);
- }
- }
- }
|