| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <?php
- namespace App\Module\GameItems\AdminControllers;
- use App\Module\GameItems\AdminForms\AddItemForm;
- use App\Module\GameItems\Repositorys\ItemUserRepository;
- use App\Module\GameItems\Repositorys\ItemRepository;
- use App\Module\GameItems\Repositorys\ItemInstanceRepository;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Layout\Content;
- use Dcat\Admin\Widgets\Modal;
- use Spatie\RouteAttributes\Attributes\Resource;
- 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 UCore\DcatAdmin\AdminController;
- /**
- * 用户物品管理控制器
- *
- * @package App\Module\GameItems\AdminControllers
- */
- #[Resource('game-items-user-items', names: 'dcat.admin.game-items-user-items')]
- class UserItemController extends AdminController
- {
- /**
- * 标题
- *
- * @var string
- */
- protected $title = '用户物品管理';
- /**
- * 列表页
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new ItemUserRepository(['item']), function (Grid $grid) {
- // 禁用创建、编辑、查看和删除按钮
- $grid->disableCreateButton();
- $grid->disableActions();
- $grid->disableBatchDelete();
- $helper = new GridHelper($grid, $this);
- $helper->columnId();
- $grid->column('user_id', '用户ID');
- $grid->column('item.name', '物品名称');
- $grid->column('instance_id', '实例ID');
- $grid->column('quantity', '数量');
- $grid->column('expire_at', '过期时间');
- $grid->column('created_at', '创建时间');
- $grid->column('updated_at', '更新时间');
- // 筛选
- $grid->filter(function ($filter) {
- $helper = new FilterHelper($filter, $this);
- $helper->equal('id', 'ID');
- $helper->equal('user_id', '用户ID');
- $helper->equalSelectModelItem('item_id', '物品');
- $helper->equal('instance_id', '实例ID');
- $filter->between('quantity', '数量');
- $filter->between('expire_at', '过期时间')->datetime();
- });
- // 添加自定义"增加物品"按钮
- $grid->tools(function (Grid\Tools $tools) {
- $tools->append(
- Modal::make()
- ->lg()
- ->title('增加物品')
- ->body(AddItemForm::make())
- ->button('<button class="btn btn-primary"><i class="feather icon-plus"></i><span class="d-none d-sm-inline"> 增加物品</span></button>')
- );
- });
- });
- }
- /**
- * 详情页
- *
- * @param mixed $id
- * @param Content $content
- * @return Content
- */
- public function show($id, Content $content)
- {
- return $content
- ->header($this->title)
- ->description('详情')
- ->body($this->detail($id));
- }
- /**
- * 详情页
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- $model = (new ItemUserRepository())->findOrFail($id);
- return Show::make($model, function (Show $show) {
- $helper = new ShowHelper($show, $this);
- $helper->field('id', 'ID');
- $helper->field('user_id', '用户ID');
- $show->field('item.name', '物品名称');
- $show->field('item.description', '物品描述');
- $show->field('item.type', '物品类型')->as(function ($type) {
- $types = [
- 1 => '可使用',
- 2 => '可装备',
- 3 => '可合成',
- 4 => '可交任务',
- 5 => '可开启',
- ];
- return $types[$type] ?? '未知';
- });
- // 如果是单独属性物品,显示实例信息
- if ($show->getModel()->instance_id) {
- $show->field('instance_id', '实例ID');
- $show->field('instance.name', '实例名称');
- // 显示实例显示属性
- $show->field('instance.display_attributes', '实例显示属性')->as(function ($attributes) {
- if (empty($attributes)) {
- return '无';
- }
- if (is_string($attributes)) {
- $attributes = json_decode($attributes, true);
- }
- if (is_array($attributes)) {
- $html = '<table class="table table-bordered">';
- $html .= '<thead><tr><th>属性名</th><th>属性值</th></tr></thead>';
- $html .= '<tbody>';
- foreach ($attributes as $key => $value) {
- $html .= '<tr>';
- $html .= '<td>' . $key . '</td>';
- $html .= '<td>' . $value . '</td>';
- $html .= '</tr>';
- }
- $html .= '</tbody></table>';
- return $html;
- }
- return $attributes;
- })->unescape();
- // 显示实例数值属性
- $show->field('instance.numeric_attributes', '实例数值属性')->as(function ($attributes) {
- if (empty($attributes)) {
- return '无';
- }
- if (is_string($attributes)) {
- $attributes = json_decode($attributes, true);
- }
- if (is_array($attributes)) {
- $html = '<table class="table table-bordered">';
- $html .= '<thead><tr><th>属性名</th><th>属性值</th></tr></thead>';
- $html .= '<tbody>';
- foreach ($attributes as $key => $value) {
- $html .= '<tr>';
- $html .= '<td>' . $key . '</td>';
- $html .= '<td>' . $value . '</td>';
- $html .= '</tr>';
- }
- $html .= '</tbody></table>';
- return $html;
- }
- return $attributes;
- })->unescape();
- $show->field('instance.tradable', '实例可交易')->as(function ($value) {
- return $value ? '是' : '否';
- });
- $show->field('instance.is_bound', '实例已绑定')->as(function ($value) {
- return $value ? '是' : '否';
- });
- $show->field('instance.bound_to', '实例绑定用户ID');
- $show->field('instance.bind_exp_time', '实例绑定过期时间');
- $show->field('instance.expire_at', '实例过期时间');
- }
- $helper->field('quantity', '数量');
- $helper->field('expire_at', '过期时间');
- // 检查是否过期
- $show->field('is_expired', '是否过期')->as(function () {
- return $this->isExpired() ? '是' : '否';
- });
- $show->field('created_at', '创建时间');
- $show->field('updated_at', '更新时间');
- });
- }
- /**
- * 创建页
- *
- * @param Content $content
- * @return Content
- */
- public function create(Content $content)
- {
- return $content
- ->header($this->title)
- ->description('创建')
- ->body($this->form());
- }
- /**
- * 编辑页
- *
- * @param mixed $id
- * @param Content $content
- * @return Content
- */
- public function edit($id, Content $content)
- {
- return $content
- ->header($this->title)
- ->description('编辑')
- ->body($this->form()->edit($id));
- }
- /**
- * 表单
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new ItemUserRepository(), function (Form $form) {
- $helper = new \App\Module\GameItems\AdminControllers\Helper\FormHelper($form, $this);
- $helper->text('user_id', '用户ID')
- ->required()
- ->help('物品所属的用户ID');
- // 物品类型选择
- $form->radio('item_type', '物品类型')
- ->options(['normal' => '普通物品', 'unique' => '单独属性物品'])
- ->default('normal')
- ->when('normal', function (Form $form) {
- $form->select('item_id', '物品')
- ->options((new ItemRepository())->pluck('name', 'id'))
- ->required();
- $form->number('quantity', '数量')
- ->default(1)
- ->min(1)
- ->required();
- })
- ->when('unique', function (Form $form) {
- $form->select('item_id', '物品')
- ->options((new ItemRepository())->where('is_unique', 1)->pluck('name', 'id'))
- ->required();
- $form->select('instance_id', '物品实例')
- ->options(function ($id) {
- if ($id) {
- $instance = (new ItemInstanceRepository())->find($id);
- if ($instance) {
- return [$instance->id => $instance->name . ' (ID: ' . $instance->id . ')'];
- }
- }
- return [];
- })
- ->ajax('api/game-items/instances')
- ->required();
- $form->hidden('quantity')->default(1);
- });
- $form->datetime('expire_at', '过期时间')
- ->help('物品过期时间,为空表示使用物品默认过期时间');
- // 保存前回调
- $form->saving(function (Form $form) {
- // 如果是单独属性物品,数量固定为1
- if ($form->item_type == 'unique') {
- $form->quantity = 1;
- }
- });
- });
- }
- }
|