UserItemController.php 9.8 KB

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