ItemController.php 13 KB

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