|
|
@@ -0,0 +1,242 @@
|
|
|
+<?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']);
|
|
|
+ }
|
|
|
+}
|