PetConfigController.php 5.3 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('grade_probability', '品阶概率配置')->display(function ($value) {
  55. if (is_array($value) || is_object($value)) {
  56. return json_encode($value, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  57. }
  58. return $value;
  59. });
  60. $grid->column('stamina_recovery', '体力恢复值/分钟')->sortable();
  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('grade_probability', '品阶概率配置')->json();
  109. $show->field('display_attributes', '显示属性配置')->json();
  110. $show->field('numeric_attributes', '数值属性配置')->json();
  111. $show->field('created_at', '创建时间');
  112. $show->field('updated_at', '更新时间');
  113. return $show;
  114. });
  115. }
  116. /**
  117. * 表单
  118. *
  119. * @return Form
  120. */
  121. protected function form()
  122. {
  123. return Form::make(new PetConfigRepository(), function (Form $form) {
  124. $helper = new FormHelper($form, $this);
  125. $form->display('id', 'ID');
  126. $form->text('pet_type', '宠物类型')
  127. ->required()
  128. ->help('宠物类型标识,如:松狮、哈士奇等');
  129. $helper->embedsCats('grade_probability', '品阶概率配置')
  130. ->help('各品阶的概率配置,如:{"FIRST":0.6,"SECOND":0.25,"THIRD":0.1,"FOURTH":0.05}');
  131. $helper->embedsCats('display_attributes', '显示属性配置')
  132. ->help('宠物的显示属性配置');
  133. $helper->embedsCats('numeric_attributes', '数值属性配置')
  134. ->help('宠物的数值属性配置');
  135. $form->display('created_at', '创建时间');
  136. $form->display('updated_at', '更新时间');
  137. return $form;
  138. });
  139. }
  140. }