ItemController.php 7.9 KB

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