InstanceController.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers;
  3. use App\Module\GameItems\Repositorys\ItemInstanceRepository;
  4. use App\Module\GameItems\Repositorys\ItemRepository;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Show;
  8. use UCore\DcatAdmin\AdminController;
  9. use Dcat\Admin\Layout\Content;
  10. use Spatie\RouteAttributes\Attributes\Resource;
  11. use App\Module\GameItems\AdminControllers\Helper\FilterHelper;
  12. use App\Module\GameItems\AdminControllers\Helper\FormHelper;
  13. use App\Module\GameItems\AdminControllers\Helper\GridHelper;
  14. use App\Module\GameItems\AdminControllers\Helper\ShowHelper;
  15. /**
  16. * 单独属性物品管理控制器
  17. *
  18. * @package App\Module\GameItems\AdminControllers
  19. */
  20. #[Resource('game-items-instances', names: 'dcat.admin.game-items-instances')]
  21. class InstanceController extends AdminController
  22. {
  23. /**
  24. * 标题
  25. *
  26. * @var string
  27. */
  28. protected $title = '单独属性物品管理';
  29. /**
  30. * 列表页
  31. *
  32. * @return Grid
  33. */
  34. protected function grid()
  35. {
  36. return Grid::make(new ItemInstanceRepository(), function (Grid $grid) {
  37. $helper = new GridHelper($grid, $this);
  38. $helper->columnId();
  39. $grid->column('item.name', '基础物品');
  40. $grid->column('name', '实例名称');
  41. $grid->column('tradable', '可交易')->switch();
  42. $grid->column('is_bound', '已绑定')->switch();
  43. $grid->column('bound_to', '绑定用户ID');
  44. $grid->column('bind_exp_time', '绑定过期时间');
  45. $grid->column('expire_at', '过期时间');
  46. $grid->column('created_at', '创建时间');
  47. $grid->column('updated_at', '更新时间');
  48. // 筛选
  49. $grid->filter(function ($filter) {
  50. $helper = new FilterHelper($filter, $this);
  51. $helper->equal('id', 'ID');
  52. $filter->equal('item_id', '基础物品')->select(
  53. (new ItemRepository())->where('is_unique', 1)->pluck('name', 'id')
  54. );
  55. $filter->like('name', '实例名称');
  56. $filter->equal('tradable', '可交易')->radio([
  57. 1 => '是',
  58. 0 => '否',
  59. ]);
  60. $filter->equal('is_bound', '已绑定')->radio([
  61. 1 => '是',
  62. 0 => '否',
  63. ]);
  64. $helper->equal('bound_to', '绑定用户ID');
  65. $filter->between('bind_exp_time', '绑定过期时间')->datetime();
  66. $filter->between('expire_at', '过期时间')->datetime();
  67. });
  68. });
  69. }
  70. /**
  71. * 详情页
  72. *
  73. * @param mixed $id
  74. * @param Content $content
  75. * @return Content
  76. */
  77. public function show($id, Content $content)
  78. {
  79. return $content
  80. ->header($this->title)
  81. ->description('详情')
  82. ->body($this->detail($id));
  83. }
  84. /**
  85. * 详情页
  86. *
  87. * @param mixed $id
  88. * @return Show
  89. */
  90. protected function detail($id)
  91. {
  92. return Show::make($id, new ItemInstanceRepository(), function (Show $show) {
  93. $helper = new ShowHelper($show, $this);
  94. $helper->field('id', 'ID');
  95. $show->field('item.name', '基础物品');
  96. $helper->field('name', '实例名称');
  97. // 显示显示属性
  98. $show->field('display_attributes', '显示属性')->as(function ($attributes) {
  99. if (empty($attributes)) {
  100. return '无';
  101. }
  102. if (is_string($attributes)) {
  103. $attributes = json_decode($attributes, true);
  104. }
  105. if (is_array($attributes)) {
  106. $html = '<table class="table table-bordered">';
  107. $html .= '<thead><tr><th>属性名</th><th>属性值</th></tr></thead>';
  108. $html .= '<tbody>';
  109. foreach ($attributes as $key => $value) {
  110. $html .= '<tr>';
  111. $html .= '<td>' . $key . '</td>';
  112. $html .= '<td>' . $value . '</td>';
  113. $html .= '</tr>';
  114. }
  115. $html .= '</tbody></table>';
  116. return $html;
  117. }
  118. return $attributes;
  119. })->unescape();
  120. // 显示数值属性
  121. $show->field('numeric_attributes', '数值属性')->as(function ($attributes) {
  122. if (empty($attributes)) {
  123. return '无';
  124. }
  125. if (is_string($attributes)) {
  126. $attributes = json_decode($attributes, true);
  127. }
  128. if (is_array($attributes)) {
  129. $html = '<table class="table table-bordered">';
  130. $html .= '<thead><tr><th>属性名</th><th>属性值</th></tr></thead>';
  131. $html .= '<tbody>';
  132. foreach ($attributes as $key => $value) {
  133. $html .= '<tr>';
  134. $html .= '<td>' . $key . '</td>';
  135. $html .= '<td>' . $value . '</td>';
  136. $html .= '</tr>';
  137. }
  138. $html .= '</tbody></table>';
  139. return $html;
  140. }
  141. return $attributes;
  142. })->unescape();
  143. $show->field('tradable', '可交易')->as(function ($value) {
  144. return $value ? '是' : '否';
  145. });
  146. $show->field('is_bound', '已绑定')->as(function ($value) {
  147. return $value ? '是' : '否';
  148. });
  149. $helper->field('bound_to', '绑定用户ID');
  150. $helper->field('bind_exp_time', '绑定过期时间');
  151. $helper->field('expire_at', '过期时间');
  152. $helper->field('created_at', '创建时间');
  153. $helper->field('updated_at', '更新时间');
  154. // 显示拥有该物品实例的用户
  155. $show->users('拥有用户', function ($users) {
  156. $users->resource('/admin/game-items-user-items');
  157. $users->id('ID');
  158. $users->user_id('用户ID');
  159. $users->quantity('数量');
  160. $users->expire_at('过期时间');
  161. $users->created_at('获得时间');
  162. });
  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 ItemInstanceRepository(), function (Form $form) {
  200. $helper = new \App\Module\GameItems\AdminControllers\Helper\FormHelper($form, $this);
  201. $form->select('item_id', '基础物品')
  202. ->options((new ItemRepository())->where('is_unique', 1)->pluck('name', 'id'))
  203. ->required();
  204. $helper->text('name')
  205. ->help('留空则使用基础物品名称');
  206. // 显示属性
  207. $form->keyValue('display_attributes', '显示属性')
  208. ->help('用于显示的属性,如:攻击力、防御力等');
  209. // 数值属性
  210. $form->keyValue('numeric_attributes', '数值属性')
  211. ->help('用于计算的属性,如:攻击加成、防御加成等');
  212. $form->switch('tradable', '可交易')
  213. ->default(true);
  214. $form->switch('is_bound', '已绑定')
  215. ->default(false)
  216. ->when(true, function (Form $form) {
  217. $helper = new \App\Module\GameItems\AdminControllers\Helper\FormHelper($form, $this);
  218. $helper->text('bound_to')
  219. ->required()
  220. ->help('物品绑定的用户ID');
  221. $helper->datetime('bind_exp_time')
  222. ->help('绑定过期时间,为空表示永久绑定');
  223. });
  224. $helper->datetime('expire_at')
  225. ->help('物品过期时间,为空表示永不过期');
  226. // 保存前回调
  227. $form->saving(function (Form $form) {
  228. // 如果名称为空,使用基础物品名称
  229. if (empty($form->name)) {
  230. $item = (new ItemRepository())->find($form->item_id);
  231. if ($item) {
  232. $form->name = $item->name;
  233. }
  234. }
  235. // 如果未绑定,清空绑定相关字段
  236. if (!$form->is_bound) {
  237. $form->bound_to = null;
  238. $form->bind_exp_time = null;
  239. }
  240. });
  241. });
  242. }
  243. }