UserItemController.php 9.4 KB

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