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('')
);
});
});
}
/**
* 详情页
*
* @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 = '
';
$html .= '| 属性名 | 属性值 |
';
$html .= '';
foreach ($attributes as $key => $value) {
$html .= '';
$html .= '| ' . $key . ' | ';
$html .= '' . $value . ' | ';
$html .= '
';
}
$html .= '
';
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 = '';
$html .= '| 属性名 | 属性值 |
';
$html .= '';
foreach ($attributes as $key => $value) {
$html .= '';
$html .= '| ' . $key . ' | ';
$html .= '' . $value . ' | ';
$html .= '
';
}
$html .= '
';
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;
}
});
});
}
}