ItemController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 App\Module\GameItems\AdminControllers\Helper\FilterHelper;
  15. use App\Module\GameItems\AdminControllers\Helper\FormHelper;
  16. use App\Module\GameItems\AdminControllers\Helper\GridHelper;
  17. use App\Module\GameItems\AdminControllers\Helper\ShowHelper;
  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. * 标题
  29. *
  30. * @var string
  31. */
  32. protected $title = '物品管理';
  33. /**
  34. * 列表页
  35. *
  36. * @return Grid
  37. */
  38. protected function grid()
  39. {
  40. return Grid::make(new ItemRepository([ 'category' ]), function (Grid $grid) {
  41. $status = \App\Module\GameItems\AdminControllers\Tools\RefreshCheckTool::checkSyncStatus();
  42. if ($status['is_synced']) {
  43. admin_success('JSON配置表状态', $status['message']);
  44. } else {
  45. admin_warning('JSON配置表状态', $status['message']);
  46. }
  47. $grid->tools([
  48. new \App\Module\GameItems\AdminControllers\Tools\RefreshCheckTool($status['should_display']),
  49. new \App\Module\GameItems\AdminControllers\Tools\SyncItemsJsonTool($status['should_display'])
  50. ]);
  51. $helper = new GridHelper($grid, $this);
  52. $grid->column('id', 'ID')->sortable();
  53. $grid->column('name', '名称');
  54. $grid->column('category.name', '分类');
  55. $helper->columnModelCats('type');
  56. $grid->column('display_attributes', '展示属性')->display(function ($value) {
  57. if (empty($value)) {
  58. return '-';
  59. }
  60. if (is_string($value)) {
  61. $value = json_decode($value, true);
  62. }
  63. $html = '<div style="max-width: 250px; max-height: 150px; overflow: auto;">';
  64. foreach ((array)$value as $key => $val) {
  65. if (empty($val)) continue;
  66. $html .= "<div><strong>{$key}</strong>: {$val}</div>";
  67. }
  68. $html .= '</div>';
  69. return $html;
  70. });
  71. // 添加数值属性列,只显示有效的属性(非0值),并使用注释作为键名
  72. $grid->column('numeric_attributes', '数值属性')->display(function ($value) {
  73. if (empty($value)) {
  74. return '-';
  75. }
  76. if (is_string($value)) {
  77. $value = json_decode($value, true);
  78. }
  79. // 属性名到注释的映射
  80. $attributeNames = [
  81. 'crop_growth_time' => '减少作物生长时间',
  82. 'pet_power' => '增加宠物体力',
  83. 'reward_group_id' => '随机奖励物品组',
  84. 'pet_exp' => '增加宠物经验',
  85. 'fram_pesticide_rate' => '除虫概率(%)',
  86. 'fram_drought_rate' => '解决干旱概率(%)',
  87. 'fram_weedicide_rate' => '除草概率(%)'
  88. ];
  89. $html = '<div style="max-width: 250px; max-height: 150px; overflow: auto;">';
  90. foreach ((array)$value as $key => $val) {
  91. if (empty($val) || $val === 0) continue; // 过滤掉空值和0值
  92. $name = $attributeNames[$key] ?? $key;
  93. $html .= "<div><strong>{$name}</strong>: {$val}</div>";
  94. }
  95. $html .= '</div>';
  96. return $html;
  97. });
  98. $grid->column('is_unique', '单独属性')->bool();
  99. $grid->column('max_stack', '最大堆叠');
  100. $grid->column('tradable', '可交易')->bool();
  101. $grid->column('dismantlable', '可分解')->bool();
  102. $grid->column('default_expire_seconds', '默认过期时间(秒)');
  103. $grid->column('global_expire_at', '全局过期时间');
  104. $grid->column('created_at', '创建时间');
  105. $grid->column('updated_at', '更新时间');
  106. // 添加行操作
  107. $grid->actions(function (Grid\Displayers\Actions $actions) {
  108. $actions->disableDelete();
  109. $actions->append(new \App\Module\GameItems\AdminControllers\Actions\DuplicateRowAction());
  110. // 如果是宝箱类型,添加宝箱管理按钮
  111. $actions->append(new \App\Module\GameItems\AdminControllers\Actions\ChestNewManageAction());
  112. });
  113. // 筛选
  114. $grid->filter(function (Grid\Filter $filter) {
  115. $helper = new FilterHelper($filter, $this);
  116. $helper->equal('id', 'ID');
  117. $filter->like('name', '名称');
  118. $filter->equal('category_id', '分类')->select(
  119. ItemCategory::pluck('name', 'id')
  120. );
  121. $helper->equalRadioModelCats('type', '类型');
  122. $filter->equal('is_unique', '单独属性')->radio([
  123. 1 => '是',
  124. 0 => '否',
  125. ]);
  126. $filter->equal('tradable', '可交易')->radio([
  127. 1 => '是',
  128. 0 => '否',
  129. ]);
  130. $filter->equal('dismantlable', '可分解')->radio([
  131. 1 => '是',
  132. 0 => '否',
  133. ]);
  134. });
  135. return $grid;
  136. });
  137. }
  138. /**
  139. * 详情页
  140. *
  141. * @param mixed $id
  142. * @return Show
  143. */
  144. protected function detail($id)
  145. {
  146. return Show::make($id, new ItemRepository(), function (Show $show) {
  147. $helper = new ShowHelper($show, $this);
  148. $helper->field('id', 'ID');
  149. $show->field('name', '名称');
  150. $show->field('description', '描述');
  151. $show->field('category.name', '分类');
  152. $helper->fieldModelCats('type');
  153. $show->field('display_attributes', '展示属性')->unescape()->as(function ($value) {
  154. if (empty($value)) {
  155. return '无';
  156. }
  157. if (is_string($value)) {
  158. $value = json_decode($value, true);
  159. }
  160. $html = '<table class="table table-bordered">';
  161. $html .= '<thead><tr><th>属性名</th><th>属性值</th></tr></thead>';
  162. $html .= '<tbody>';
  163. foreach ((array)$value as $key => $val) {
  164. if (empty($val)) continue;
  165. $html .= '<tr>';
  166. $html .= '<td>' . htmlspecialchars($key) . '</td>';
  167. $html .= '<td>' . htmlspecialchars((string)$val) . '</td>';
  168. $html .= '</tr>';
  169. }
  170. $html .= '</tbody></table>';
  171. return $html;
  172. });
  173. $show->field('is_unique', '单独属性')->as(function ($isUnique) {
  174. return $isUnique ? '是' : '否';
  175. });
  176. $show->field('max_stack', '最大堆叠');
  177. $show->field('sell_price', '出售价格');
  178. $show->field('tradable', '可交易')->as(function ($tradable) {
  179. return $tradable ? '是' : '否';
  180. });
  181. $show->field('dismantlable', '可分解')->as(function ($dismantlable) {
  182. return $dismantlable ? '是' : '否';
  183. });
  184. $show->field('default_expire_seconds', '默认过期时间(秒)');
  185. $helper->fieldModelCatsJson('display_attributes', '显示属性');
  186. $helper->fieldModelCatsJson('numeric_attributes', '数值属性');
  187. $show->field('global_expire_at', '全局过期时间');
  188. $show->field('created_at', '创建时间');
  189. $show->field('updated_at', '更新时间');
  190. $show->divider();
  191. // 如果是宝箱类型,显示宝箱内容
  192. // 宝箱配置(新系统)
  193. if ($show->model()->type == ITEM_TYPE::CHEST) {
  194. $config = $show->model()->chestConfig()->first();
  195. $show->field('chest_config_status', '宝箱配置状态')->as(function () use ($config) {
  196. return $config ? '已配置新系统' : '未配置';
  197. });
  198. $show->field('consume_group_name', '消耗组')->as(function () use ($config) {
  199. return $config && $config->consumeGroup ? $config->consumeGroup->name : '无';
  200. });
  201. $show->field('reward_group_name', '奖励组')->as(function () use ($config) {
  202. return $config && $config->rewardGroup ? $config->rewardGroup->name : '无';
  203. });
  204. $show->field('condition_group_name', '条件组')->as(function () use ($config) {
  205. return $config && $config->conditionGroup ? $config->conditionGroup->name : '无';
  206. });
  207. }
  208. return $show;
  209. });
  210. }
  211. /**
  212. * 表单
  213. *
  214. * @return Form
  215. */
  216. protected function form()
  217. {
  218. return Form::make(new ItemRepository(), function (Form $form) {
  219. $helper = new \App\Module\GameItems\AdminControllers\Helper\FormHelper($form, $this);
  220. $helper->text('name')->required();
  221. $form->textarea('description', '描述');
  222. $form->select('category_id', '分类')
  223. ->options(ItemCategory::pluck('name', 'id'))
  224. ->required();
  225. $helper->selectOptionCast('type', '类型');
  226. $form->switch('is_unique', '单独属性')
  227. ->default(false);
  228. $form->number('max_stack', '最大堆叠')
  229. ->default(0)
  230. ->min(0)
  231. ->help('0表示无限堆叠,大于0表示限制堆叠数量');
  232. $form->number('sell_price', '出售价格')
  233. ->default(0)
  234. ->min(0);
  235. $form->switch('tradable', '可交易')
  236. ->default(true);
  237. $form->switch('dismantlable', '可分解')
  238. ->default(true);
  239. $form->number('default_expire_seconds', '默认过期时间(秒)')
  240. ->default(0)
  241. ->help('0表示永不过期');
  242. $helper->embedsCats('display_attributes', '显示属性')
  243. ->help('用于显示的属性,如:攻击力、防御力等');
  244. $helper->embedsCats('numeric_attributes', '数值属性')
  245. ->help('用于计算的属性,如:宝箱掉落物品数量范围等');
  246. $form->datetime('global_expire_at', '全局过期时间')
  247. ->help('所有该物品的全局过期时间,为空表示永不过期');
  248. // 保存前回调
  249. $form->saving(function (Form $form) {
  250. // 如果是宝箱类型,确保有min_drop_count和max_drop_count属性
  251. if ($form->type == ITEM_TYPE::CHEST) {
  252. $numericAttributes = $form->numeric_attributes ?: [];
  253. if (!isset($numericAttributes['min_drop_count'])) {
  254. $numericAttributes['min_drop_count'] = 1;
  255. }
  256. if (!isset($numericAttributes['max_drop_count'])) {
  257. $numericAttributes['max_drop_count'] = 1;
  258. }
  259. $form->numeric_attributes = $numericAttributes;
  260. }
  261. });
  262. return $form;
  263. });
  264. }
  265. }