ItemController.php 8.4 KB

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