InstanceController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers;
  3. use App\Module\GameItems\Models\ItemInstance;
  4. use Dcat\Admin\Form;
  5. use Dcat\Admin\Grid;
  6. use Dcat\Admin\Show;
  7. use UCore\DcatAdmin\AdminController;
  8. use Dcat\Admin\Layout\Content;
  9. use Spatie\RouteAttributes\Attributes\Resource;
  10. use UCore\DcatAdmin\FilterHelper;
  11. use UCore\DcatAdmin\FormHelper;
  12. use UCore\DcatAdmin\GridHelper;
  13. use UCore\DcatAdmin\ShowHelper;
  14. #[Resource('game-items-instances', names: 'dcat.admin.game-items-instances')]
  15. class InstanceController extends AdminController
  16. {
  17. /**
  18. * 标题
  19. *
  20. * @var string
  21. */
  22. protected $title = '单独属性物品管理';
  23. /**
  24. * 列表页
  25. *
  26. * @return Grid
  27. */
  28. protected function grid()
  29. {
  30. return Grid::make(new ItemInstance(), function (Grid $grid) {
  31. $helper = new GridHelper($grid, $this);
  32. $grid->column('id', 'ID')->sortable();
  33. $grid->column('item.name', '基础物品');
  34. $grid->column('name', '实例名称');
  35. $grid->column('tradable', '可交易')->switch();
  36. $grid->column('is_bound', '已绑定')->switch();
  37. $grid->column('bound_to', '绑定用户ID');
  38. $grid->column('bind_exp_time', '绑定过期时间');
  39. $grid->column('expire_at', '过期时间');
  40. $grid->column('created_at', '创建时间');
  41. $grid->column('updated_at', '更新时间');
  42. // 筛选
  43. $grid->filter(function ($filter) {
  44. $helper = new FilterHelper($filter, $this);
  45. $helper->equal('id','ID');
  46. $filter->equal('item_id', '基础物品')->select(
  47. ItemItem::where('is_unique', 1)->pluck('name', 'id')
  48. );
  49. $filter->like('name', '实例名称');
  50. $filter->equal('tradable', '可交易')->radio([
  51. 1 => '是',
  52. 0 => '否',
  53. ]);
  54. $filter->equal('is_bound', '已绑定')->radio([
  55. 1 => '是',
  56. 0 => '否',
  57. ]);
  58. $filter->equal('bound_to', '绑定用户ID');
  59. $filter->between('bind_exp_time', '绑定过期时间')->datetime();
  60. $filter->between('expire_at', '过期时间')->datetime();
  61. });
  62. return $grid;
  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($id, new ItemInstance(), function (Show $show) {
  88. $helper = new ShowHelper($show, $this);
  89. $helper->field('id','ID');
  90. $show->field('item.name', '基础物品');
  91. $show->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. $show->field('bound_to', '绑定用户ID');
  145. $show->field('bind_exp_time', '绑定过期时间');
  146. $show->field('expire_at', '过期时间');
  147. $show->field('created_at', '创建时间');
  148. $show->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. return $show;
  159. });
  160. }
  161. /**
  162. * 创建页
  163. *
  164. * @param Content $content
  165. * @return Content
  166. */
  167. public function create(Content $content)
  168. {
  169. return $content
  170. ->header($this->title)
  171. ->description('创建')
  172. ->body($this->form());
  173. }
  174. /**
  175. * 编辑页
  176. *
  177. * @param mixed $id
  178. * @param Content $content
  179. * @return Content
  180. */
  181. public function edit($id, Content $content)
  182. {
  183. return $content
  184. ->header($this->title)
  185. ->description('编辑')
  186. ->body($this->form()->edit($id));
  187. }
  188. /**
  189. * 表单
  190. *
  191. * @return Form
  192. */
  193. protected function form()
  194. {
  195. return Form::make(new ItemInstance(), function (Form $form) {
  196. $helper = new FormHelper($form, $this);
  197. $form->select('item_id', '基础物品')
  198. ->options(ItemItem::where('is_unique', 1)->pluck('name', 'id'))
  199. ->required();
  200. $form->text('name', '实例名称')
  201. ->help('留空则使用基础物品名称');
  202. // 显示属性
  203. $form->keyValue('display_attributes', '显示属性')
  204. ->help('用于显示的属性,如:攻击力、防御力等');
  205. // 数值属性
  206. $form->keyValue('numeric_attributes', '数值属性')
  207. ->help('用于计算的属性,如:攻击加成、防御加成等');
  208. $form->switch('tradable', '可交易')
  209. ->default(true);
  210. $form->switch('is_bound', '已绑定')
  211. ->default(false)
  212. ->when(true, function (Form $form) {
  213. $form->text('bound_to', '绑定用户ID')
  214. ->required()
  215. ->help('物品绑定的用户ID');
  216. $form->datetime('bind_exp_time', '绑定过期时间')
  217. ->help('绑定过期时间,为空表示永久绑定');
  218. });
  219. $form->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. return $form;
  237. });
  238. }
  239. }