ItemController.php 8.5 KB

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