| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- namespace App\Module\GameItems\AdminControllers;
- use App\Module\GameItems\Models\ItemCategory;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Layout\Row;
- use Dcat\Admin\Tree;
- use Dcat\Admin\Layout\Content;
- use Spatie\RouteAttributes\Attributes\Resource;
- use UCore\DcatAdmin\AdminController;
- #[Resource('game-items-categories', names: 'dcat.admin.game-items-categories')]
- class CategoryController extends AdminController
- {
- /**
- * 标题
- *
- * @var string
- */
- protected $title = '物品分类管理';
- /**
- * 首页
- *
- * @param Content $content
- * @return Content
- */
- public function index(Content $content)
- {
- return $content
- ->header($this->title)
- ->description('分类列表')
- ->body(function (Row $row) {
- $tree = new Tree(new ItemCategory());
- $tree->branch(function ($branch) {
- $icon = $branch['icon'] ? "<img src='{$branch['icon']}' width='30' height='30'>" : '';
- return "{$icon} {$branch['name']} ({$branch['code']})";
- });
- $row->column(12, $tree);
- });
- }
- /**
- * 列表页
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new ItemCategory(), function (Grid $grid) {
- $grid->column('id', 'ID')->sortable();
- $grid->column('name', '名称');
- $grid->column('code', '编码');
- $grid->column('icon', '图标')->image('', 50, 50);
- $grid->column('sort', '排序')->sortable();
- $grid->column('parent.name', '父分类');
- $grid->column('created_at', '创建时间');
- $grid->column('updated_at', '更新时间');
- // 筛选
- $grid->filter(function ($filter) {
- $filter->equal('id', 'ID');
- $filter->like('name', '名称');
- $filter->like('code', '编码');
- $filter->equal('parent_id', '父分类')->select(
- ItemCategory::pluck('name', 'id')
- );
- });
- });
- }
- /**
- * 详情页
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make(ItemCategory::findOrFail($id), function (Show $show) {
- $show->field('id', 'ID');
- $show->field('name', '名称');
- $show->field('code', '编码');
- $show->field('icon', '图标')->image();
- $show->field('sort', '排序');
- $show->field('parent.name', '父分类');
- $show->field('created_at', '创建时间');
- $show->field('updated_at', '更新时间');
- // 显示子分类
- $show->children('子分类', function ($children) {
- $children->resource('/admin/game-items-categories');
- $children->id('ID');
- $children->name('名称');
- $children->code('编码');
- $children->icon('图标')->image('', 50, 50);
- $children->sort('排序');
- });
- // 显示分类下的物品
- $show->items('物品', function ($items) {
- $items->resource('/admin/game-items');
- $items->id('ID');
- $items->name('名称');
- $items->icon('图标')->image('', 50, 50);
- $items->type('类型');
- $items->rarity('稀有度');
- });
- });
- }
- /**
- * 表单
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new ItemCategory(), function (Form $form) {
- $form->text('name', '名称')->required();
- $form->text('code', '编码')->required()->help('用于系统识别的唯一编码');
- $form->image('icon', '图标')
- ->uniqueName()
- ->autoUpload();
- $form->number('sort', '排序')
- ->default(0)
- ->help('数字越小越靠前');
- // 父分类选择,排除自己及其子分类
- $form->select('parent_id', '父分类')
- ->options(function () use ($form) {
- $categories = ItemCategory::all();
- $options = [0 => '无'];
- // 如果是编辑状态,需要排除自己及其子分类
- if ($form->isEditing()) {
- $id = $form->getKey();
- $childrenIds = $this->getChildrenIds($id);
- $childrenIds[] = $id;
- foreach ($categories as $category) {
- if (!in_array($category->id, $childrenIds)) {
- $options[$category->id] = $category->name;
- }
- }
- } else {
- foreach ($categories as $category) {
- $options[$category->id] = $category->name;
- }
- }
- return $options;
- })
- ->default(0);
- });
- }
- /**
- * 获取所有子分类ID
- *
- * @param int $categoryId
- * @return array
- */
- protected function getChildrenIds(int $categoryId): array
- {
- $ids = [];
- $children = ItemCategory::where('parent_id', $categoryId)->get();
- foreach ($children as $child) {
- $ids[] = $child->id;
- $ids = array_merge($ids, $this->getChildrenIds($child->id));
- }
- return $ids;
- }
- }
|