ItemController.php 9.0 KB

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