| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- <?php
- namespace App\Module\GameItems\AdminControllers;
- use App\Module\GameItems\Repositorys\ItemInstanceRepository;
- use App\Module\GameItems\Repositorys\ItemRepository;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use UCore\DcatAdmin\AdminController;
- use Dcat\Admin\Layout\Content;
- use Spatie\RouteAttributes\Attributes\Resource;
- use UCore\DcatAdmin\FilterHelper;
- use App\Module\GameItems\AdminControllers\Helper\FormHelper;
- use UCore\DcatAdmin\GridHelper;
- use UCore\DcatAdmin\ShowHelper;
- /**
- * 单独属性物品管理控制器
- *
- * @package App\Module\GameItems\AdminControllers
- */
- #[Resource('game-items-instances', names: 'dcat.admin.game-items-instances')]
- class InstanceController extends AdminController
- {
- /**
- * 标题
- *
- * @var string
- */
- protected $title = '单独属性物品管理';
- /**
- * 列表页
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new ItemInstanceRepository(), function (Grid $grid) {
- $helper = new GridHelper($grid, $this);
- $helper->columnId();
- $grid->column('item.name', '基础物品');
- $grid->column('name', '实例名称');
- $grid->column('tradable', '可交易')->switch();
- $grid->column('is_bound', '已绑定')->switch();
- $grid->column('bound_to', '绑定用户ID');
- $grid->column('bind_exp_time', '绑定过期时间');
- $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');
- $filter->equal('item_id', '基础物品')->select(
- (new ItemRepository())->where('is_unique', 1)->pluck('name', 'id')
- );
- $filter->like('name', '实例名称');
- $filter->equal('tradable', '可交易')->radio([
- 1 => '是',
- 0 => '否',
- ]);
- $filter->equal('is_bound', '已绑定')->radio([
- 1 => '是',
- 0 => '否',
- ]);
- $helper->equal('bound_to', '绑定用户ID');
- $filter->between('bind_exp_time', '绑定过期时间')->datetime();
- $filter->between('expire_at', '过期时间')->datetime();
- });
- });
- }
- /**
- * 详情页
- *
- * @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)
- {
- return Show::make($id, new ItemInstanceRepository(), function (Show $show) {
- $helper = new ShowHelper($show, $this);
- $helper->field('id', 'ID');
- $show->field('item.name', '基础物品');
- $helper->field('name', '实例名称');
- // 显示显示属性
- $show->field('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('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('tradable', '可交易')->as(function ($value) {
- return $value ? '是' : '否';
- });
- $show->field('is_bound', '已绑定')->as(function ($value) {
- return $value ? '是' : '否';
- });
- $helper->field('bound_to', '绑定用户ID');
- $helper->field('bind_exp_time', '绑定过期时间');
- $helper->field('expire_at', '过期时间');
- $helper->field('created_at', '创建时间');
- $helper->field('updated_at', '更新时间');
- // 显示拥有该物品实例的用户
- $show->users('拥有用户', function ($users) {
- $users->resource('/admin/game-items-user-items');
- $users->id('ID');
- $users->user_id('用户ID');
- $users->quantity('数量');
- $users->expire_at('过期时间');
- $users->created_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 ItemInstanceRepository(), function (Form $form) {
- $helper = new \App\Module\GameItems\AdminControllers\Helper\FormHelper($form, $this);
- $form->select('item_id', '基础物品')
- ->options((new ItemRepository())->where('is_unique', 1)->pluck('name', 'id'))
- ->required();
- $helper->text('name')
- ->help('留空则使用基础物品名称');
- // 显示属性
- $form->keyValue('display_attributes', '显示属性')
- ->help('用于显示的属性,如:攻击力、防御力等');
- // 数值属性
- $form->keyValue('numeric_attributes', '数值属性')
- ->help('用于计算的属性,如:攻击加成、防御加成等');
- $form->switch('tradable', '可交易')
- ->default(true);
- $form->switch('is_bound', '已绑定')
- ->default(false)
- ->when(true, function (Form $form) {
- $helper = new \App\Module\GameItems\AdminControllers\Helper\FormHelper($form, $this);
- $helper->text('bound_to')
- ->required()
- ->help('物品绑定的用户ID');
- $helper->datetime('bind_exp_time')
- ->help('绑定过期时间,为空表示永久绑定');
- });
- $helper->datetime('expire_at')
- ->help('物品过期时间,为空表示永不过期');
- // 保存前回调
- $form->saving(function (Form $form) {
- // 如果名称为空,使用基础物品名称
- if (empty($form->name)) {
- $item = (new ItemRepository())->find($form->item_id);
- if ($item) {
- $form->name = $item->name;
- }
- }
- // 如果未绑定,清空绑定相关字段
- if (!$form->is_bound) {
- $form->bound_to = null;
- $form->bind_exp_time = null;
- }
- });
- });
- }
- }
|