| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- <?php
- namespace App\Module\Mex\AdminControllers;
- use App\Module\Mex\AdminControllers\Helper\GridHelper;
- use App\Module\Mex\Enums\MexConfigGroup;
- use App\Module\Mex\Enums\MexConfigType;
- use App\Module\Mex\Repositories\MexConfigRepository;
- use App\Module\Mex\Service\MexConfigService;
- use Spatie\RouteAttributes\Attributes\Get;
- use Spatie\RouteAttributes\Attributes\Post;
- use Spatie\RouteAttributes\Attributes\Resource;
- use UCore\DcatAdmin\AdminController;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Form;
- use Dcat\Admin\Layout\Content;
- /**
- * Mex配置管理控制器
- *
- * 路由:/admin/mex-configs
- */
- #[Resource('mex-configs', names: 'dcat.admin.mex-configs')]
- class MexConfigController extends AdminController
- {
- /**
- * 数据表格
- */
- protected function grid()
- {
- return Grid::make(new MexConfigRepository(), function (Grid $grid) {
- $grid->column('id', 'ID')->sortable();
- $grid->column('key', '配置键名')->copyable();
- $grid->column('name', '配置名称');
- $grid->column('group', '分组')->display(function ($value) {
- return $value instanceof MexConfigGroup ? $value->getLabel() : MexConfigGroup::from($value)->getLabel();
- })->label();
- $grid->column('type', '类型')->display(function ($value) {
- return $value instanceof MexConfigType ? $value->getLabel() : MexConfigType::from($value)->getLabel();
- })->label('info');
- $grid->column('value', '当前值')->limit(50);
- $grid->column('is_enabled', '状态')->switch();
- $grid->column('is_readonly', '只读')->display(function ($value) {
- return $value ? '是' : '否';
- });
- $grid->column('sort_order', '排序')->editable();
- $grid->column('updated_at', '更新时间');
- // 筛选器
- $grid->filter(function (Grid\Filter $filter) {
- $filter->equal('group', '分组')->select(MexConfigGroup::getOptions());
- $filter->equal('type', '类型')->select(MexConfigType::getOptions());
- $filter->equal('is_enabled', '状态')->select([1 => '启用', 0 => '禁用']);
- $filter->equal('is_readonly', '只读')->select([1 => '是', 0 => '否']);
- $filter->like('key', '配置键名');
- $filter->like('name', '配置名称');
- });
- // 工具栏
- $grid->tools(function (Grid\Tools $tools) {
- $tools->append('<a href="' . admin_url('mex-configs/clear-cache') . '" class="btn btn-sm btn-warning"><i class="fa fa-refresh"></i> 清除缓存</a>');
- });
- // 行操作
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- if ($actions->row->is_readonly) {
- $actions->disableEdit();
- $actions->disableDelete();
- }
- });
- // 暂时禁用批量操作
- $grid->disableBatchActions();
- // 默认排序
- $grid->model()->orderBy('sort_order')->orderBy('key');
- // 禁用创建按钮(配置通过SQL初始化)
- $grid->disableCreateButton();
- });
- }
- /**
- * 详情页面
- */
- protected function detail($id)
- {
- return Show::make($id, new MexConfigRepository(), function (Show $show) {
- $show->field('id', 'ID');
- $show->field('key', '配置键名');
- $show->field('name', '配置名称');
- $show->field('description', '配置描述');
- $show->field('group', '分组')->as(function ($value) {
- return MexConfigGroup::from($value)->getLabel();
- });
- $show->field('type', '类型')->as(function ($value) {
- return MexConfigType::from($value)->getLabel();
- });
- $show->field('value', '当前值');
- $show->field('default_value', '默认值');
- $show->field('options', '可选项')->json();
- $show->field('validation_rules', '验证规则');
- $show->field('is_enabled', '是否启用')->using([1 => '是', 0 => '否']);
- $show->field('is_readonly', '是否只读')->using([1 => '是', 0 => '否']);
- $show->field('sort_order', '排序权重');
- $show->field('remark', '备注说明');
- $show->field('created_at', '创建时间');
- $show->field('updated_at', '更新时间');
- // 禁用编辑和删除按钮(只读配置)
- $show->disableDeleteButton();
- });
- }
- /**
- * 编辑表单
- */
- protected function form()
- {
- return Form::make(new MexConfigRepository(), function (Form $form) {
- $form->display('id', 'ID');
- $form->display('key', '配置键名');
- $form->display('name', '配置名称');
- $form->display('description', '配置描述');
- $form->display('group', '分组')->with(function ($value) {
- return MexConfigGroup::from($value)->getLabel();
- });
- $form->display('type', '类型')->with(function ($value) {
- return MexConfigType::from($value)->getLabel();
- });
- // 根据类型显示配置值输入控件
- $form->text('value', '配置值')->help('请根据配置类型输入正确格式的值');
- $form->display('default_value', '默认值');
- $form->switch('is_enabled', '是否启用');
- $form->display('is_readonly', '是否只读')->with(function ($value) {
- return $value ? '是' : '否';
- });
- $form->number('sort_order', '排序权重');
- $form->textarea('remark', '备注说明')->rows(3);
- // 保存前验证
- $form->saving(function (Form $form) {
- $key = $form->model()->key;
- $value = $form->value;
-
- $validation = MexConfigService::validateValue($key, $value);
- if (!$validation['valid']) {
- return $form->response()->error($validation['message']);
- }
- });
- // 保存后清除缓存
- $form->saved(function (Form $form) {
- MexConfigService::clearCache($form->model()->key);
- });
- });
- }
- /**
- * 清除缓存
- */
- #[Get('mex-configs/clear-cache', name: 'admin.mex-configs.clear-cache')]
- public function clearCache()
- {
- try {
- MexConfigService::clearCache();
- return redirect()->back()->with('success', '缓存清除成功!');
- } catch (\Exception $e) {
- return redirect()->back()->with('error', '缓存清除失败:' . $e->getMessage());
- }
- }
- /**
- * 批量重置配置
- */
- #[Post('mex-configs/batch-reset', name: 'admin.mex-configs.batch-reset')]
- public function batchReset()
- {
- $keys = request('keys', []);
-
- if (empty($keys)) {
- return response()->json(['status' => false, 'message' => '请选择要重置的配置项']);
- }
- $successCount = 0;
- $failedKeys = [];
- foreach ($keys as $key) {
- if (MexConfigService::reset($key)) {
- $successCount++;
- } else {
- $failedKeys[] = $key;
- }
- }
- $message = "成功重置 {$successCount} 个配置项";
- if (!empty($failedKeys)) {
- $message .= ",失败:" . implode(', ', $failedKeys);
- }
- return response()->json([
- 'status' => empty($failedKeys),
- 'message' => $message
- ]);
- }
- /**
- * 配置分组视图
- */
- #[Get('mex-configs/groups', name: 'admin.mex-configs.groups')]
- public function groups(Content $content)
- {
- return $content
- ->title('Mex配置分组')
- ->description('按分组查看配置项')
- ->body(view('admin.mex.config-groups', [
- 'groups' => MexConfigGroup::cases(),
- 'configs' => MexConfigService::getAllConfigs()
- ]));
- }
- }
|