Your Name 8 luni în urmă
părinte
comite
764a70f776
1 a modificat fișierele cu 159 adăugiri și 144 ștergeri
  1. 159 144
      app/Module/GameItems/AdminControllers/ItemController.php

+ 159 - 144
app/Module/GameItems/AdminControllers/ItemController.php

@@ -11,10 +11,15 @@ use Dcat\Admin\Grid;
 use Dcat\Admin\Show;
 use UCore\DcatAdmin\AdminController;
 use Spatie\RouteAttributes\Attributes\Resource;
+use UCore\DcatAdmin\FilterHelper;
+use UCore\DcatAdmin\FormHelper;
+use UCore\DcatAdmin\GridHelper;
+use UCore\DcatAdmin\ShowHelper;
 
 #[Resource('game-items', names: 'dcat.admin.game-items')]
 class ItemController extends AdminController
 {
+
     /**
      * 标题
      *
@@ -29,54 +34,55 @@ class ItemController extends AdminController
      */
     protected function grid()
     {
-        $grid = new Grid(new Item());
+        return Grid::make(new Item(), function (Grid $grid) {
+            $helper = new GridHelper($grid, $this);
+            $grid->column('id', 'ID')->sortable();
+            $grid->column('name', '名称');
+            $grid->column('category.name', '分类');
+            $helper->columnModelCats('type');
+            $helper->columnModelCats('rarity');
 
-        $grid->column('id', 'ID')->sortable();
-        $grid->column('name', '名称');
-        $grid->column('category.name', '分类');
-        $grid->column('type', '类型')->display(function ($type) {
-            return ITEM_TYPE::getName($type);
-        });
-        $grid->column('rarity', '稀有度')->display(function ($rarity) {
-            return ITEM_RARITY::getName($rarity);
-        })->label(function ($rarity) {
-            $color = ITEM_RARITY::getColor($rarity);
-            return "<span style='color:{$color}'>" . ITEM_RARITY::getName($rarity) . "</span>";
-        });
-        $grid->column('icon', '图标')->image('', 50, 50);
-        $grid->column('is_unique', '单独属性')->bool();
-        $grid->column('max_stack', '最大堆叠');
-        $grid->column('tradable', '可交易')->bool();
-        $grid->column('dismantlable', '可分解')->bool();
-        $grid->column('default_expire_seconds', '默认过期时间(秒)');
-        $grid->column('global_expire_at', '全局过期时间');
-        $grid->column('created_at', '创建时间');
-        $grid->column('updated_at', '更新时间');
-
-        // 筛选
-        $grid->filter(function ($filter) {
-            $filter->equal('id', 'ID');
-            $filter->like('name', '名称');
-            $filter->equal('category_id', '分类')->select(
-                ItemCategory::pluck('name', 'id')
-            );
-            $filter->equal('type', '类型')->select(ITEM_TYPE::all());
-            $filter->equal('rarity', '稀有度')->select(ITEM_RARITY::all());
-            $filter->equal('is_unique', '单独属性')->radio([
-                1 => '是',
-                0 => '否',
-            ]);
-            $filter->equal('tradable', '可交易')->radio([
-                1 => '是',
-                0 => '否',
-            ]);
-            $filter->equal('dismantlable', '可分解')->radio([
-                1 => '是',
-                0 => '否',
-            ]);
+
+            $grid->column('icon', '图标')->image('', 50, 50);
+            $grid->column('is_unique', '单独属性')->bool();
+            $grid->column('max_stack', '最大堆叠');
+            $grid->column('tradable', '可交易')->bool();
+            $grid->column('dismantlable', '可分解')->bool();
+            $grid->column('default_expire_seconds', '默认过期时间(秒)');
+            $grid->column('global_expire_at', '全局过期时间');
+            $grid->column('created_at', '创建时间');
+            $grid->column('updated_at', '更新时间');
+
+            // 筛选
+            $grid->filter(function ($filter) {
+                $helper = new FilterHelper($filter, $this);
+                $helper->equal('id','ID');
+
+                $filter->like('name', '名称');
+                $filter->equal('category_id', '分类')->select(
+                    ItemCategory::pluck('name', 'id')
+                );
+                $helper->equalRadioModelCats('type','类型');
+
+                $filter->equal('rarity', '稀有度')->select(ITEM_RARITY::all());
+                $filter->equal('is_unique', '单独属性')->radio([
+                                                                   1 => '是',
+                                                                   0 => '否',
+                                                               ]);
+                $filter->equal('tradable', '可交易')->radio([
+                                                                1 => '是',
+                                                                0 => '否',
+                                                            ]);
+                $filter->equal('dismantlable', '可分解')->radio([
+                                                                    1 => '是',
+                                                                    0 => '否',
+                                                                ]);
+            });
+
+            return $grid;
         });
 
-        return $grid;
+
     }
 
     /**
@@ -87,54 +93,57 @@ class ItemController extends AdminController
      */
     protected function detail($id)
     {
-        $show = new Show(Item::findOrFail($id));
-
-        $show->field('id', 'ID');
-        $show->field('name', '名称');
-        $show->field('description', '描述');
-        $show->field('category.name', '分类');
-        $show->field('type', '类型')->as(function ($type) {
-            return ITEM_TYPE::getName($type);
-        });
-        $show->field('rarity', '稀有度')->as(function ($rarity) {
-            return ITEM_RARITY::getName($rarity);
-        });
-        $show->field('icon', '图标')->image();
-        $show->field('is_unique', '单独属性')->as(function ($isUnique) {
-            return $isUnique ? '是' : '否';
-        });
-        $show->field('max_stack', '最大堆叠');
-        $show->field('sell_price', '出售价格');
-        $show->field('tradable', '可交易')->as(function ($tradable) {
-            return $tradable ? '是' : '否';
-        });
-        $show->field('dismantlable', '可分解')->as(function ($dismantlable) {
-            return $dismantlable ? '是' : '否';
-        });
-        $show->field('default_expire_seconds', '默认过期时间(秒)');
-        $show->field('display_attributes', '显示属性')->json();
-        $show->field('numeric_attributes', '数值属性')->json();
-        $show->field('global_expire_at', '全局过期时间');
-        $show->field('created_at', '创建时间');
-        $show->field('updated_at', '更新时间');
-
-        // 如果是宝箱类型,显示宝箱内容
-        if ($show->getModel()->type == ITEM_TYPE::OPENABLE) {
-            $show->chestContents('宝箱内容', function ($chestContents) {
-                $chestContents->resource('/admin/game-items-chest-contents');
-                $chestContents->id('ID');
-                $chestContents->item()->name('物品名称');
-                $chestContents->group()->name('物品组名称');
-                $chestContents->min_quantity('最小数量');
-                $chestContents->max_quantity('最大数量');
-                $chestContents->weight('权重');
-                $chestContents->allow_duplicate('允许重复')->bool();
-                $chestContents->pity_count('保底次数');
-                $chestContents->pity_weight_factor('保底权重因子');
+        return Show::make($id, new Item(), function (Show $show) {
+            $helper = new ShowHelper($show, $this);
+            $helper->field('id','ID');
+            $show->field('name', '名称');
+            $show->field('description', '描述');
+            $show->field('category.name', '分类');
+            $show->field('type', '类型')->as(function ($type) {
+                return ITEM_TYPE::getName($type);
+            });
+            $show->field('rarity', '稀有度')->as(function ($rarity) {
+                return ITEM_RARITY::getName($rarity);
+            });
+            $show->field('icon', '图标')->image();
+            $show->field('is_unique', '单独属性')->as(function ($isUnique) {
+                return $isUnique ? '是' : '否';
             });
-        }
+            $show->field('max_stack', '最大堆叠');
+            $show->field('sell_price', '出售价格');
+            $show->field('tradable', '可交易')->as(function ($tradable) {
+                return $tradable ? '是' : '否';
+            });
+            $show->field('dismantlable', '可分解')->as(function ($dismantlable) {
+                return $dismantlable ? '是' : '否';
+            });
+            $show->field('default_expire_seconds', '默认过期时间(秒)');
+            $show->field('display_attributes', '显示属性')->json();
+            $show->field('numeric_attributes', '数值属性')->json();
+            $show->field('global_expire_at', '全局过期时间');
+            $show->field('created_at', '创建时间');
+            $show->field('updated_at', '更新时间');
+
+            // 如果是宝箱类型,显示宝箱内容
+            if ($show->getModel()->type == ITEM_TYPE::OPENABLE) {
+                $show->chestContents('宝箱内容', function ($chestContents) {
+                    $chestContents->resource('/admin/game-items-chest-contents');
+                    $chestContents->id('ID');
+                    $chestContents->item()->name('物品名称');
+                    $chestContents->group()->name('物品组名称');
+                    $chestContents->min_quantity('最小数量');
+                    $chestContents->max_quantity('最大数量');
+                    $chestContents->weight('权重');
+                    $chestContents->allow_duplicate('允许重复')->bool();
+                    $chestContents->pity_count('保底次数');
+                    $chestContents->pity_weight_factor('保底权重因子');
+                });
+            }
+
+            return $show;
+        });
+
 
-        return $show;
     }
 
     /**
@@ -144,61 +153,67 @@ class ItemController extends AdminController
      */
     protected function form()
     {
-        $form = new Form(new Item());
-
-        $form->text('name', '名称')->required();
-        $form->textarea('description', '描述');
-        $form->select('category_id', '分类')
-            ->options(ItemCategory::pluck('name', 'id'))
-            ->required();
-        $form->select('type', '类型')
-            ->options(ITEM_TYPE::all())
-            ->required();
-        $form->select('rarity', '稀有度')
-            ->options(ITEM_RARITY::all())
-            ->required();
-        $form->image('icon', '图标')
-            ->uniqueName()
-            ->autoUpload()
-            ->required();
-        $form->switch('is_unique', '单独属性')
-            ->default(false);
-        $form->number('max_stack', '最大堆叠')
-            ->default(1)
-            ->min(1)
-            ->help('0表示无限堆叠');
-        $form->number('sell_price', '出售价格')
-            ->default(0)
-            ->min(0);
-        $form->switch('tradable', '可交易')
-            ->default(true);
-        $form->switch('dismantlable', '可分解')
-            ->default(true);
-        $form->number('default_expire_seconds', '默认过期时间(秒)')
-            ->default(0)
-            ->help('0表示永不过期');
-        $form->keyValue('display_attributes', '显示属性')
-            ->help('用于显示的属性,如:攻击力、防御力等');
-        $form->keyValue('numeric_attributes', '数值属性')
-            ->help('用于计算的属性,如:宝箱掉落物品数量范围等');
-        $form->datetime('global_expire_at', '全局过期时间')
-            ->help('所有该物品的全局过期时间,为空表示永不过期');
-
-        // 保存前回调
-        $form->saving(function (Form $form) {
-            // 如果是宝箱类型,确保有min_drop_count和max_drop_count属性
-            if ($form->type == ITEM_TYPE::OPENABLE) {
-                $numericAttributes = $form->numeric_attributes ?: [];
-                if (!isset($numericAttributes['min_drop_count'])) {
-                    $numericAttributes['min_drop_count'] = 1;
-                }
-                if (!isset($numericAttributes['max_drop_count'])) {
-                    $numericAttributes['max_drop_count'] = 1;
+        return Form::make(new Item(), function (Form $form) {
+            $helper = new FormHelper($form,$this);
+            $helper->text('name')->required();
+
+            $form->textarea('description', '描述');
+            $form->select('category_id', '分类')
+                ->options(ItemCategory::pluck('name', 'id'))
+                ->required();
+            $form->select('type', '类型')
+                ->options(ITEM_TYPE::all())
+                ->required();
+            $form->select('rarity', '稀有度')
+                ->options(ITEM_RARITY::all())
+                ->required();
+            $form->image('icon', '图标')
+                ->uniqueName()
+                ->autoUpload()
+                ->required();
+            $form->switch('is_unique', '单独属性')
+                ->default(false);
+            $form->number('max_stack', '最大堆叠')
+                ->default(1)
+                ->min(1)
+                ->help('0表示无限堆叠');
+            $form->number('sell_price', '出售价格')
+                ->default(0)
+                ->min(0);
+            $form->switch('tradable', '可交易')
+                ->default(true);
+            $form->switch('dismantlable', '可分解')
+                ->default(true);
+            $form->number('default_expire_seconds', '默认过期时间(秒)')
+                ->default(0)
+                ->help('0表示永不过期');
+            $form->keyValue('display_attributes', '显示属性')
+                ->help('用于显示的属性,如:攻击力、防御力等');
+            $form->keyValue('numeric_attributes', '数值属性')
+                ->help('用于计算的属性,如:宝箱掉落物品数量范围等');
+            $form->datetime('global_expire_at', '全局过期时间')
+                ->help('所有该物品的全局过期时间,为空表示永不过期');
+
+            // 保存前回调
+            $form->saving(function (Form $form) {
+                // 如果是宝箱类型,确保有min_drop_count和max_drop_count属性
+                if ($form->type == ITEM_TYPE::OPENABLE) {
+                    $numericAttributes = $form->numeric_attributes ?: [];
+                    if (!isset($numericAttributes['min_drop_count'])) {
+                        $numericAttributes['min_drop_count'] = 1;
+                    }
+                    if (!isset($numericAttributes['max_drop_count'])) {
+                        $numericAttributes['max_drop_count'] = 1;
+                    }
+                    $form->numeric_attributes = $numericAttributes;
                 }
-                $form->numeric_attributes = $numericAttributes;
-            }
+            });
+
+            return $form;
         });
 
-        return $form;
+
+
     }
+
 }