UserItemController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. $grid->column('expire_at', '过期时间');
  63. $grid->column('created_at', '创建时间');
  64. $grid->column('updated_at', '更新时间');
  65. // 筛选
  66. $grid->filter(function ($filter) {
  67. $helper = new FilterHelper($filter, $this);
  68. $helper->equal('id', 'ID');
  69. $helper->equal('user_id', '用户ID');
  70. $helper->equalSelectModelItem('item_id', '物品');
  71. $helper->equal('instance_id', '实例ID');
  72. $filter->between('quantity', '数量');
  73. $filter->between('expire_at', '过期时间')->datetime();
  74. });
  75. // 添加自定义"增加物品"按钮
  76. $grid->tools(function (Grid\Tools $tools) {
  77. $tools->append(
  78. Modal::make()
  79. ->lg()
  80. ->title('增加物品')
  81. ->body(AddItemForm::make())
  82. ->button('<button class="btn btn-primary"><i class="feather icon-plus"></i><span class="d-none d-sm-inline">&nbsp; 增加物品</span></button>')
  83. );
  84. });
  85. });
  86. }
  87. /**
  88. * 详情页
  89. *
  90. * @param mixed $id
  91. * @param Content $content
  92. * @return Content
  93. */
  94. public function show($id, Content $content)
  95. {
  96. return $content
  97. ->header($this->title)
  98. ->description('详情')
  99. ->body($this->detail($id));
  100. }
  101. /**
  102. * 详情页
  103. *
  104. * @param mixed $id
  105. * @return Show
  106. */
  107. protected function detail($id)
  108. {
  109. $model = (new ItemUserRepository())->findOrFail($id);
  110. return Show::make($model, function (Show $show) {
  111. $helper = new ShowHelper($show, $this);
  112. $helper->field('id', 'ID');
  113. $helper->field('user_id', '用户ID');
  114. $show->field('item.name', '物品名称');
  115. $show->field('item.description', '物品描述');
  116. $show->field('item.type', '物品类型')->as(function ($type) {
  117. $types = [
  118. 1 => '可使用',
  119. 2 => '可装备',
  120. 3 => '可合成',
  121. 4 => '可交任务',
  122. 5 => '可开启',
  123. ];
  124. return $types[$type] ?? '未知';
  125. });
  126. // 如果是单独属性物品,显示实例信息
  127. if ($show->getModel()->instance_id) {
  128. $show->field('instance_id', '实例ID');
  129. $show->field('instance.name', '实例名称');
  130. // 显示实例显示属性
  131. $show->field('instance.display_attributes', '实例显示属性')->as(function ($attributes) {
  132. if (empty($attributes)) {
  133. return '无';
  134. }
  135. if (is_string($attributes)) {
  136. $attributes = json_decode($attributes, true);
  137. }
  138. if (is_array($attributes)) {
  139. $html = '<table class="table table-bordered">';
  140. $html .= '<thead><tr><th>属性名</th><th>属性值</th></tr></thead>';
  141. $html .= '<tbody>';
  142. foreach ($attributes as $key => $value) {
  143. $html .= '<tr>';
  144. $html .= '<td>' . $key . '</td>';
  145. $html .= '<td>' . $value . '</td>';
  146. $html .= '</tr>';
  147. }
  148. $html .= '</tbody></table>';
  149. return $html;
  150. }
  151. return $attributes;
  152. })->unescape();
  153. // 显示实例数值属性
  154. $show->field('instance.numeric_attributes', '实例数值属性')->as(function ($attributes) {
  155. if (empty($attributes)) {
  156. return '无';
  157. }
  158. if (is_string($attributes)) {
  159. $attributes = json_decode($attributes, true);
  160. }
  161. if (is_array($attributes)) {
  162. $html = '<table class="table table-bordered">';
  163. $html .= '<thead><tr><th>属性名</th><th>属性值</th></tr></thead>';
  164. $html .= '<tbody>';
  165. foreach ($attributes as $key => $value) {
  166. $html .= '<tr>';
  167. $html .= '<td>' . $key . '</td>';
  168. $html .= '<td>' . $value . '</td>';
  169. $html .= '</tr>';
  170. }
  171. $html .= '</tbody></table>';
  172. return $html;
  173. }
  174. return $attributes;
  175. })->unescape();
  176. $show->field('instance.tradable', '实例可交易')->as(function ($value) {
  177. return $value ? '是' : '否';
  178. });
  179. $show->field('instance.is_bound', '实例已绑定')->as(function ($value) {
  180. return $value ? '是' : '否';
  181. });
  182. $show->field('instance.bound_to', '实例绑定用户ID');
  183. $show->field('instance.bind_exp_time', '实例绑定过期时间');
  184. $show->field('instance.expire_at', '实例过期时间');
  185. }
  186. $helper->field('quantity', '数量');
  187. $helper->field('expire_at', '过期时间');
  188. // 检查是否过期
  189. $show->field('is_expired', '是否过期')->as(function () {
  190. return $this->isExpired() ? '是' : '否';
  191. });
  192. $show->field('created_at', '创建时间');
  193. $show->field('updated_at', '更新时间');
  194. });
  195. }
  196. /**
  197. * 创建页
  198. *
  199. * @param Content $content
  200. * @return Content
  201. */
  202. public function create(Content $content)
  203. {
  204. return $content
  205. ->header($this->title)
  206. ->description('创建')
  207. ->body($this->form());
  208. }
  209. /**
  210. * 编辑页
  211. *
  212. * @param mixed $id
  213. * @param Content $content
  214. * @return Content
  215. */
  216. public function edit($id, Content $content)
  217. {
  218. return $content
  219. ->header($this->title)
  220. ->description('编辑')
  221. ->body($this->form()->edit($id));
  222. }
  223. /**
  224. * 表单
  225. *
  226. * @return Form
  227. */
  228. protected function form()
  229. {
  230. return Form::make(new ItemUserRepository(), function (Form $form) {
  231. $helper = new \App\Module\GameItems\AdminControllers\Helper\FormHelper($form, $this);
  232. $helper->text('user_id', '用户ID')
  233. ->required()
  234. ->help('物品所属的用户ID');
  235. // 物品类型选择
  236. $form->radio('item_type', '物品类型')
  237. ->options(['normal' => '普通物品', 'unique' => '单独属性物品'])
  238. ->default('normal')
  239. ->when('normal', function (Form $form) {
  240. $form->select('item_id', '物品')
  241. ->options((new ItemRepository())->pluck('name', 'id'))
  242. ->required();
  243. $form->number('quantity', '数量')
  244. ->default(1)
  245. ->min(1)
  246. ->required();
  247. })
  248. ->when('unique', function (Form $form) {
  249. $form->select('item_id', '物品')
  250. ->options((new ItemRepository())->where('is_unique', 1)->pluck('name', 'id'))
  251. ->required();
  252. $form->select('instance_id', '物品实例')
  253. ->options(function ($id) {
  254. if ($id) {
  255. $instance = (new ItemInstanceRepository())->find($id);
  256. if ($instance) {
  257. return [$instance->id => $instance->name . ' (ID: ' . $instance->id . ')'];
  258. }
  259. }
  260. return [];
  261. })
  262. ->ajax('api/game-items/instances')
  263. ->required();
  264. $form->hidden('quantity')->default(1);
  265. });
  266. $form->datetime('expire_at', '过期时间')
  267. ->help('物品过期时间,为空表示使用物品默认过期时间');
  268. // 保存前回调
  269. $form->saving(function (Form $form) {
  270. // 如果是单独属性物品,数量固定为1
  271. if ($form->item_type == 'unique') {
  272. $form->quantity = 1;
  273. }
  274. });
  275. });
  276. }
  277. }