ItemController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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\Models\ItemChestOpenCost;
  7. use App\Module\GameItems\Repositorys\ItemChestContentRepository;
  8. use App\Module\GameItems\Repositorys\ItemChestOpenCostRepository;
  9. use App\Module\GameItems\Repositorys\ItemRepository;
  10. use Dcat\Admin\Form;
  11. use Dcat\Admin\Grid;
  12. use Dcat\Admin\Show;
  13. use UCore\DcatAdmin\AdminController;
  14. use Spatie\RouteAttributes\Attributes\Resource;
  15. use Spatie\RouteAttributes\Attributes\Get;
  16. use Illuminate\Support\Facades\Artisan;
  17. use App\Module\GameItems\AdminControllers\Helper\FilterHelper;
  18. use App\Module\GameItems\AdminControllers\Helper\FormHelper;
  19. use App\Module\GameItems\AdminControllers\Helper\GridHelper;
  20. use App\Module\GameItems\AdminControllers\Helper\ShowHelper;
  21. use App\Module\GameItems\AdminControllers\Actions\ChestManageAction;
  22. use App\Module\GameItems\AdminControllers\Actions\DuplicateRowAction;
  23. /**
  24. * 物品管理控制器
  25. *
  26. * @package App\Module\GameItems\AdminControllers
  27. */
  28. #[Resource('game-items', names: 'dcat.admin.game-items')]
  29. class ItemController extends AdminController
  30. {
  31. /**
  32. * 标题
  33. *
  34. * @var string
  35. */
  36. protected $title = '物品管理';
  37. /**
  38. * 列表页
  39. *
  40. * @return Grid
  41. */
  42. protected function grid()
  43. {
  44. return Grid::make(new ItemRepository([ 'category' ]), function (Grid $grid) {
  45. $status = \App\Module\GameItems\AdminControllers\Tools\RefreshCheckTool::checkSyncStatus();
  46. if ($status['is_synced']) {
  47. admin_success('JSON配置表状态', $status['message']);
  48. } else {
  49. admin_warning('JSON配置表状态', $status['message']);
  50. }
  51. $grid->tools([
  52. new \App\Module\GameItems\AdminControllers\Tools\RefreshCheckTool($status['should_display']),
  53. new \App\Module\GameItems\AdminControllers\Tools\SyncItemsJsonTool($status['should_display'])
  54. ]);
  55. $helper = new GridHelper($grid, $this);
  56. $grid->column('id', 'ID')->sortable();
  57. $grid->column('name', '名称');
  58. $grid->column('category.name', '分类');
  59. $helper->columnModelCats('type');
  60. $grid->column('display_attributes', '展示属性')->display(function ($value) {
  61. if (empty($value)) {
  62. return '-';
  63. }
  64. if (is_string($value)) {
  65. $value = json_decode($value, true);
  66. }
  67. $html = '<div style="max-width: 250px; max-height: 150px; overflow: auto;">';
  68. foreach ((array)$value as $key => $val) {
  69. if (empty($val)) continue;
  70. $html .= "<div><strong>{$key}</strong>: {$val}</div>";
  71. }
  72. $html .= '</div>';
  73. return $html;
  74. });
  75. $grid->column('is_unique', '单独属性')->bool();
  76. $grid->column('max_stack', '最大堆叠');
  77. $grid->column('tradable', '可交易')->bool();
  78. $grid->column('dismantlable', '可分解')->bool();
  79. $grid->column('default_expire_seconds', '默认过期时间(秒)');
  80. $grid->column('global_expire_at', '全局过期时间');
  81. $grid->column('created_at', '创建时间');
  82. $grid->column('updated_at', '更新时间');
  83. // 添加行操作
  84. $grid->actions(function (Grid\Displayers\Actions $actions) {
  85. $actions->disableDelete();
  86. $actions->append(new \App\Module\GameItems\AdminControllers\Actions\DuplicateRowAction());
  87. // 如果是宝箱类型,添加宝箱管理按钮
  88. $actions->append(new \App\Module\GameItems\AdminControllers\Actions\ChestManageAction());
  89. $actions->append(new \App\Module\GameItems\AdminControllers\Actions\ChestCostAction());
  90. });
  91. // 筛选
  92. $grid->filter(function ($filter) {
  93. $helper = new FilterHelper($filter, $this);
  94. $helper->equal('id', 'ID');
  95. $filter->like('name', '名称');
  96. $filter->equal('category_id', '分类')->select(
  97. ItemCategory::pluck('name', 'id')
  98. );
  99. $helper->equalRadioModelCats('type', '类型');
  100. $filter->equal('is_unique', '单独属性')->radio([
  101. 1 => '是',
  102. 0 => '否',
  103. ]);
  104. $filter->equal('tradable', '可交易')->radio([
  105. 1 => '是',
  106. 0 => '否',
  107. ]);
  108. $filter->equal('dismantlable', '可分解')->radio([
  109. 1 => '是',
  110. 0 => '否',
  111. ]);
  112. });
  113. return $grid;
  114. });
  115. }
  116. /**
  117. * 详情页
  118. *
  119. * @param mixed $id
  120. * @return Show
  121. */
  122. protected function detail($id)
  123. {
  124. return Show::make($id, new ItemRepository(), function (Show $show) {
  125. $helper = new ShowHelper($show, $this);
  126. $helper->field('id', 'ID');
  127. $show->field('name', '名称');
  128. $show->field('description', '描述');
  129. $show->field('category.name', '分类');
  130. $helper->fieldModelCats('type');
  131. $show->field('display_attributes', '展示属性')->unescape()->as(function ($value) {
  132. if (empty($value)) {
  133. return '无';
  134. }
  135. if (is_string($value)) {
  136. $value = json_decode($value, true);
  137. }
  138. $html = '<table class="table table-bordered">';
  139. $html .= '<thead><tr><th>属性名</th><th>属性值</th></tr></thead>';
  140. $html .= '<tbody>';
  141. foreach ((array)$value as $key => $val) {
  142. if (empty($val)) continue;
  143. $html .= '<tr>';
  144. $html .= '<td>' . htmlspecialchars($key) . '</td>';
  145. $html .= '<td>' . htmlspecialchars((string)$val) . '</td>';
  146. $html .= '</tr>';
  147. }
  148. $html .= '</tbody></table>';
  149. return $html;
  150. });
  151. $show->field('is_unique', '单独属性')->as(function ($isUnique) {
  152. return $isUnique ? '是' : '否';
  153. });
  154. $show->field('max_stack', '最大堆叠');
  155. $show->field('sell_price', '出售价格');
  156. $show->field('tradable', '可交易')->as(function ($tradable) {
  157. return $tradable ? '是' : '否';
  158. });
  159. $show->field('dismantlable', '可分解')->as(function ($dismantlable) {
  160. return $dismantlable ? '是' : '否';
  161. });
  162. $show->field('default_expire_seconds', '默认过期时间(秒)');
  163. $helper->fieldModelCatsJson('display_attributes', '显示属性');
  164. $helper->fieldModelCatsJson('numeric_attributes', '数值属性');
  165. $show->field('global_expire_at', '全局过期时间');
  166. $show->field('created_at', '创建时间');
  167. $show->field('updated_at', '更新时间');
  168. $show->divider();
  169. // 如果是宝箱类型,显示宝箱内容
  170. $show->relation('chest_contents', '宝箱内容',
  171. function (\App\Module\GameItems\Models\Item $item) {
  172. // dd($item);
  173. $grid = new Grid(new ItemChestContentRepository([ 'chest', 'item', 'group' ]));
  174. $grid->model()->where('chest_id', $item->id);
  175. // 设置路由
  176. $grid->setResource('game-items-chest-contents');
  177. $grid->id();
  178. $grid->column('chest.name', '宝箱名称');
  179. $grid->column('item.name', '物品名称');
  180. $grid->column('group.name', '物品组名称');
  181. $grid->column('min_quantity', '最小数量');
  182. $grid->column('max_quantity', '最大数量');
  183. $grid->column('weight', '权重');
  184. $grid->disableActions();
  185. return $grid;
  186. });
  187. $show->relation('chest_costs', '宝箱消耗',
  188. function (\App\Module\GameItems\Models\Item $item) {
  189. // dd($item);
  190. $grid = new Grid(new ItemChestOpenCostRepository(['costItem']));
  191. $grid->model()->where('chest_id', $item->id);
  192. // 设置路由
  193. $grid->setResource('game-items-chest-costs');
  194. $grid->id();
  195. $grid->column('costItem.name', '物品名称');
  196. $grid->disableActions();
  197. return $grid;
  198. });
  199. return $show;
  200. });
  201. }
  202. /**
  203. * 表单
  204. *
  205. * @return Form
  206. */
  207. protected function form()
  208. {
  209. return Form::make(new ItemRepository(), function (Form $form) {
  210. $helper = new \App\Module\GameItems\AdminControllers\Helper\FormHelper($form, $this);
  211. $helper->text('name')->required();
  212. $form->textarea('description', '描述');
  213. $form->select('category_id', '分类')
  214. ->options(ItemCategory::pluck('name', 'id'))
  215. ->required();
  216. $helper->selectOptionCast('type', '类型');
  217. $form->switch('is_unique', '单独属性')
  218. ->default(false);
  219. $form->number('max_stack', '最大堆叠')
  220. ->default(1)
  221. ->min(1)
  222. ->help('0表示无限堆叠');
  223. $form->number('sell_price', '出售价格')
  224. ->default(0)
  225. ->min(0);
  226. $form->switch('tradable', '可交易')
  227. ->default(true);
  228. $form->switch('dismantlable', '可分解')
  229. ->default(true);
  230. $form->number('default_expire_seconds', '默认过期时间(秒)')
  231. ->default(0)
  232. ->help('0表示永不过期');
  233. $helper->embedsCats('display_attributes', '显示属性')
  234. ->help('用于显示的属性,如:攻击力、防御力等');
  235. $helper->embedsCats('numeric_attributes', '数值属性')
  236. ->help('用于计算的属性,如:宝箱掉落物品数量范围等');
  237. $form->datetime('global_expire_at', '全局过期时间')
  238. ->help('所有该物品的全局过期时间,为空表示永不过期');
  239. // 保存前回调
  240. $form->saving(function (Form $form) {
  241. // 如果是宝箱类型,确保有min_drop_count和max_drop_count属性
  242. if ($form->type == ITEM_TYPE::CHEST) {
  243. $numericAttributes = $form->numeric_attributes ?: [];
  244. if (!isset($numericAttributes['min_drop_count'])) {
  245. $numericAttributes['min_drop_count'] = 1;
  246. }
  247. if (!isset($numericAttributes['max_drop_count'])) {
  248. $numericAttributes['max_drop_count'] = 1;
  249. }
  250. $form->numeric_attributes = $numericAttributes;
  251. }
  252. });
  253. return $form;
  254. });
  255. }
  256. }