| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- namespace App\Module\GameItems\AdminForms;
- use App\Module\GameItems\Repositorys\ItemRepository;
- use App\Module\GameItems\Services\ItemService;
- use Dcat\Admin\Contracts\LazyRenderable;
- use Dcat\Admin\Traits\LazyWidget;
- use Dcat\Admin\Widgets\Form;
- use Exception;
- /**
- * 添加物品表单
- *
- * 用于在后台添加物品到用户背包
- */
- class AddItemForm extends Form implements LazyRenderable
- {
- use LazyWidget;
- /**
- * 处理表单提交
- *
- * @param array $input
- * @return mixed
- */
- public function handle(array $input)
- {
- try {
- $userId = (int) $input['user_id'];
- $itemId = (int) $input['item_id'];
- $quantity = (int) $input['quantity'];
- // 调用ItemService添加物品
- $itemService = new ItemService();
- $result = $itemService->addItem($userId, $itemId, $quantity, [
- 'source_type' => 'admin_add',
- 'source_id' => 0,
- 'details' => [
- 'admin_id' => auth('admin')->id(),
- 'admin_name' => auth('admin')->user()->name,
- 'remark' => $input['remark'] ?? '',
- ],
- ]);
- if (!empty($result['success'])) {
- return $this->response()
- ->success('物品添加成功')
- ->refresh();
- }
- return $this->response()->error('物品添加失败');
- } catch (Exception $e) {
- return $this->response()->error('操作失败: ' . $e->getMessage());
- }
- }
- /**
- * 构建表单
- */
- public function form()
- {
- // 添加确认提示
- $this->confirm('确定要添加物品吗?');
- // 用户ID
- $this->text('user_id', '用户ID')
- ->required()
- ->help('物品将添加到该用户的背包中');
- // 物品选择
- $this->select('item_id', '物品')
- ->options(function () {
- return \App\Module\GameItems\Models\Item::pluck('name', 'id')->toArray();
- })
- ->required()
- ->help('选择要添加的物品');
- // 数量
- $this->number('quantity', '数量')
- ->default(1)
- ->min(1)
- ->required()
- ->help('添加物品的数量,单独属性物品将自动设为1');
- // 备注
- $this->textarea('remark', '备注')
- ->rows(3)
- ->help('添加物品的备注信息,仅用于记录');
- }
- }
|