CategoryController.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers;
  3. use App\Module\GameItems\Models\ItemCategory;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Grid;
  6. use Dcat\Admin\Show;
  7. use Dcat\Admin\Layout\Row;
  8. use Dcat\Admin\Tree;
  9. use Dcat\Admin\Layout\Content;
  10. use Spatie\RouteAttributes\Attributes\Resource;
  11. use UCore\DcatAdmin\AdminController;
  12. use UCore\DcatAdmin\FilterHelper;
  13. use UCore\DcatAdmin\FormHelper;
  14. use UCore\DcatAdmin\GridHelper;
  15. use UCore\DcatAdmin\ShowHelper;
  16. #[Resource('game-items-categories', names: 'dcat.admin.game-items-categories')]
  17. class CategoryController extends AdminController
  18. {
  19. /**
  20. * 标题
  21. *
  22. * @var string
  23. */
  24. protected $title = '物品分类管理';
  25. /**
  26. * 首页
  27. *
  28. * @param Content $content
  29. * @return Content
  30. */
  31. public function index(Content $content)
  32. {
  33. return $content
  34. ->header($this->title)
  35. ->description('分类列表')
  36. ->body(function (Row $row) {
  37. $tree = new Tree(new ItemCategory());
  38. $tree->branch(function ($branch) {
  39. $icon = $branch['icon'] ? "<img src='{$branch['icon']}' width='30' height='30'>" : '';
  40. return "{$icon} {$branch['name']} ({$branch['code']})";
  41. });
  42. $row->column(12, $tree);
  43. });
  44. }
  45. /**
  46. * 列表页
  47. *
  48. * @return Grid
  49. */
  50. protected function grid()
  51. {
  52. return Grid::make(new ItemCategory(), function (Grid $grid) {
  53. $helper = new GridHelper($grid, $this);
  54. $helper->columnId();
  55. $grid->column('name', '名称');
  56. $grid->column('code', '编码');
  57. $grid->column('icon', '图标')->image('', 50, 50);
  58. $grid->column('sort', '排序')->sortable();
  59. $grid->column('parent.name', '父分类');
  60. $grid->column('created_at', '创建时间');
  61. $grid->column('updated_at', '更新时间');
  62. // 筛选
  63. $grid->filter(function ($filter) {
  64. $helper = new FilterHelper($filter, $this);
  65. $helper->equal('id','ID');
  66. $filter->like('name', '名称');
  67. $filter->like('code', '编码');
  68. $filter->equal('parent_id', '父分类')->select(
  69. ItemCategory::pluck('name', 'id')
  70. );});
  71. });
  72. return $grid;
  73. }
  74. /**
  75. * 详情页
  76. *
  77. * @param mixed $id
  78. * @return Show
  79. */
  80. protected function detail($id)
  81. {
  82. return Show::make(ItemCategory::findOrFail($id), function (Show $show) {
  83. $helper = new ShowHelper($show, $this);
  84. $helper->field('id', 'ID');
  85. $helper->field('name', '名称');
  86. $helper->field('code', '编码');
  87. $show->field('icon', '图标')->image();
  88. $helper->field('sort', '排序');
  89. $show->field('parent.name', '父分类');
  90. $helper->field('created_at', '创建时间');
  91. $helper->field('updated_at', '更新时间');
  92. // 显示子分类
  93. $show->children('子分类', function ($children) {
  94. $children->resource('/admin/game-items-categories');
  95. $children->id('ID');
  96. $children->name('名称');
  97. $children->code('编码');
  98. $children->icon('图标')->image('', 50, 50);
  99. $children->sort('排序');
  100. });
  101. // 显示分类下的物品
  102. $show->items('物品', function ($items) {
  103. $items->resource('/admin/game-items');
  104. $items->id('ID');
  105. $items->name('名称');
  106. $items->icon('图标')->image('', 50, 50);
  107. $items->type('类型');
  108. $items->rarity('稀有度');
  109. });
  110. });
  111. }
  112. /**
  113. * 表单
  114. *
  115. * @return Form
  116. */
  117. protected function form()
  118. {
  119. return Form::make(new ItemCategory(), function (Form $form) {
  120. $helper = new FormHelper($form, $this);
  121. $helper->text('name')->required();
  122. $helper->text('code')->required()->help('用于系统识别的唯一编码');
  123. $form->image('icon', '图标')
  124. ->uniqueName()
  125. ->autoUpload();
  126. $helper->number('sort')
  127. ->default(0)
  128. ->help('数字越小越靠前');
  129. // 父分类选择,排除自己及其子分类
  130. $form->select('parent_id', '父分类')
  131. ->options(function () use ($form) {
  132. $categories = ItemCategory::all();
  133. $options = [0 => '无'];
  134. // 如果是编辑状态,需要排除自己及其子分类
  135. if ($form->isEditing()) {
  136. $id = $form->getKey();
  137. $childrenIds = $this->getChildrenIds($id);
  138. $childrenIds[] = $id;
  139. foreach ($categories as $category) {
  140. if (!in_array($category->id, $childrenIds)) {
  141. $options[$category->id] = $category->name;
  142. }
  143. }
  144. } else {
  145. foreach ($categories as $category) {
  146. $options[$category->id] = $category->name;
  147. }
  148. }
  149. return $options;
  150. })
  151. ->default(0);
  152. });
  153. }
  154. /**
  155. * 获取所有子分类ID
  156. *
  157. * @param int $categoryId
  158. * @return array
  159. */
  160. protected function getChildrenIds(int $categoryId): array
  161. {
  162. $ids = [];
  163. $children = ItemCategory::where('parent_id', $categoryId)->get();
  164. foreach ($children as $child) {
  165. $ids[] = $child->id;
  166. $ids = array_merge($ids, $this->getChildrenIds($child->id));
  167. }
  168. return $ids;
  169. }
  170. }