ItemController.php 9.5 KB

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