ItemController.php 13 KB

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