ItemController.php 8.9 KB

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