UserItemController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. $grid = new Grid(new ItemUser());
  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. return $grid;
  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. $show = new Show(ItemUser::findOrFail($id));
  73. $show->field('id', 'ID');
  74. $show->field('user_id', '用户ID');
  75. $show->field('item.name', '物品名称');
  76. $show->field('item.description', '物品描述');
  77. $show->field('item.type', '物品类型')->as(function ($type) {
  78. $types = [
  79. 1 => '可使用',
  80. 2 => '可装备',
  81. 3 => '可合成',
  82. 4 => '可交任务',
  83. 5 => '可开启',
  84. ];
  85. return $types[$type] ?? '未知';
  86. });
  87. // 如果是单独属性物品,显示实例信息
  88. if ($show->getModel()->instance_id) {
  89. $show->field('instance_id', '实例ID');
  90. $show->field('instance.name', '实例名称');
  91. // 显示实例显示属性
  92. $show->field('instance.display_attributes', '实例显示属性')->as(function ($attributes) {
  93. if (empty($attributes)) {
  94. return '无';
  95. }
  96. if (is_string($attributes)) {
  97. $attributes = json_decode($attributes, true);
  98. }
  99. if (is_array($attributes)) {
  100. $html = '<table class="table table-bordered">';
  101. $html .= '<thead><tr><th>属性名</th><th>属性值</th></tr></thead>';
  102. $html .= '<tbody>';
  103. foreach ($attributes as $key => $value) {
  104. $html .= '<tr>';
  105. $html .= '<td>' . $key . '</td>';
  106. $html .= '<td>' . $value . '</td>';
  107. $html .= '</tr>';
  108. }
  109. $html .= '</tbody></table>';
  110. return $html;
  111. }
  112. return $attributes;
  113. })->unescape();
  114. // 显示实例数值属性
  115. $show->field('instance.numeric_attributes', '实例数值属性')->as(function ($attributes) {
  116. if (empty($attributes)) {
  117. return '无';
  118. }
  119. if (is_string($attributes)) {
  120. $attributes = json_decode($attributes, true);
  121. }
  122. if (is_array($attributes)) {
  123. $html = '<table class="table table-bordered">';
  124. $html .= '<thead><tr><th>属性名</th><th>属性值</th></tr></thead>';
  125. $html .= '<tbody>';
  126. foreach ($attributes as $key => $value) {
  127. $html .= '<tr>';
  128. $html .= '<td>' . $key . '</td>';
  129. $html .= '<td>' . $value . '</td>';
  130. $html .= '</tr>';
  131. }
  132. $html .= '</tbody></table>';
  133. return $html;
  134. }
  135. return $attributes;
  136. })->unescape();
  137. $show->field('instance.tradable', '实例可交易')->as(function ($value) {
  138. return $value ? '是' : '否';
  139. });
  140. $show->field('instance.is_bound', '实例已绑定')->as(function ($value) {
  141. return $value ? '是' : '否';
  142. });
  143. $show->field('instance.bound_to', '实例绑定用户ID');
  144. $show->field('instance.bind_exp_time', '实例绑定过期时间');
  145. $show->field('instance.expire_at', '实例过期时间');
  146. }
  147. $show->field('quantity', '数量');
  148. $show->field('expire_at', '过期时间');
  149. // 检查是否过期
  150. $show->field('is_expired', '是否过期')->as(function () {
  151. return $this->isExpired() ? '是' : '否';
  152. });
  153. $show->field('created_at', '创建时间');
  154. $show->field('updated_at', '更新时间');
  155. return $show;
  156. }
  157. /**
  158. * 创建页
  159. *
  160. * @param Content $content
  161. * @return Content
  162. */
  163. public function create(Content $content)
  164. {
  165. return $content
  166. ->header($this->title)
  167. ->description('创建')
  168. ->body($this->form());
  169. }
  170. /**
  171. * 编辑页
  172. *
  173. * @param mixed $id
  174. * @param Content $content
  175. * @return Content
  176. */
  177. public function edit($id, Content $content)
  178. {
  179. return $content
  180. ->header($this->title)
  181. ->description('编辑')
  182. ->body($this->form()->edit($id));
  183. }
  184. /**
  185. * 表单
  186. *
  187. * @return Form
  188. */
  189. protected function form()
  190. {
  191. $form = new Form(new ItemUser());
  192. $form->text('user_id', '用户ID')
  193. ->required()
  194. ->help('物品所属的用户ID');
  195. // 物品类型选择
  196. $form->radio('item_type', '物品类型')
  197. ->options(['normal' => '普通物品', 'unique' => '单独属性物品'])
  198. ->default('normal')
  199. ->when('normal', function (Form $form) {
  200. $form->select('item_id', '物品')
  201. ->options(Item::pluck('name', 'id'))
  202. ->required();
  203. $form->number('quantity', '数量')
  204. ->default(1)
  205. ->min(1)
  206. ->required();
  207. })
  208. ->when('unique', function (Form $form) {
  209. $form->select('item_id', '物品')
  210. ->options(Item::where('is_unique', 1)->pluck('name', 'id'))
  211. ->required();
  212. $form->select('instance_id', '物品实例')
  213. ->options(function ($id) {
  214. if ($id) {
  215. $instance = ItemInstance::find($id);
  216. if ($instance) {
  217. return [$instance->id => $instance->name . ' (ID: ' . $instance->id . ')'];
  218. }
  219. }
  220. return [];
  221. })
  222. ->ajax('api/game-items/instances')
  223. ->required();
  224. $form->hidden('quantity')->default(1);
  225. });
  226. $form->datetime('expire_at', '过期时间')
  227. ->help('物品过期时间,为空表示使用物品默认过期时间');
  228. // 保存前回调
  229. $form->saving(function (Form $form) {
  230. // 如果是单独属性物品,数量固定为1
  231. if ($form->item_type == 'unique') {
  232. $form->quantity = 1;
  233. }
  234. });
  235. return $form;
  236. }
  237. }