| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- namespace App\Module\GameItems\AdminControllers;
- use App\Module\GameItems\AdminControllers\Actions\BatchActivateAction;
- use App\Module\GameItems\Enums\CHEST_COST_TYPE;
- use App\Module\GameItems\Models\Item;
- use App\Module\GameItems\Models\ItemChestOpenCost;
- use App\Module\GameItems\Repositorys\ItemChestOpenCostRepository;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use UCore\DcatAdmin\AdminController;
- use App\Module\GameItems\AdminControllers\Helper\FilterHelper;
- use App\Module\GameItems\AdminControllers\Helper\FormHelper;
- use App\Module\GameItems\AdminControllers\Helper\GridHelper;
- use App\Module\GameItems\AdminControllers\Helper\ShowHelper;
- use Dcat\Admin\Layout\Content;
- use Spatie\RouteAttributes\Attributes\Resource;
- /**
- * 宝箱开启消耗配置控制器
- */
- #[Resource('game-items-chest-costs', names: 'dcat.admin.game-items-chest-costs')]
- class ItemChestOpenCostController extends AdminController
- {
- protected $title ='宝箱开启消耗配置';
- /**
- * 列表页
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new ItemChestOpenCostRepository(['chest', 'costItem']), function (Grid $grid) {
- $helper = new GridHelper($grid, $this);
- $helper->columnId();
- $grid->column('chest.name', '宝箱名称')->link(function () {
- return admin_url('game-items/' . $this->chest_id);
- });
- $grid->column('cost_type', '消耗类型')->display(function ($value) {
- return CHEST_COST_TYPE::getAll()[$value] ?? '未知';
- });
- $grid->column('cost_id', '消耗ID')->as(function ($value) {
- if ($this->cost_type == CHEST_COST_TYPE::ITEM->value) {
- if ($this->costItem) {
- $itemUrl = admin_url('game-items-items/' . $value);
- return "<a href='{$itemUrl}' target='_blank'>{$this->costItem->name} <i class='fa fa-external-link'></i></a> (ID: {$value})";
- }
- return $value;
- }
- return $value;
- })->unescape();
- $grid->column('cost_quantity', '消耗数量');
- $grid->column('is_active', '是否激活')->switch();
- $grid->column('created_at', '创建时间');
- $grid->column('updated_at', '更新时间');
- // 筛选器
- $grid->filter(function (Grid\Filter $filter) {
- $helper = new FilterHelper($filter, $this);
- $helper->equal('id', 'ID');
- $helper->equalSelectModelChestItem('chest_id', '宝箱ID');
- $filter->equal('cost_type', '消耗类型')->select(CHEST_COST_TYPE::getAll());
- $filter->equal('is_active', '是否激活')->radio([
- 1 => '是',
- 0 => '否',
- ]);
- });
- // 批量操作
- $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
- $batch->add(new \App\Module\GameItems\AdminControllers\Actions\BatchDeactivateAction());
- $batch->add(new BatchActivateAction());
- });
- // 工具栏
- $grid->tools(function (Grid\Tools $tools) {
- $tools->append(new \App\Module\GameItems\AdminControllers\Actions\CopyToAnotherChestAction());
- });
- });
- }
- /**
- * 详情页
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new ItemChestOpenCostRepository(['chest', 'costItem']), function (Show $show) {
- $helper = new ShowHelper($show, $this);
- $helper->field('id', 'ID');
- $show->field('chest.name', '宝箱名称');
- $helper->field('chest_id', '宝箱ID');
- $show->field('cost_type', '消耗类型')->as(function ($value) {
- return CHEST_COST_TYPE::getAll()[$value] ?? '未知';
- });
- $helper->field('cost_id', '消耗ID');
- // 根据消耗类型显示不同的关联信息
- $show->field('costItem.name', '消耗物品名称')->unescape()->as(function ($name) {
- if ($this->cost_type == CHEST_COST_TYPE::ITEM->value && $name) {
- $itemUrl = admin_url('game-items/items/' . $this->cost_id);
- return "<span class='badge badge-primary'>{$name}</span> "
- . "<a href='{$itemUrl}' target='_blank' class='btn btn-sm btn-primary ml-1'>"
- . "<i class='fa fa-external-link'></i> 查看物品详情</a>";
- }
- return '-';
- });
- $helper->field('cost_quantity', '消耗数量');
- $show->field('is_active', '是否激活')->as(function ($value) {
- return $value ? '是' : '否';
- });
- $helper->field('created_at', '创建时间');
- $helper->field('updated_at', '更新时间');
- });
- }
- /**
- * 表单
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new ItemChestOpenCostRepository(), function (Form $form) {
- $helper = new FormHelper($form, $this);
- // 只显示宝箱类型的物品
- $chests = Item::where('type', 3)->pluck('name', 'id');
- $helper->selectModelChestItem('chest_id', '宝箱');
- $form->select('cost_type', '消耗类型')->options(CHEST_COST_TYPE::getAll())->required();
- // 根据消耗类型显示不同的选择器
- $form->number('cost_id', '消耗ID')->required();
- $helper->number('cost_quantity', '消耗数量')->min(1)->default(1)->required();
- $form->switch('is_active', '是否激活')->default(1);
- // 保存前回调
- $form->saving(function (Form $form) {
- // 验证消耗ID
- if ($form->cost_type == CHEST_COST_TYPE::ITEM->value) {
- $item = Item::find($form->cost_id);
- if (!$item) {
- return $form->response()->error('无效的物品ID');
- }
- }
- });
- });
- }
- }
|