CategoryController.php 5.6 KB

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