FarmConfigController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace App\Module\Farm\AdminControllers;
  3. use App\Module\Farm\Models\FarmConfig;
  4. use App\Module\Farm\Repositories\FarmConfigRepository;
  5. use App\Module\Farm\Services\FarmConfigService;
  6. use App\Module\Game\AdminControllers\LazyRenderable\GameRewardGroupLazyRenderable;
  7. use Dcat\Admin\Form;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Show;
  10. use Dcat\Admin\Http\Controllers\AdminController;
  11. /**
  12. * 农场配置管理控制器
  13. *
  14. * 路由: /admin/farm-configs
  15. * 清除缓存路由: POST /admin/farm-configs/clear-cache
  16. */
  17. class FarmConfigController extends AdminController
  18. {
  19. /**
  20. * 页面标题
  21. *
  22. * @var string
  23. */
  24. protected $title = '农场配置管理';
  25. /**
  26. * 页面描述
  27. *
  28. * @var string
  29. */
  30. protected $description = '管理农场相关的配置参数';
  31. /**
  32. * 构建表格
  33. *
  34. * @return Grid
  35. */
  36. protected function grid()
  37. {
  38. return Grid::make(new FarmConfigRepository(), function (Grid $grid) {
  39. $grid->column('id', 'ID')->sortable();
  40. $grid->column('config_key', '配置键')->copyable();
  41. $grid->column('config_name', '配置名称');
  42. $grid->column('config_value', '配置值')->display(function ($value) {
  43. if (strlen($value) > 50) {
  44. return substr($value, 0, 50) . '...';
  45. }
  46. return $value;
  47. });
  48. $grid->column('config_type', '配置类型')->using([
  49. 'string' => '字符串',
  50. 'integer' => '整数',
  51. 'float' => '浮点数',
  52. 'boolean' => '布尔值',
  53. 'json' => 'JSON对象',
  54. ])->label([
  55. 'string' => 'primary',
  56. 'integer' => 'success',
  57. 'float' => 'warning',
  58. 'boolean' => 'info',
  59. 'json' => 'danger',
  60. ]);
  61. $grid->column('is_active', '状态')->switch();
  62. $grid->column('description', '描述')->limit(30);
  63. $grid->column('updated_at', '更新时间')->sortable();
  64. // 禁用创建按钮(配置项通过数据库初始化)
  65. $grid->disableCreateButton();
  66. // 禁用删除操作
  67. $grid->disableDeleteButton();
  68. // 添加清除缓存工具
  69. $grid->tools(function (Grid\Tools $tools) {
  70. $tools->append('<a href="javascript:void(0)" class="btn btn-sm btn-outline-primary" onclick="clearFarmConfigCache()">清除缓存</a>');
  71. });
  72. // 添加JavaScript
  73. admin_script('
  74. function clearFarmConfigCache() {
  75. $.post("' . admin_url('farm-configs/clear-cache') . '", {
  76. _token: "' . csrf_token() . '"
  77. }).done(function(data) {
  78. if (data.status) {
  79. Dcat.success(data.message || "缓存清除成功");
  80. } else {
  81. Dcat.error(data.message || "缓存清除失败");
  82. }
  83. }).fail(function() {
  84. Dcat.error("请求失败");
  85. });
  86. }
  87. ');
  88. $grid->filter(function (Grid\Filter $filter) {
  89. $filter->like('config_key', '配置键');
  90. $filter->like('config_name', '配置名称');
  91. $filter->equal('config_type', '配置类型')->select([
  92. 'string' => '字符串',
  93. 'integer' => '整数',
  94. 'float' => '浮点数',
  95. 'boolean' => '布尔值',
  96. 'json' => 'JSON对象',
  97. ]);
  98. $filter->equal('is_active', '状态')->select([
  99. 1 => '启用',
  100. 0 => '禁用',
  101. ]);
  102. });
  103. });
  104. }
  105. /**
  106. * 构建详情页
  107. *
  108. * @param mixed $id
  109. * @return Show
  110. */
  111. protected function detail($id)
  112. {
  113. return Show::make($id, new FarmConfigRepository(), function (Show $show) {
  114. $show->field('id', 'ID');
  115. $show->field('config_key', '配置键');
  116. $show->field('config_name', '配置名称');
  117. $show->field('config_value', '配置值');
  118. $show->field('config_type', '配置类型')->using([
  119. 'string' => '字符串',
  120. 'integer' => '整数',
  121. 'float' => '浮点数',
  122. 'boolean' => '布尔值',
  123. 'json' => 'JSON对象',
  124. ]);
  125. $show->field('description', '配置描述');
  126. $show->field('default_value', '默认值');
  127. $show->field('is_active', '启用状态')->using([
  128. 1 => '启用',
  129. 0 => '禁用',
  130. ]);
  131. $show->field('created_at', '创建时间');
  132. $show->field('updated_at', '更新时间');
  133. // 显示类型转换后的值
  134. $show->field('typed_value', '类型转换后的值')->as(function () {
  135. return json_encode($this->getTypedValue(), JSON_UNESCAPED_UNICODE);
  136. });
  137. });
  138. }
  139. /**
  140. * 构建表单
  141. *
  142. * @return Form
  143. */
  144. protected function form()
  145. {
  146. return Form::make(new FarmConfigRepository(), function (Form $form) {
  147. $form->display('id', 'ID');
  148. $form->display('config_key', '配置键');
  149. $form->display('config_name', '配置名称');
  150. // 根据配置键显示不同的输入控件
  151. if ($form->model() && $form->model()->config_key === 'farm_init_reward_group_id') {
  152. // 农场初始化奖励组ID - 使用数字输入框
  153. $form->number('config_value', '配置值')
  154. ->min(0)
  155. ->help('选择农场初始化时发放的奖励组ID,设置为0表示不发放奖励');
  156. } else {
  157. // 其他配置项的通用处理
  158. $configType = $form->model()->config_type ?? 'string';
  159. switch ($configType) {
  160. case 'integer':
  161. $form->number('config_value', '配置值');
  162. break;
  163. case 'float':
  164. $form->number('config_value', '配置值')->decimal(2);
  165. break;
  166. case 'boolean':
  167. $form->switch('config_value', '配置值');
  168. break;
  169. case 'json':
  170. $form->textarea('config_value', '配置值')->help('请输入有效的JSON格式');
  171. break;
  172. case 'string':
  173. default:
  174. $form->text('config_value', '配置值');
  175. break;
  176. }
  177. }
  178. $form->display('config_type_name', '配置类型');
  179. $form->display('description', '配置描述');
  180. $form->display('default_value', '默认值');
  181. $form->switch('is_active', '启用状态')->default(1);
  182. $form->display('created_at', '创建时间');
  183. $form->display('updated_at', '更新时间');
  184. // 保存后清除缓存
  185. $form->saved(function (Form $form) {
  186. FarmConfigService::clearCache();
  187. });
  188. });
  189. }
  190. /**
  191. * 清除配置缓存
  192. *
  193. * @return \Illuminate\Http\JsonResponse
  194. */
  195. public function clearCache()
  196. {
  197. try {
  198. FarmConfigService::clearCache();
  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. /**
  211. * 注册自定义路由
  212. *
  213. * @return void
  214. */
  215. public static function routes()
  216. {
  217. // 清除缓存路由
  218. admin_route('farm-configs/clear-cache', [static::class, 'clearCache'], ['POST']);
  219. }
  220. }