ItemController.php 7.6 KB

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