CategoryController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers;
  3. use App\Module\GameItems\Repositorys\ItemCategoryRepository;
  4. use App\Module\GameItems\Models\ItemCategory;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Show;
  8. use Dcat\Admin\Layout\Row;
  9. use Dcat\Admin\Tree;
  10. use Dcat\Admin\Layout\Content;
  11. use Spatie\RouteAttributes\Attributes\Resource;
  12. use UCore\DcatAdmin\AdminController;
  13. use UCore\DcatAdmin\FilterHelper;
  14. use UCore\DcatAdmin\FormHelper;
  15. use UCore\DcatAdmin\GridHelper;
  16. use UCore\DcatAdmin\ShowHelper;
  17. #[Resource('game-items-categories', names: 'dcat.admin.game-items-categories')]
  18. class CategoryController extends AdminController
  19. {
  20. /**
  21. * 标题
  22. *
  23. * @var string
  24. */
  25. protected $title = '物品分类管理';
  26. /**
  27. * 首页
  28. *
  29. * @param Content $content
  30. * @return Content
  31. */
  32. public function index(Content $content)
  33. {
  34. return $content
  35. ->header($this->title)
  36. ->description('分类列表')
  37. ->body(function (Row $row) {
  38. $tree = new Tree(new ItemCategoryRepository());
  39. $tree->branch(function ($branch) {
  40. $icon = $branch['icon'] ? "<img src='{$branch['icon']}' width='30' height='30'>" : '';
  41. return "{$icon} {$branch['name']} ({$branch['code']})";
  42. });
  43. $row->column(12, $tree);
  44. });
  45. }
  46. /**
  47. * 列表页
  48. *
  49. * @return Grid
  50. */
  51. protected function grid()
  52. {
  53. return Grid::make(new ItemCategoryRepository(), function (Grid $grid) {
  54. $helper = new GridHelper($grid, $this);
  55. $helper->columnId();
  56. $grid->column('name', '名称');
  57. $grid->column('code', '编码');
  58. $grid->column('icon', '图标')->image('', 50, 50);
  59. $grid->column('sort', '排序')->sortable();
  60. $grid->column('parent.name', '父分类');
  61. $grid->column('created_at', '创建时间');
  62. $grid->column('updated_at', '更新时间');
  63. // 筛选
  64. $grid->filter(function ($filter) {
  65. $helper = new FilterHelper($filter, $this);
  66. $helper->equal('id','ID');
  67. $filter->like('name', '名称');
  68. $filter->like('code', '编码');
  69. $filter->equal('parent_id', '父分类')->select(
  70. (new ItemCategoryRepository())->pluck('name', 'id')
  71. );});
  72. });
  73. return $grid;
  74. }
  75. /**
  76. * 详情页
  77. *
  78. * @param mixed $id
  79. * @return Show
  80. */
  81. protected function detail($id)
  82. {
  83. return Show::make((new ItemCategoryRepository())->findOrFail($id), function (Show $show) {
  84. $helper = new ShowHelper($show, $this);
  85. $helper->field('id', 'ID');
  86. $helper->field('name', '名称');
  87. $helper->field('code', '编码');
  88. $show->field('icon', '图标')->image();
  89. $helper->field('sort', '排序');
  90. $show->field('parent.name', '父分类');
  91. $helper->field('created_at', '创建时间');
  92. $helper->field('updated_at', '更新时间');
  93. // 显示子分类
  94. $show->children('子分类', function ($children) {
  95. $children->resource('/admin/game-items-categories');
  96. $children->id('ID');
  97. $children->name('名称');
  98. $children->code('编码');
  99. $children->icon('图标')->image('', 50, 50);
  100. $children->sort('排序');
  101. });
  102. // 显示分类下的物品
  103. $show->items('物品', function ($items) {
  104. $items->resource('/admin/game-items');
  105. $items->id('ID');
  106. $items->name('名称');
  107. $items->icon('图标')->image('', 50, 50);
  108. $items->type('类型');
  109. });
  110. });
  111. }
  112. /**
  113. * 表单
  114. *
  115. * @return Form
  116. */
  117. protected function form()
  118. {
  119. return Form::make(new ItemCategoryRepository(), 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 = (new ItemCategoryRepository())->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 = (new ItemCategoryRepository())->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. }