MexConfigController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. namespace App\Module\Mex\AdminControllers;
  3. use App\Module\Mex\AdminControllers\Helper\GridHelper;
  4. use App\Module\Mex\Enums\MexConfigGroup;
  5. use App\Module\Mex\Enums\MexConfigType;
  6. use App\Module\Mex\Repositories\MexConfigRepository;
  7. use App\Module\Mex\Service\MexConfigService;
  8. use Spatie\RouteAttributes\Attributes\Get;
  9. use Spatie\RouteAttributes\Attributes\Post;
  10. use Spatie\RouteAttributes\Attributes\Resource;
  11. use UCore\DcatAdmin\AdminController;
  12. use Dcat\Admin\Grid;
  13. use Dcat\Admin\Show;
  14. use Dcat\Admin\Form;
  15. use Dcat\Admin\Layout\Content;
  16. /**
  17. * Mex配置管理控制器
  18. *
  19. * 路由:/admin/mex-configs
  20. */
  21. #[Resource('mex-configs', names: 'dcat.admin.mex-configs')]
  22. class MexConfigController extends AdminController
  23. {
  24. /**
  25. * 数据表格
  26. */
  27. protected function grid()
  28. {
  29. return Grid::make(new MexConfigRepository(), function (Grid $grid) {
  30. $grid->column('id', 'ID')->sortable();
  31. $grid->column('key', '配置键名')->copyable();
  32. $grid->column('name', '配置名称');
  33. $grid->column('group', '分组')->display(function ($value) {
  34. return $value instanceof MexConfigGroup ? $value->getLabel() : MexConfigGroup::from($value)->getLabel();
  35. })->label();
  36. $grid->column('type', '类型')->display(function ($value) {
  37. return $value instanceof MexConfigType ? $value->getLabel() : MexConfigType::from($value)->getLabel();
  38. })->label('info');
  39. $grid->column('value', '当前值')->limit(50);
  40. $grid->column('is_enabled', '状态')->switch();
  41. $grid->column('is_readonly', '只读')->display(function ($value) {
  42. return $value ? '是' : '否';
  43. });
  44. $grid->column('sort_order', '排序')->editable();
  45. $grid->column('updated_at', '更新时间');
  46. // 筛选器
  47. $grid->filter(function (Grid\Filter $filter) {
  48. $filter->equal('group', '分组')->select(MexConfigGroup::getOptions());
  49. $filter->equal('type', '类型')->select(MexConfigType::getOptions());
  50. $filter->equal('is_enabled', '状态')->select([1 => '启用', 0 => '禁用']);
  51. $filter->equal('is_readonly', '只读')->select([1 => '是', 0 => '否']);
  52. $filter->like('key', '配置键名');
  53. $filter->like('name', '配置名称');
  54. });
  55. // 工具栏
  56. $grid->tools(function (Grid\Tools $tools) {
  57. $tools->append('<a href="' . admin_url('mex-configs/clear-cache') . '" class="btn btn-sm btn-warning"><i class="fa fa-refresh"></i> 清除缓存</a>');
  58. });
  59. // 行操作
  60. $grid->actions(function (Grid\Displayers\Actions $actions) {
  61. if ($actions->row->is_readonly) {
  62. $actions->disableEdit();
  63. $actions->disableDelete();
  64. }
  65. });
  66. // 暂时禁用批量操作
  67. $grid->disableBatchActions();
  68. // 默认排序
  69. $grid->model()->orderBy('sort_order')->orderBy('key');
  70. // 禁用创建按钮(配置通过SQL初始化)
  71. $grid->disableCreateButton();
  72. });
  73. }
  74. /**
  75. * 详情页面
  76. */
  77. protected function detail($id)
  78. {
  79. return Show::make($id, new MexConfigRepository(), function (Show $show) {
  80. $show->field('id', 'ID');
  81. $show->field('key', '配置键名');
  82. $show->field('name', '配置名称');
  83. $show->field('description', '配置描述');
  84. $show->field('group', '分组')->as(function ($value) {
  85. return MexConfigGroup::from($value)->getLabel();
  86. });
  87. $show->field('type', '类型')->as(function ($value) {
  88. return MexConfigType::from($value)->getLabel();
  89. });
  90. $show->field('value', '当前值');
  91. $show->field('default_value', '默认值');
  92. $show->field('options', '可选项')->json();
  93. $show->field('validation_rules', '验证规则');
  94. $show->field('is_enabled', '是否启用')->using([1 => '是', 0 => '否']);
  95. $show->field('is_readonly', '是否只读')->using([1 => '是', 0 => '否']);
  96. $show->field('sort_order', '排序权重');
  97. $show->field('remark', '备注说明');
  98. $show->field('created_at', '创建时间');
  99. $show->field('updated_at', '更新时间');
  100. // 禁用编辑和删除按钮(只读配置)
  101. $show->disableDeleteButton();
  102. });
  103. }
  104. /**
  105. * 编辑表单
  106. */
  107. protected function form()
  108. {
  109. return Form::make(new MexConfigRepository(), function (Form $form) {
  110. $form->display('id', 'ID');
  111. $form->display('key', '配置键名');
  112. $form->display('name', '配置名称');
  113. $form->display('description', '配置描述');
  114. $form->display('group', '分组')->with(function ($value) {
  115. return MexConfigGroup::from($value)->getLabel();
  116. });
  117. $form->display('type', '类型')->with(function ($value) {
  118. return MexConfigType::from($value)->getLabel();
  119. });
  120. // 根据类型显示配置值输入控件
  121. $form->text('value', '配置值')->help('请根据配置类型输入正确格式的值');
  122. $form->display('default_value', '默认值');
  123. $form->switch('is_enabled', '是否启用');
  124. $form->display('is_readonly', '是否只读')->with(function ($value) {
  125. return $value ? '是' : '否';
  126. });
  127. $form->number('sort_order', '排序权重');
  128. $form->textarea('remark', '备注说明')->rows(3);
  129. // 保存前验证
  130. $form->saving(function (Form $form) {
  131. $key = $form->model()->key;
  132. $value = $form->value;
  133. $validation = MexConfigService::validateValue($key, $value);
  134. if (!$validation['valid']) {
  135. return $form->response()->error($validation['message']);
  136. }
  137. });
  138. // 保存后清除缓存
  139. $form->saved(function (Form $form) {
  140. MexConfigService::clearCache($form->model()->key);
  141. });
  142. });
  143. }
  144. /**
  145. * 清除缓存
  146. */
  147. #[Get('mex-configs/clear-cache', name: 'admin.mex-configs.clear-cache')]
  148. public function clearCache()
  149. {
  150. try {
  151. MexConfigService::clearCache();
  152. return redirect()->back()->with('success', '缓存清除成功!');
  153. } catch (\Exception $e) {
  154. return redirect()->back()->with('error', '缓存清除失败:' . $e->getMessage());
  155. }
  156. }
  157. /**
  158. * 批量重置配置
  159. */
  160. #[Post('mex-configs/batch-reset', name: 'admin.mex-configs.batch-reset')]
  161. public function batchReset()
  162. {
  163. $keys = request('keys', []);
  164. if (empty($keys)) {
  165. return response()->json(['status' => false, 'message' => '请选择要重置的配置项']);
  166. }
  167. $successCount = 0;
  168. $failedKeys = [];
  169. foreach ($keys as $key) {
  170. if (MexConfigService::reset($key)) {
  171. $successCount++;
  172. } else {
  173. $failedKeys[] = $key;
  174. }
  175. }
  176. $message = "成功重置 {$successCount} 个配置项";
  177. if (!empty($failedKeys)) {
  178. $message .= ",失败:" . implode(', ', $failedKeys);
  179. }
  180. return response()->json([
  181. 'status' => empty($failedKeys),
  182. 'message' => $message
  183. ]);
  184. }
  185. /**
  186. * 配置分组视图
  187. */
  188. #[Get('mex-configs/groups', name: 'admin.mex-configs.groups')]
  189. public function groups(Content $content)
  190. {
  191. return $content
  192. ->title('Mex配置分组')
  193. ->description('按分组查看配置项')
  194. ->body(view('admin.mex.config-groups', [
  195. 'groups' => MexConfigGroup::cases(),
  196. 'configs' => MexConfigService::getAllConfigs()
  197. ]));
  198. }
  199. }