UserItemController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. return $grid;
  49. });
  50. }
  51. /**
  52. * 详情页
  53. *
  54. * @param mixed $id
  55. * @param Content $content
  56. * @return Content
  57. */
  58. public function show($id, Content $content)
  59. {
  60. return $content
  61. ->header($this->title)
  62. ->description('详情')
  63. ->body($this->detail($id));
  64. }
  65. /**
  66. * 详情页
  67. *
  68. * @param mixed $id
  69. * @return Show
  70. */
  71. protected function detail($id)
  72. {
  73. return Show::make($id, new ItemUser(), 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. return $show;
  157. });
  158. }
  159. /**
  160. * 创建页
  161. *
  162. * @param Content $content
  163. * @return Content
  164. */
  165. public function create(Content $content)
  166. {
  167. return $content
  168. ->header($this->title)
  169. ->description('创建')
  170. ->body($this->form());
  171. }
  172. /**
  173. * 编辑页
  174. *
  175. * @param mixed $id
  176. * @param Content $content
  177. * @return Content
  178. */
  179. public function edit($id, Content $content)
  180. {
  181. return $content
  182. ->header($this->title)
  183. ->description('编辑')
  184. ->body($this->form()->edit($id));
  185. }
  186. /**
  187. * 表单
  188. *
  189. * @return Form
  190. */
  191. protected function form()
  192. {
  193. return Form::make(new ItemUser(), function (Form $form) {
  194. $form->text('user_id', '用户ID')
  195. ->required()
  196. ->help('物品所属的用户ID');
  197. // 物品类型选择
  198. $form->radio('item_type', '物品类型')
  199. ->options(['normal' => '普通物品', 'unique' => '单独属性物品'])
  200. ->default('normal')
  201. ->when('normal', function (Form $form) {
  202. $form->select('item_id', '物品')
  203. ->options(Item::pluck('name', 'id'))
  204. ->required();
  205. $form->number('quantity', '数量')
  206. ->default(1)
  207. ->min(1)
  208. ->required();
  209. })
  210. ->when('unique', function (Form $form) {
  211. $form->select('item_id', '物品')
  212. ->options(Item::where('is_unique', 1)->pluck('name', 'id'))
  213. ->required();
  214. $form->select('instance_id', '物品实例')
  215. ->options(function ($id) {
  216. if ($id) {
  217. $instance = ItemInstance::find($id);
  218. if ($instance) {
  219. return [$instance->id => $instance->name . ' (ID: ' . $instance->id . ')'];
  220. }
  221. }
  222. return [];
  223. })
  224. ->ajax('api/game-items/instances')
  225. ->required();
  226. $form->hidden('quantity')->default(1);
  227. });
  228. $form->datetime('expire_at', '过期时间')
  229. ->help('物品过期时间,为空表示使用物品默认过期时间');
  230. // 保存前回调
  231. $form->saving(function (Form $form) {
  232. // 如果是单独属性物品,数量固定为1
  233. if ($form->item_type == 'unique') {
  234. $form->quantity = 1;
  235. }
  236. });
  237. return $form;
  238. });
  239. }
  240. }