UserItemController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers;
  3. use App\Module\GameItems\AdminForms\AddItemForm;
  4. use App\Module\GameItems\AdminControllers\Actions\ItemQuantityAction;
  5. use App\Module\GameItems\Repositorys\ItemUserRepository;
  6. use App\Module\GameItems\Repositorys\ItemRepository;
  7. use App\Module\GameItems\Repositorys\ItemInstanceRepository;
  8. use Dcat\Admin\Form;
  9. use Dcat\Admin\Grid;
  10. use Dcat\Admin\Show;
  11. use Dcat\Admin\Layout\Content;
  12. use Dcat\Admin\Widgets\Modal;
  13. use Spatie\RouteAttributes\Attributes\Resource;
  14. use \App\Module\GameItems\AdminControllers\Helper\FilterHelper;
  15. use \App\Module\GameItems\AdminControllers\Helper\FormHelper;
  16. use \App\Module\GameItems\AdminControllers\Helper\GridHelper;
  17. use \App\Module\GameItems\AdminControllers\Helper\ShowHelper;
  18. use UCore\DcatAdmin\AdminController;
  19. /**
  20. * 用户物品管理控制器
  21. *
  22. * @package App\Module\GameItems\AdminControllers
  23. */
  24. #[Resource('game-items-user-items', names: 'dcat.admin.game-items-user-items')]
  25. class UserItemController extends AdminController
  26. {
  27. /**
  28. * 标题
  29. *
  30. * @var string
  31. */
  32. protected $title = '用户物品管理';
  33. /**
  34. * 列表页
  35. *
  36. * @return Grid
  37. */
  38. protected function grid()
  39. {
  40. return Grid::make(new ItemUserRepository(['item']), function (Grid $grid) {
  41. // 禁用创建、编辑、查看和删除按钮
  42. $grid->disableCreateButton();
  43. $grid->disableEditButton();
  44. $grid->disableViewButton();
  45. $grid->disableDeleteButton();
  46. $grid->disableBatchDelete();
  47. // 添加自定义行操作
  48. $grid->actions(function (Grid\Displayers\Actions $actions) {
  49. $actions->append(new ItemQuantityAction());
  50. });
  51. $helper = new GridHelper($grid, $this);
  52. $helper->columnId();
  53. $grid->column('user_id', '用户ID');
  54. // 添加物品ID列,并链接到物品列表
  55. $grid->column('item_id', '物品ID')->display(function ($itemId) {
  56. $url = admin_url('game-items/?id=' . $itemId);
  57. return "<a href='{$url}' target='_blank'>{$itemId}</a>";
  58. });
  59. $grid->column('item.name', '物品名称');
  60. $grid->column('instance_id', '实例ID');
  61. $grid->column('quantity', '数量');
  62. // 添加是否冻结列
  63. $grid->column('is_frozen', '是否冻结')->display(function ($value) {
  64. return $value ?
  65. '<span class="badge badge-danger">已冻结</span>' :
  66. '<span class="badge badge-success">正常</span>';
  67. });
  68. $grid->column('expire_at', '过期时间');
  69. $grid->column('created_at', '创建时间');
  70. $grid->column('updated_at', '更新时间');
  71. // 筛选
  72. $grid->filter(function ($filter) {
  73. $helper = new FilterHelper($filter, $this);
  74. $helper->equal('id', 'ID');
  75. $helper->equal('user_id', '用户ID');
  76. $helper->equalSelectModelItem('item_id', '物品');
  77. $helper->equal('instance_id', '实例ID');
  78. $filter->between('quantity', '数量');
  79. // 添加冻结状态筛选
  80. $filter->equal('is_frozen', '是否冻结')->select([
  81. '' => '全部',
  82. '0' => '正常',
  83. '1' => '已冻结'
  84. ]);
  85. $filter->between('expire_at', '过期时间')->datetime();
  86. });
  87. // 添加自定义"增加物品"按钮
  88. $grid->tools(function (Grid\Tools $tools) {
  89. $tools->append(
  90. Modal::make()
  91. ->lg()
  92. ->title('增加物品')
  93. ->body(AddItemForm::make())
  94. ->button('<button class="btn btn-primary"><i class="feather icon-plus"></i><span class="d-none d-sm-inline">&nbsp; 增加物品</span></button>')
  95. );
  96. });
  97. });
  98. }
  99. /**
  100. * 详情页
  101. *
  102. * @param mixed $id
  103. * @param Content $content
  104. * @return Content
  105. */
  106. public function show($id, Content $content)
  107. {
  108. return $content
  109. ->header($this->title)
  110. ->description('详情')
  111. ->body($this->detail($id));
  112. }
  113. /**
  114. * 详情页
  115. *
  116. * @param mixed $id
  117. * @return Show
  118. */
  119. protected function detail($id)
  120. {
  121. $model = \App\Module\GameItems\Models\ItemUser::with(['item', 'instance', 'freezeLog'])->findOrFail($id);
  122. return Show::make($model, function (Show $show) use ($model) {
  123. $helper = new ShowHelper($show, $this);
  124. $helper->field('id', 'ID');
  125. $helper->field('user_id', '用户ID');
  126. $show->field('item.name', '物品名称');
  127. $show->field('item.description', '物品描述');
  128. $show->field('item.type', '物品类型')->as(function ($type) {
  129. $types = [
  130. 1 => '可使用',
  131. 2 => '可装备',
  132. 3 => '可合成',
  133. 4 => '可交任务',
  134. 5 => '可开启',
  135. ];
  136. return $types[$type] ?? '未知';
  137. });
  138. // 如果是单独属性物品,显示实例信息
  139. if ($model->instance_id) {
  140. $show->field('instance_id', '实例ID');
  141. $show->field('instance.name', '实例名称');
  142. // 显示实例显示属性
  143. $show->field('instance.display_attributes', '实例显示属性')->as(function ($attributes) {
  144. if (empty($attributes)) {
  145. return '无';
  146. }
  147. if (is_string($attributes)) {
  148. $attributes = json_decode($attributes, true);
  149. }
  150. if (is_array($attributes)) {
  151. $html = '<table class="table table-bordered">';
  152. $html .= '<thead><tr><th>属性名</th><th>属性值</th></tr></thead>';
  153. $html .= '<tbody>';
  154. foreach ($attributes as $key => $value) {
  155. $html .= '<tr>';
  156. $html .= '<td>' . $key . '</td>';
  157. $html .= '<td>' . $value . '</td>';
  158. $html .= '</tr>';
  159. }
  160. $html .= '</tbody></table>';
  161. return $html;
  162. }
  163. return $attributes;
  164. })->unescape();
  165. // 显示实例数值属性
  166. $show->field('instance.numeric_attributes', '实例数值属性')->as(function ($attributes) {
  167. if (empty($attributes)) {
  168. return '无';
  169. }
  170. if (is_string($attributes)) {
  171. $attributes = json_decode($attributes, true);
  172. }
  173. if (is_array($attributes)) {
  174. $html = '<table class="table table-bordered">';
  175. $html .= '<thead><tr><th>属性名</th><th>属性值</th></tr></thead>';
  176. $html .= '<tbody>';
  177. foreach ($attributes as $key => $value) {
  178. $html .= '<tr>';
  179. $html .= '<td>' . $key . '</td>';
  180. $html .= '<td>' . $value . '</td>';
  181. $html .= '</tr>';
  182. }
  183. $html .= '</tbody></table>';
  184. return $html;
  185. }
  186. return $attributes;
  187. })->unescape();
  188. $show->field('instance.tradable', '实例可交易')->as(function ($value) {
  189. return $value ? '是' : '否';
  190. });
  191. $show->field('instance.is_bound', '实例已绑定')->as(function ($value) {
  192. return $value ? '是' : '否';
  193. });
  194. $show->field('instance.bound_to', '实例绑定用户ID');
  195. $show->field('instance.bind_exp_time', '实例绑定过期时间');
  196. $show->field('instance.expire_at', '实例过期时间');
  197. }
  198. $helper->field('quantity', '数量');
  199. // 添加冻结状态显示
  200. $show->field('is_frozen', '是否冻结')->as(function ($value) {
  201. return $value ? '已冻结' : '正常';
  202. });
  203. $helper->field('expire_at', '过期时间');
  204. // 检查是否过期
  205. $show->field('is_expired', '是否过期')->as(function () {
  206. if (!$this->expire_at) {
  207. return '永不过期';
  208. }
  209. return now()->gt($this->expire_at) ? '是' : '否';
  210. });
  211. $show->field('created_at', '创建时间');
  212. $show->field('updated_at', '更新时间');
  213. });
  214. }
  215. /**
  216. * 创建页
  217. *
  218. * @param Content $content
  219. * @return Content
  220. */
  221. public function create(Content $content)
  222. {
  223. return $content
  224. ->header($this->title)
  225. ->description('创建')
  226. ->body($this->form());
  227. }
  228. /**
  229. * 编辑页
  230. *
  231. * @param mixed $id
  232. * @param Content $content
  233. * @return Content
  234. */
  235. public function edit($id, Content $content)
  236. {
  237. return $content
  238. ->header($this->title)
  239. ->description('编辑')
  240. ->body($this->form()->edit($id));
  241. }
  242. /**
  243. * 表单
  244. *
  245. * @return Form
  246. */
  247. protected function form()
  248. {
  249. return Form::make(new ItemUserRepository(), function (Form $form) {
  250. $helper = new \App\Module\GameItems\AdminControllers\Helper\FormHelper($form, $this);
  251. $helper->text('user_id', '用户ID')
  252. ->required()
  253. ->help('物品所属的用户ID');
  254. // 物品类型选择
  255. $form->radio('item_type', '物品类型')
  256. ->options(['normal' => '普通物品', 'unique' => '单独属性物品'])
  257. ->default('normal')
  258. ->when('normal', function (Form $form) {
  259. $form->select('item_id', '物品')
  260. ->options((new ItemRepository())->pluck('name', 'id'))
  261. ->required();
  262. $form->number('quantity', '数量')
  263. ->default(1)
  264. ->min(1)
  265. ->required();
  266. })
  267. ->when('unique', function (Form $form) {
  268. $form->select('item_id', '物品')
  269. ->options((new ItemRepository())->where('is_unique', 1)->pluck('name', 'id'))
  270. ->required();
  271. $form->select('instance_id', '物品实例')
  272. ->options(function ($id) {
  273. if ($id) {
  274. $instance = (new ItemInstanceRepository())->find($id);
  275. if ($instance) {
  276. return [$instance->id => $instance->name . ' (ID: ' . $instance->id . ')'];
  277. }
  278. }
  279. return [];
  280. })
  281. ->ajax('api/game-items/instances')
  282. ->required();
  283. $form->hidden('quantity')->default(1);
  284. });
  285. $form->datetime('expire_at', '过期时间')
  286. ->help('物品过期时间,为空表示使用物品默认过期时间');
  287. // 保存前回调
  288. $form->saving(function (Form $form) {
  289. // 如果是单独属性物品,数量固定为1
  290. if ($form->item_type == 'unique') {
  291. $form->quantity = 1;
  292. }
  293. });
  294. });
  295. }
  296. }