FarmConfigController.php 8.5 KB

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