InstanceController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers;
  3. use App\Module\GameItems\Models\ItemInstance;
  4. use App\Module\GameItems\Models\Item as ItemItem;
  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 UCore\DcatAdmin\FilterHelper;
  12. use UCore\DcatAdmin\FormHelper;
  13. use UCore\DcatAdmin\GridHelper;
  14. use UCore\DcatAdmin\ShowHelper;
  15. #[Resource('game-items-instances', names: 'dcat.admin.game-items-instances')]
  16. class InstanceController extends AdminController
  17. {
  18. /**
  19. * 标题
  20. *
  21. * @var string
  22. */
  23. protected $title = '单独属性物品管理';
  24. /**
  25. * 列表页
  26. *
  27. * @return Grid
  28. */
  29. protected function grid()
  30. {
  31. return Grid::make(new ItemInstance(), function (Grid $grid) {
  32. $helper = new GridHelper($grid, $this);
  33. $helper->columnId();
  34. $grid->column('item.name', '基础物品');
  35. $grid->column('name', '实例名称');
  36. $grid->column('tradable', '可交易')->switch();
  37. $grid->column('is_bound', '已绑定')->switch();
  38. $grid->column('bound_to', '绑定用户ID');
  39. $grid->column('bind_exp_time', '绑定过期时间');
  40. $grid->column('expire_at', '过期时间');
  41. $grid->column('created_at', '创建时间');
  42. $grid->column('updated_at', '更新时间');
  43. // 筛选
  44. $grid->filter(function ($filter) {
  45. $helper = new FilterHelper($filter, $this);
  46. $helper->equal('id', 'ID');
  47. $filter->equal('item_id', '基础物品')->select(
  48. ItemItem::where('is_unique', 1)->pluck('name', 'id')
  49. );
  50. $filter->like('name', '实例名称');
  51. $filter->equal('tradable', '可交易')->radio([
  52. 1 => '是',
  53. 0 => '否',
  54. ]);
  55. $filter->equal('is_bound', '已绑定')->radio([
  56. 1 => '是',
  57. 0 => '否',
  58. ]);
  59. $helper->equal('bound_to', '绑定用户ID');
  60. $filter->between('bind_exp_time', '绑定过期时间')->datetime();
  61. $filter->between('expire_at', '过期时间')->datetime();
  62. });
  63. });
  64. }
  65. /**
  66. * 详情页
  67. *
  68. * @param mixed $id
  69. * @param Content $content
  70. * @return Content
  71. */
  72. public function show($id, Content $content)
  73. {
  74. return $content
  75. ->header($this->title)
  76. ->description('详情')
  77. ->body($this->detail($id));
  78. }
  79. /**
  80. * 详情页
  81. *
  82. * @param mixed $id
  83. * @return Show
  84. */
  85. protected function detail($id)
  86. {
  87. return Show::make(ItemInstance::findOrFail($id), function (Show $show) {
  88. $helper = new ShowHelper($show, $this);
  89. $helper->field('id', 'ID');
  90. $show->field('item.name', '基础物品');
  91. $helper->field('name', '实例名称');
  92. // 显示显示属性
  93. $show->field('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('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('tradable', '可交易')->as(function ($value) {
  139. return $value ? '是' : '否';
  140. });
  141. $show->field('is_bound', '已绑定')->as(function ($value) {
  142. return $value ? '是' : '否';
  143. });
  144. $helper->field('bound_to', '绑定用户ID');
  145. $helper->field('bind_exp_time', '绑定过期时间');
  146. $helper->field('expire_at', '过期时间');
  147. $helper->field('created_at', '创建时间');
  148. $helper->field('updated_at', '更新时间');
  149. // 显示拥有该物品实例的用户
  150. $show->users('拥有用户', function ($users) {
  151. $users->resource('/admin/game-items-user-items');
  152. $users->id('ID');
  153. $users->user_id('用户ID');
  154. $users->quantity('数量');
  155. $users->expire_at('过期时间');
  156. $users->created_at('获得时间');
  157. });
  158. });
  159. }
  160. /**
  161. * 创建页
  162. *
  163. * @param Content $content
  164. * @return Content
  165. */
  166. public function create(Content $content)
  167. {
  168. return $content
  169. ->header($this->title)
  170. ->description('创建')
  171. ->body($this->form());
  172. }
  173. /**
  174. * 编辑页
  175. *
  176. * @param mixed $id
  177. * @param Content $content
  178. * @return Content
  179. */
  180. public function edit($id, Content $content)
  181. {
  182. return $content
  183. ->header($this->title)
  184. ->description('编辑')
  185. ->body($this->form()->edit($id));
  186. }
  187. /**
  188. * 表单
  189. *
  190. * @return Form
  191. */
  192. protected function form()
  193. {
  194. return Form::make(new ItemInstance(), function (Form $form) {
  195. $helper = new FormHelper($form, $this);
  196. $form->select('item_id', '基础物品')
  197. ->options(ItemItem::where('is_unique', 1)->pluck('name', 'id'))
  198. ->required();
  199. $helper->text('name')
  200. ->help('留空则使用基础物品名称');
  201. // 显示属性
  202. $form->keyValue('display_attributes', '显示属性')
  203. ->help('用于显示的属性,如:攻击力、防御力等');
  204. // 数值属性
  205. $form->keyValue('numeric_attributes', '数值属性')
  206. ->help('用于计算的属性,如:攻击加成、防御加成等');
  207. $form->switch('tradable', '可交易')
  208. ->default(true);
  209. $form->switch('is_bound', '已绑定')
  210. ->default(false)
  211. ->when(true, function (Form $form) {
  212. $helper = new FormHelper($form, $this);
  213. $helper->text('bound_to')
  214. ->required()
  215. ->help('物品绑定的用户ID');
  216. $helper->datetime('bind_exp_time')
  217. ->help('绑定过期时间,为空表示永久绑定');
  218. });
  219. $helper->datetime('expire_at')
  220. ->help('物品过期时间,为空表示永不过期');
  221. // 保存前回调
  222. $form->saving(function (Form $form) {
  223. // 如果名称为空,使用基础物品名称
  224. if (empty($form->name)) {
  225. $item = ItemItem::find($form->item_id);
  226. if ($item) {
  227. $form->name = $item->name;
  228. }
  229. }
  230. // 如果未绑定,清空绑定相关字段
  231. if (!$form->is_bound) {
  232. $form->bound_to = null;
  233. $form->bind_exp_time = null;
  234. }
  235. });
  236. });
  237. }
  238. }