UserItemController.php 9.0 KB

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