CategoryController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. $grid->column('id', 'ID')->sortable();
  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. *
  78. * @param mixed $id
  79. * @return Show
  80. */
  81. protected function detail($id)
  82. {
  83. return Show::make($id, new ItemCategory(), function (Show $show) {
  84. $helper = new ShowHelper($show, $this);
  85. $helper->field('id','ID');
  86. $show->field('name', '名称');
  87. $show->field('code', '编码');
  88. $show->field('icon', '图标')->image();
  89. $show->field('sort', '排序');
  90. $show->field('parent.name', '父分类');
  91. $show->field('created_at', '创建时间');
  92. $show->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. $items->rarity('稀有度');
  110. });
  111. return $show;
  112. });
  113. }
  114. /**
  115. * 表单
  116. *
  117. * @return Form
  118. */
  119. protected function form()
  120. {
  121. return Form::make(new ItemCategory(), function (Form $form) {
  122. $helper = new FormHelper($form, $this);
  123. $form->text('name', '名称')->required();
  124. $form->text('code', '编码')->required()->help('用于系统识别的唯一编码');
  125. $form->image('icon', '图标')
  126. ->uniqueName()
  127. ->autoUpload();
  128. $form->number('sort', '排序')
  129. ->default(0)
  130. ->help('数字越小越靠前');
  131. // 父分类选择,排除自己及其子分类
  132. $form->select('parent_id', '父分类')
  133. ->options(function () use ($form) {
  134. $categories = ItemCategory::all();
  135. $options = [0 => '无'];
  136. // 如果是编辑状态,需要排除自己及其子分类
  137. if ($form->isEditing()) {
  138. $id = $form->getKey();
  139. $childrenIds = $this->getChildrenIds($id);
  140. $childrenIds[] = $id;
  141. foreach ($categories as $category) {
  142. if (!in_array($category->id, $childrenIds)) {
  143. $options[$category->id] = $category->name;
  144. }
  145. }
  146. } else {
  147. foreach ($categories as $category) {
  148. $options[$category->id] = $category->name;
  149. }
  150. }
  151. return $options;
  152. })
  153. ->default(0);
  154. return $form;
  155. });
  156. }
  157. /**
  158. * 获取所有子分类ID
  159. *
  160. * @param int $categoryId
  161. * @return array
  162. */
  163. protected function getChildrenIds(int $categoryId): array
  164. {
  165. $ids = [];
  166. $children = ItemCategory::where('parent_id', $categoryId)->get();
  167. foreach ($children as $child) {
  168. $ids[] = $child->id;
  169. $ids = array_merge($ids, $this->getChildrenIds($child->id));
  170. }
  171. return $ids;
  172. }
  173. }