PetConfigController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Module\Pet\AdminControllers;
  3. use App\Module\Game\DCache\PetJsonConfig;
  4. use App\Module\Pet\Models\PetConfig;
  5. use App\Module\Pet\Repositorys\PetConfigRepository;
  6. use Dcat\Admin\Form;
  7. use Dcat\Admin\Grid;
  8. use Dcat\Admin\Show;
  9. use Illuminate\Http\Request;
  10. use UCore\DcatAdmin\AdminController;
  11. use Spatie\RouteAttributes\Attributes\Resource;
  12. use Spatie\RouteAttributes\Attributes\Get;
  13. use App\Module\Pet\AdminControllers\Helper\FilterHelper;
  14. use App\Module\Pet\AdminControllers\Helper\FormHelper;
  15. use App\Module\Pet\AdminControllers\Helper\GridHelper;
  16. use App\Module\Pet\AdminControllers\Helper\ShowHelper;
  17. /**
  18. * 宠物配置管理控制器
  19. *
  20. * @package App\Module\Pet\AdminControllers
  21. */
  22. #[Resource('pet-configs', names: 'dcat.admin.pet-configs')]
  23. class PetConfigController extends AdminController
  24. {
  25. /**
  26. * 生成宠物JSON数据
  27. */
  28. #[Get('pet-configs/generate-json')]
  29. public function generateJson()
  30. {
  31. $result = PetJsonConfig::getData([], true);
  32. return response()->json([
  33. 'status' => $result['success'] ? 'success' : 'error',
  34. 'message' => $result['success'] ? 'JSON生成成功' : 'JSON生成失败: ' . ($result['error'] ?? '')
  35. ]);
  36. }
  37. /**
  38. * 标题
  39. *
  40. * @var string
  41. */
  42. protected $title = '宠物配置管理';
  43. /**
  44. * 列表页
  45. *
  46. * @return Grid
  47. */
  48. protected function grid()
  49. {
  50. return Grid::make(new PetConfigRepository(), function (Grid $grid) {
  51. $helper = new GridHelper($grid, $this);
  52. $grid->column('id', 'ID')->sortable();
  53. $grid->column('pet_type', '宠物类型');
  54. $grid->column('stamina_recovery', '体力恢复值/分钟')->display(function () {
  55. $numericAttributes = $this->numeric_attributes;
  56. if ($numericAttributes && isset($numericAttributes->stamina_recovery)) {
  57. return $numericAttributes->stamina_recovery . ' /分钟';
  58. }
  59. return '-';
  60. });
  61. $grid->column('created_at', '创建时间');
  62. $grid->column('updated_at', '更新时间');
  63. // 添加生成JSON配置按钮
  64. $grid->tools(function ($tools) {
  65. $tools->append('<a class="btn btn-primary btn-sm" href="javascript:void(0);" onclick="generatePetJson()">生成宠物配置JSON</a>');
  66. // 添加JavaScript代码
  67. admin_script(<<<JS
  68. function generatePetJson() {
  69. $.ajax({
  70. method: 'GET',
  71. url: '/admin/pet-configs/generate-json',
  72. success: function (data) {
  73. if (data.status === 'success') {
  74. Dcat.success(data.message);
  75. } else {
  76. Dcat.error(data.message);
  77. }
  78. },
  79. error: function (xhr) {
  80. Dcat.error('生成JSON失败: ' + xhr.statusText);
  81. }
  82. });
  83. }
  84. JS);
  85. });
  86. // 筛选
  87. $grid->filter(function ($filter) {
  88. $helper = new FilterHelper($filter, $this);
  89. $helper->equal('id', 'ID');
  90. $filter->like('pet_type', '宠物类型');
  91. $filter->equal('max_level', '最大等级');
  92. });
  93. return $grid;
  94. });
  95. }
  96. /**
  97. * 详情页
  98. *
  99. * @param mixed $id
  100. * @return Show
  101. */
  102. protected function detail($id)
  103. {
  104. return Show::make($id, new PetConfigRepository(), function (Show $show) {
  105. $helper = new ShowHelper($show, $this);
  106. $helper->field('id', 'ID');
  107. $show->field('pet_type', '宠物类型');
  108. $show->field('display_attributes', '显示属性配置')->json();
  109. $show->field('numeric_attributes', '数值属性配置')->json();
  110. $show->field('created_at', '创建时间');
  111. $show->field('updated_at', '更新时间');
  112. return $show;
  113. });
  114. }
  115. /**
  116. * 表单
  117. *
  118. * @return Form
  119. */
  120. protected function form()
  121. {
  122. return Form::make(new PetConfigRepository(), function (Form $form) {
  123. $helper = new FormHelper($form, $this);
  124. $form->display('id', 'ID');
  125. $form->text('pet_type', '宠物类型')
  126. ->required()
  127. ->help('宠物类型标识,如:松狮、哈士奇等');
  128. $helper->embedsCats('display_attributes', '显示属性配置')
  129. ->help('宠物的显示属性配置');
  130. $helper->embedsCats('numeric_attributes', '数值属性配置')
  131. ->help('宠物的数值属性配置');
  132. $form->display('created_at', '创建时间');
  133. $form->display('updated_at', '更新时间');
  134. return $form;
  135. });
  136. }
  137. }