ItemController.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers;
  3. use App\Module\GameItems\Enums\ITEM_TYPE;
  4. use App\Module\GameItems\Models\ItemCategory;
  5. use App\Module\GameItems\Repositorys\ItemRepository;
  6. use Dcat\Admin\Form;
  7. use Dcat\Admin\Grid;
  8. use Dcat\Admin\Show;
  9. use UCore\DcatAdmin\AdminController;
  10. use Spatie\RouteAttributes\Attributes\Resource;
  11. use Spatie\RouteAttributes\Attributes\Get;
  12. use Illuminate\Support\Facades\Artisan;
  13. use UCore\DcatAdmin\FilterHelper;
  14. use UCore\DcatAdmin\FormHelper;
  15. use UCore\DcatAdmin\GridHelper;
  16. use UCore\DcatAdmin\ShowHelper;
  17. #[Resource('game-items', names: 'dcat.admin.game-items')]
  18. class ItemController extends AdminController
  19. {
  20. /**
  21. * 生成物品JSON数据
  22. */
  23. #[Get('game-items/generate-json')]
  24. public function generateJson()
  25. {
  26. $success = \App\Module\GameItems\Commands\GenerateItemsJsonCommand::generateJson();
  27. return response()->json([
  28. 'status' => $success ? 'success' : 'error',
  29. 'message' => $success ? 'JSON生成成功' : 'JSON生成失败'
  30. ]);
  31. }
  32. /**
  33. * 标题
  34. *
  35. * @var string
  36. */
  37. protected $title = '物品管理';
  38. /**
  39. * 列表页
  40. *
  41. * @return Grid
  42. */
  43. protected function grid()
  44. {
  45. return Grid::make(new ItemRepository(['category']), function (Grid $grid) {
  46. $status = \App\Module\GameItems\AdminControllers\Tools\RefreshCheckTool::checkSyncStatus();
  47. if ($status['is_synced']) {
  48. admin_success('JSON配置表状态', $status['message']);
  49. } else {
  50. admin_warning('JSON配置表状态', $status['message']);
  51. }
  52. $grid->tools([
  53. new \App\Module\GameItems\AdminControllers\Tools\RefreshCheckTool($status['should_display']),
  54. new \App\Module\GameItems\AdminControllers\Tools\SyncItemsJsonTool($status['should_display'])
  55. ]);
  56. $helper = new GridHelper($grid, $this);
  57. $grid->column('id', 'ID')->sortable();
  58. $grid->column('name', '名称');
  59. $grid->column('category.name', '分类');
  60. $helper->columnModelCats('type');
  61. $grid->column('is_unique', '单独属性')->bool();
  62. $grid->column('max_stack', '最大堆叠');
  63. $grid->column('tradable', '可交易')->bool();
  64. $grid->column('dismantlable', '可分解')->bool();
  65. $grid->column('default_expire_seconds', '默认过期时间(秒)');
  66. $grid->column('global_expire_at', '全局过期时间');
  67. $grid->column('created_at', '创建时间');
  68. $grid->column('updated_at', '更新时间');
  69. // 添加复制行操作
  70. $grid->actions(function (Grid\Displayers\Actions $actions) {
  71. $actions->disableDelete();
  72. $actions->append(new \App\Module\GameItems\AdminControllers\Actions\DuplicateRowAction());
  73. });
  74. // 筛选
  75. $grid->filter(function ($filter) {
  76. $helper = new FilterHelper($filter, $this);
  77. $helper->equal('id','ID');
  78. $filter->like('name', '名称');
  79. $filter->equal('category_id', '分类')->select(
  80. ItemCategory::pluck('name', 'id')
  81. );
  82. $helper->equalRadioModelCats('type','类型');
  83. $filter->equal('is_unique', '单独属性')->radio([
  84. 1 => '是',
  85. 0 => '否',
  86. ]);
  87. $filter->equal('tradable', '可交易')->radio([
  88. 1 => '是',
  89. 0 => '否',
  90. ]);
  91. $filter->equal('dismantlable', '可分解')->radio([
  92. 1 => '是',
  93. 0 => '否',
  94. ]);
  95. });
  96. return $grid;
  97. });
  98. }
  99. /**
  100. * 详情页
  101. *
  102. * @param mixed $id
  103. * @return Show
  104. */
  105. protected function detail($id)
  106. {
  107. return Show::make($id, new ItemRepository(), function (Show $show) {
  108. $helper = new ShowHelper($show, $this);
  109. $helper->field('id','ID');
  110. $show->field('name', '名称');
  111. $show->field('description', '描述');
  112. $show->field('category.name', '分类');
  113. $helper->fieldModelCats('type');
  114. $show->field('is_unique', '单独属性')->as(function ($isUnique) {
  115. return $isUnique ? '是' : '否';
  116. });
  117. $show->field('max_stack', '最大堆叠');
  118. $show->field('sell_price', '出售价格');
  119. $show->field('tradable', '可交易')->as(function ($tradable) {
  120. return $tradable ? '是' : '否';
  121. });
  122. $show->field('dismantlable', '可分解')->as(function ($dismantlable) {
  123. return $dismantlable ? '是' : '否';
  124. });
  125. $show->field('default_expire_seconds', '默认过期时间(秒)');
  126. $show->field('display_attributes', '显示属性')->json();
  127. $show->field('numeric_attributes', '数值属性')->json();
  128. $show->field('global_expire_at', '全局过期时间');
  129. $show->field('created_at', '创建时间');
  130. $show->field('updated_at', '更新时间');
  131. // 如果是宝箱类型,显示宝箱内容
  132. if ($show->getModel()->type == ITEM_TYPE::CHEST) {
  133. $show->chestContents('宝箱内容', function ($chestContents) {
  134. $chestContents->resource('/admin/game-items-chest-contents');
  135. $chestContents->id('ID');
  136. $chestContents->item()->name('物品名称');
  137. $chestContents->group()->name('物品组名称');
  138. $chestContents->min_quantity('最小数量');
  139. $chestContents->max_quantity('最大数量');
  140. $chestContents->weight('权重');
  141. $chestContents->allow_duplicate('允许重复')->bool();
  142. $chestContents->pity_count('保底次数');
  143. $chestContents->pity_weight_factor('保底权重因子');
  144. });
  145. }
  146. return $show;
  147. });
  148. }
  149. /**
  150. * 表单
  151. *
  152. * @return Form
  153. */
  154. protected function form()
  155. {
  156. return Form::make(new ItemRepository(), function (Form $form) {
  157. $helper = new FormHelper($form,$this);
  158. $helper->text('name')->required();
  159. $form->textarea('description', '描述');
  160. $form->select('category_id', '分类')
  161. ->options(ItemCategory::pluck('name', 'id'))
  162. ->required();
  163. $helper->selectOptionCast('type','类型');
  164. $form->switch('is_unique', '单独属性')
  165. ->default(false);
  166. $form->number('max_stack', '最大堆叠')
  167. ->default(1)
  168. ->min(1)
  169. ->help('0表示无限堆叠');
  170. $form->number('sell_price', '出售价格')
  171. ->default(0)
  172. ->min(0);
  173. $form->switch('tradable', '可交易')
  174. ->default(true);
  175. $form->switch('dismantlable', '可分解')
  176. ->default(true);
  177. $form->number('default_expire_seconds', '默认过期时间(秒)')
  178. ->default(0)
  179. ->help('0表示永不过期');
  180. $helper->embedsCats('display_attributes', '显示属性')
  181. ->help('用于显示的属性,如:攻击力、防御力等');
  182. $helper->embedsCats('numeric_attributes', '数值属性')
  183. ->help('用于计算的属性,如:宝箱掉落物品数量范围等');
  184. $form->datetime('global_expire_at', '全局过期时间')
  185. ->help('所有该物品的全局过期时间,为空表示永不过期');
  186. // 保存前回调
  187. $form->saving(function (Form $form) {
  188. // 如果是宝箱类型,确保有min_drop_count和max_drop_count属性
  189. if ($form->type == ITEM_TYPE::CHEST) {
  190. $numericAttributes = $form->numeric_attributes ?: [];
  191. if (!isset($numericAttributes['min_drop_count'])) {
  192. $numericAttributes['min_drop_count'] = 1;
  193. }
  194. if (!isset($numericAttributes['max_drop_count'])) {
  195. $numericAttributes['max_drop_count'] = 1;
  196. }
  197. $form->numeric_attributes = $numericAttributes;
  198. }
  199. });
  200. return $form;
  201. });
  202. }
  203. }