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