GameSystemConfigController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace App\Module\Game\AdminControllers;
  3. use App\Module\Game\Enums\GameConfigGroup;
  4. use App\Module\Game\Enums\GameConfigType;
  5. use App\Module\Game\Models\GameConfig;
  6. use App\Module\Game\Repositories\GameConfigRepository;
  7. use App\Module\Game\Services\GameConfigService;
  8. use Dcat\Admin\Form;
  9. use Dcat\Admin\Grid;
  10. use Dcat\Admin\Show;
  11. use Spatie\RouteAttributes\Attributes\Post;
  12. use Spatie\RouteAttributes\Attributes\Resource;
  13. use UCore\DcatAdmin\AdminController;
  14. /**
  15. * 游戏系统配置管理控制器
  16. */
  17. #[Resource('game-system-configs', names: 'dcat.admin.game-system-configs')]
  18. class GameSystemConfigController extends AdminController
  19. {
  20. /**
  21. * 页面标题
  22. */
  23. protected $title = '游戏系统配置';
  24. /**
  25. * 页面描述
  26. */
  27. protected $description = '管理游戏系统的各种配置参数';
  28. /**
  29. * 创建数据表格
  30. */
  31. protected function grid(): Grid
  32. {
  33. return Grid::make(new GameConfigRepository(), function (Grid $grid) {
  34. $grid->column('id', 'ID')->sortable();
  35. $grid->column('group', '分组')
  36. ->display(function ($group) {
  37. return $group->getIcon() . ' ' . $group->getName();
  38. });
  39. $grid->column('key', '配置键')->copyable();
  40. $grid->column('name', '配置名称');
  41. $grid->column('type', '类型')
  42. ->display(function ($type) {
  43. return $type->getName();
  44. });
  45. $grid->column('value', '当前值')->limit(50);
  46. $grid->column('default_value', '默认值')->limit(30);
  47. $grid->column('is_enabled', '启用状态')
  48. ->display(function ($enabled) {
  49. return $enabled ? '<span class="badge badge-success">启用</span>' : '<span class="badge badge-danger">禁用</span>';
  50. });
  51. $grid->column('is_readonly', '只读')
  52. ->display(function ($readonly) {
  53. return $readonly ? '<span class="badge badge-warning">只读</span>' : '<span class="badge badge-info">可编辑</span>';
  54. });
  55. $grid->column('sort_order', '排序')->sortable();
  56. $grid->column('updated_at', '更新时间')->sortable();
  57. // 添加筛选器
  58. $grid->filter(function (Grid\Filter $filter) {
  59. // 分组筛选
  60. $filter->equal('group', '分组')
  61. ->select(GameConfigGroup::getOptions())
  62. ->placeholder('选择分组');
  63. // 配置键模糊查询
  64. $filter->like('key', '配置键')
  65. ->placeholder('输入配置键关键词');
  66. // 配置名称模糊查询
  67. $filter->like('name', '配置名称')
  68. ->placeholder('输入配置名称关键词');
  69. // 启用状态筛选
  70. $filter->equal('is_enabled', '启用状态')
  71. ->select([
  72. 1 => '启用',
  73. 0 => '禁用'
  74. ])
  75. ->placeholder('选择启用状态');
  76. // 只读状态筛选
  77. $filter->equal('is_readonly', '只读状态')
  78. ->select([
  79. 1 => '只读',
  80. 0 => '可编辑'
  81. ])
  82. ->placeholder('选择只读状态');
  83. // 配置类型筛选
  84. $filter->equal('type', '配置类型')
  85. ->select(GameConfigType::getOptions())
  86. ->placeholder('选择配置类型');
  87. // 重置按钮文本
  88. $filter->resetButton('重置筛选');
  89. $filter->submitButton('应用筛选');
  90. });
  91. // 设置默认排序
  92. $grid->model()->orderBy('group')->orderBy('sort_order');
  93. // 禁用创建和删除
  94. $grid->disableCreateButton();
  95. $grid->disableBatchDelete();
  96. // 设置每页显示数量
  97. $grid->perPage(20);
  98. // 设置分页选项
  99. $grid->perPages([10, 20, 50, 100]);
  100. });
  101. }
  102. /**
  103. * 创建详情页面
  104. */
  105. protected function detail($id): Show
  106. {
  107. return Show::make($id, new GameConfigRepository(), function (Show $show) {
  108. $show->field('id', 'ID');
  109. $show->field('key', '配置键');
  110. $show->field('name', '配置名称');
  111. $show->field('description', '配置描述');
  112. $show->field('group', '分组');
  113. $show->field('type', '类型');
  114. $show->field('value', '当前值');
  115. $show->field('default_value', '默认值');
  116. $show->field('is_enabled', '启用状态');
  117. $show->field('is_readonly', '只读');
  118. $show->field('sort_order', '排序权重');
  119. $show->field('remark', '备注说明');
  120. $show->field('created_at', '创建时间');
  121. $show->field('updated_at', '更新时间');
  122. });
  123. }
  124. /**
  125. * 创建表单
  126. */
  127. protected function form(): Form
  128. {
  129. return Form::make(new GameConfigRepository(), function (Form $form) {
  130. $form->display('id', 'ID');
  131. $form->display('key', '配置键');
  132. $form->display('name', '配置名称');
  133. $form->display('description', '配置描述');
  134. $form->display('group', '分组');
  135. $form->display('type', '类型');
  136. // 简化的值编辑
  137. $form->text('value', '配置值');
  138. $form->display('default_value', '默认值');
  139. $form->switch('is_enabled', '启用状态');
  140. $form->display('is_readonly', '只读');
  141. $form->display('sort_order', '排序权重');
  142. $form->textarea('remark', '备注说明')->rows(3);
  143. // 保存后清除缓存
  144. $form->saved(function (Form $form) {
  145. GameConfigService::clearCache($form->model()->key);
  146. });
  147. });
  148. }
  149. /**
  150. * 清除配置缓存
  151. */
  152. #[Post('admin/game-system-configs/clear-cache')]
  153. public function clearCache()
  154. {
  155. try {
  156. GameConfigService::clearCache();
  157. return response()->json([
  158. 'status' => true,
  159. 'message' => '配置缓存已清除'
  160. ]);
  161. } catch (\Exception $e) {
  162. return response()->json([
  163. 'status' => false,
  164. 'message' => '清除缓存失败:' . $e->getMessage()
  165. ]);
  166. }
  167. }
  168. /**
  169. * 重置配置为默认值
  170. */
  171. #[Post('admin/game-system-configs/reset')]
  172. public function reset()
  173. {
  174. try {
  175. $key = request('key');
  176. if (!$key) {
  177. return response()->json([
  178. 'status' => false,
  179. 'message' => '配置键不能为空'
  180. ]);
  181. }
  182. $config = GameConfig::where('key', $key)->first();
  183. if (!$config) {
  184. return response()->json([
  185. 'status' => false,
  186. 'message' => '配置项不存在'
  187. ]);
  188. }
  189. if ($config->is_readonly) {
  190. return response()->json([
  191. 'status' => false,
  192. 'message' => '该配置项为只读,不能重置'
  193. ]);
  194. }
  195. $config->resetToDefault();
  196. $config->save();
  197. // 清除缓存
  198. GameConfigService::clearCache($key);
  199. return response()->json([
  200. 'status' => true,
  201. 'message' => '配置已重置为默认值'
  202. ]);
  203. } catch (\Exception $e) {
  204. return response()->json([
  205. 'status' => false,
  206. 'message' => '重置配置失败:' . $e->getMessage()
  207. ]);
  208. }
  209. }
  210. }