UserItemController.php 11 KB

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