UserItemController.php 10 KB

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