ItemController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers;
  3. use App\Module\GameItems\Enums\ITEM_TYPE;
  4. use App\Module\GameItems\Enums\ITEM_RARITY;
  5. use App\Module\GameItems\Models\ItemCategory;
  6. use App\Module\GameItems\Models\Item;
  7. use App\Module\GameItems\Repositorys\ItemRepository;
  8. use Dcat\Admin\Form;
  9. use Dcat\Admin\Grid;
  10. use Dcat\Admin\Show;
  11. use UCore\DcatAdmin\AdminController;
  12. use Spatie\RouteAttributes\Attributes\Resource;
  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. * 标题
  22. *
  23. * @var string
  24. */
  25. protected $title = '物品管理';
  26. /**
  27. * 列表页
  28. *
  29. * @return Grid
  30. */
  31. protected function grid()
  32. {
  33. return Grid::make(new ItemRepository(), function (Grid $grid) {
  34. $helper = new GridHelper($grid, $this);
  35. $grid->column('id', 'ID')->sortable();
  36. $grid->column('name', '名称');
  37. $grid->column('category.name', '分类');
  38. $helper->columnModelCats('type');
  39. $grid->column('icon', '图标')->image('', 50, 50);
  40. $grid->column('is_unique', '单独属性')->bool();
  41. $grid->column('max_stack', '最大堆叠');
  42. $grid->column('tradable', '可交易')->bool();
  43. $grid->column('dismantlable', '可分解')->bool();
  44. $grid->column('default_expire_seconds', '默认过期时间(秒)');
  45. $grid->column('global_expire_at', '全局过期时间');
  46. $grid->column('created_at', '创建时间');
  47. $grid->column('updated_at', '更新时间');
  48. // 筛选
  49. $grid->filter(function ($filter) {
  50. $helper = new FilterHelper($filter, $this);
  51. $helper->equal('id','ID');
  52. $filter->like('name', '名称');
  53. $filter->equal('category_id', '分类')->select(
  54. ItemCategory::pluck('name', 'id')
  55. );
  56. $helper->equalRadioModelCats('type','类型');
  57. $filter->equal('rarity', '稀有度')->select(ITEM_RARITY::all());
  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('icon', '图标')->image();
  90. $show->field('is_unique', '单独属性')->as(function ($isUnique) {
  91. return $isUnique ? '是' : '否';
  92. });
  93. $show->field('max_stack', '最大堆叠');
  94. $show->field('sell_price', '出售价格');
  95. $show->field('tradable', '可交易')->as(function ($tradable) {
  96. return $tradable ? '是' : '否';
  97. });
  98. $show->field('dismantlable', '可分解')->as(function ($dismantlable) {
  99. return $dismantlable ? '是' : '否';
  100. });
  101. $show->field('default_expire_seconds', '默认过期时间(秒)');
  102. $show->field('display_attributes', '显示属性')->json();
  103. $show->field('numeric_attributes', '数值属性')->json();
  104. $show->field('global_expire_at', '全局过期时间');
  105. $show->field('created_at', '创建时间');
  106. $show->field('updated_at', '更新时间');
  107. // 如果是宝箱类型,显示宝箱内容
  108. if ($show->getModel()->type == ITEM_TYPE::OPENABLE) {
  109. $show->chestContents('宝箱内容', function ($chestContents) {
  110. $chestContents->resource('/admin/game-items-chest-contents');
  111. $chestContents->id('ID');
  112. $chestContents->item()->name('物品名称');
  113. $chestContents->group()->name('物品组名称');
  114. $chestContents->min_quantity('最小数量');
  115. $chestContents->max_quantity('最大数量');
  116. $chestContents->weight('权重');
  117. $chestContents->allow_duplicate('允许重复')->bool();
  118. $chestContents->pity_count('保底次数');
  119. $chestContents->pity_weight_factor('保底权重因子');
  120. });
  121. }
  122. return $show;
  123. });
  124. }
  125. /**
  126. * 表单
  127. *
  128. * @return Form
  129. */
  130. protected function form()
  131. {
  132. return Form::make(new ItemRepository(), function (Form $form) {
  133. $helper = new FormHelper($form,$this);
  134. $helper->text('name')->required();
  135. $form->textarea('description', '描述');
  136. $form->select('category_id', '分类')
  137. ->options(ItemCategory::pluck('name', 'id'))
  138. ->required();
  139. $form->select('type', '类型')
  140. ->options(ITEM_TYPE::all())
  141. ->required();
  142. $form->select('rarity', '稀有度')
  143. ->options(ITEM_RARITY::all())
  144. ->required();
  145. $form->image('icon', '图标')
  146. ->uniqueName()
  147. ->autoUpload()
  148. ->required();
  149. $form->switch('is_unique', '单独属性')
  150. ->default(false);
  151. $form->number('max_stack', '最大堆叠')
  152. ->default(1)
  153. ->min(1)
  154. ->help('0表示无限堆叠');
  155. $form->number('sell_price', '出售价格')
  156. ->default(0)
  157. ->min(0);
  158. $form->switch('tradable', '可交易')
  159. ->default(true);
  160. $form->switch('dismantlable', '可分解')
  161. ->default(true);
  162. $form->number('default_expire_seconds', '默认过期时间(秒)')
  163. ->default(0)
  164. ->help('0表示永不过期');
  165. $form->keyValue('display_attributes', '显示属性')
  166. ->help('用于显示的属性,如:攻击力、防御力等');
  167. $form->keyValue('numeric_attributes', '数值属性')
  168. ->help('用于计算的属性,如:宝箱掉落物品数量范围等');
  169. $form->datetime('global_expire_at', '全局过期时间')
  170. ->help('所有该物品的全局过期时间,为空表示永不过期');
  171. // 保存前回调
  172. $form->saving(function (Form $form) {
  173. // 如果是宝箱类型,确保有min_drop_count和max_drop_count属性
  174. if ($form->type == ITEM_TYPE::OPENABLE) {
  175. $numericAttributes = $form->numeric_attributes ?: [];
  176. if (!isset($numericAttributes['min_drop_count'])) {
  177. $numericAttributes['min_drop_count'] = 1;
  178. }
  179. if (!isset($numericAttributes['max_drop_count'])) {
  180. $numericAttributes['max_drop_count'] = 1;
  181. }
  182. $form->numeric_attributes = $numericAttributes;
  183. }
  184. });
  185. return $form;
  186. });
  187. }
  188. }