| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <?php
- namespace App\Module\Farm\AdminControllers;
- use App\Module\Farm\Models\FarmConfig;
- use App\Module\Farm\Repositories\FarmConfigRepository;
- use App\Module\Farm\Services\FarmConfigService;
- use App\Module\Game\AdminControllers\LazyRenderable\GameRewardGroupLazyRenderable;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Http\Controllers\AdminController;
- /**
- * 农场配置管理控制器
- *
- * 路由: /admin/farm-configs
- * 清除缓存路由: POST /admin/farm-configs/clear-cache
- */
- class FarmConfigController extends AdminController
- {
- /**
- * 页面标题
- *
- * @var string
- */
- protected $title = '农场配置管理';
- /**
- * 页面描述
- *
- * @var string
- */
- protected $description = '管理农场相关的配置参数';
- /**
- * 构建表格
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new FarmConfigRepository(), function (Grid $grid) {
- $grid->column('id', 'ID')->sortable();
- $grid->column('config_key', '配置键')->copyable();
- $grid->column('config_name', '配置名称');
- $grid->column('config_value', '配置值')->display(function ($value) {
- if (strlen($value) > 50) {
- return substr($value, 0, 50) . '...';
- }
- return $value;
- });
- $grid->column('config_type', '配置类型')->using([
- 'string' => '字符串',
- 'integer' => '整数',
- 'float' => '浮点数',
- 'boolean' => '布尔值',
- 'json' => 'JSON对象',
- ])->label([
- 'string' => 'primary',
- 'integer' => 'success',
- 'float' => 'warning',
- 'boolean' => 'info',
- 'json' => 'danger',
- ]);
- $grid->column('is_active', '状态')->switch();
- $grid->column('description', '描述')->limit(30);
- $grid->column('updated_at', '更新时间')->sortable();
- // 禁用创建按钮(配置项通过数据库初始化)
- $grid->disableCreateButton();
- // 禁用删除操作
- $grid->disableDeleteButton();
- // 添加清除缓存工具
- $grid->tools(function (Grid\Tools $tools) {
- $tools->append('<a href="javascript:void(0)" class="btn btn-sm btn-outline-primary" onclick="clearFarmConfigCache()">清除缓存</a>');
- });
- // 添加JavaScript
- admin_script('
- function clearFarmConfigCache() {
- $.post("' . admin_url('farm-configs/clear-cache') . '", {
- _token: "' . csrf_token() . '"
- }).done(function(data) {
- if (data.status) {
- Dcat.success(data.message || "缓存清除成功");
- } else {
- Dcat.error(data.message || "缓存清除失败");
- }
- }).fail(function() {
- Dcat.error("请求失败");
- });
- }
- ');
- $grid->filter(function (Grid\Filter $filter) {
- $filter->like('config_key', '配置键');
- $filter->like('config_name', '配置名称');
- $filter->equal('config_type', '配置类型')->select([
- 'string' => '字符串',
- 'integer' => '整数',
- 'float' => '浮点数',
- 'boolean' => '布尔值',
- 'json' => 'JSON对象',
- ]);
- $filter->equal('is_active', '状态')->select([
- 1 => '启用',
- 0 => '禁用',
- ]);
- });
- });
- }
- /**
- * 构建详情页
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new FarmConfigRepository(), function (Show $show) {
- $show->field('id', 'ID');
- $show->field('config_key', '配置键');
- $show->field('config_name', '配置名称');
- $show->field('config_value', '配置值');
- $show->field('config_type', '配置类型')->using([
- 'string' => '字符串',
- 'integer' => '整数',
- 'float' => '浮点数',
- 'boolean' => '布尔值',
- 'json' => 'JSON对象',
- ]);
- $show->field('description', '配置描述');
- $show->field('default_value', '默认值');
- $show->field('is_active', '启用状态')->using([
- 1 => '启用',
- 0 => '禁用',
- ]);
- $show->field('created_at', '创建时间');
- $show->field('updated_at', '更新时间');
- // 显示类型转换后的值
- $show->field('typed_value', '类型转换后的值')->as(function () {
- return json_encode($this->getTypedValue(), JSON_UNESCAPED_UNICODE);
- });
- });
- }
- /**
- * 构建表单
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new FarmConfigRepository(), function (Form $form) {
- $form->display('id', 'ID');
- $form->display('config_key', '配置键');
- $form->display('config_name', '配置名称');
- // 根据配置键显示不同的输入控件
- if ($form->model() && $form->model()->config_key === 'farm_init_reward_group_id') {
- // 农场初始化奖励组ID - 使用数字输入框
- $form->number('config_value', '配置值')
- ->min(0)
- ->help('选择农场初始化时发放的奖励组ID,设置为0表示不发放奖励');
- } else {
- // 其他配置项的通用处理
- $configType = $form->model()->config_type ?? 'string';
- switch ($configType) {
- case 'integer':
- $form->number('config_value', '配置值');
- break;
- case 'float':
- $form->number('config_value', '配置值')->decimal(2);
- break;
- case 'boolean':
- $form->switch('config_value', '配置值');
- break;
- case 'json':
- $form->textarea('config_value', '配置值')->help('请输入有效的JSON格式');
- break;
- case 'string':
- default:
- $form->text('config_value', '配置值');
- break;
- }
- }
- $form->display('config_type_name', '配置类型');
- $form->display('description', '配置描述');
- $form->display('default_value', '默认值');
- $form->switch('is_active', '启用状态')->default(1);
- $form->display('created_at', '创建时间');
- $form->display('updated_at', '更新时间');
- // 保存后清除缓存
- $form->saved(function (Form $form) {
- FarmConfigService::clearCache();
- });
- });
- }
- /**
- * 清除配置缓存
- *
- * @return \Illuminate\Http\JsonResponse
- */
- public function clearCache()
- {
- try {
- FarmConfigService::clearCache();
- return response()->json([
- 'status' => true,
- 'message' => '农场配置缓存清除成功'
- ]);
- } catch (\Exception $e) {
- return response()->json([
- 'status' => false,
- 'message' => '缓存清除失败:' . $e->getMessage()
- ]);
- }
- }
- /**
- * 注册自定义路由
- *
- * @return void
- */
- public static function routes()
- {
- // 清除缓存路由
- admin_route('farm-configs/clear-cache', [static::class, 'clearCache'], ['POST']);
- }
- }
|