InstanceController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #[Resource('game-items-instances', names: 'dcat.admin.game-items-instances')]
  11. class InstanceController extends AdminController
  12. {
  13. /**
  14. * 标题
  15. *
  16. * @var string
  17. */
  18. protected $title = '单独属性物品管理';
  19. /**
  20. * 列表页
  21. *
  22. * @return Grid
  23. */
  24. protected function grid()
  25. {
  26. $grid = new Grid(new ItemInstance());
  27. $grid->column('id', 'ID')->sortable();
  28. $grid->column('item.name', '基础物品');
  29. $grid->column('name', '实例名称');
  30. $grid->column('tradable', '可交易')->switch();
  31. $grid->column('is_bound', '已绑定')->switch();
  32. $grid->column('bound_to', '绑定用户ID');
  33. $grid->column('bind_exp_time', '绑定过期时间');
  34. $grid->column('expire_at', '过期时间');
  35. $grid->column('created_at', '创建时间');
  36. $grid->column('updated_at', '更新时间');
  37. // 筛选
  38. $grid->filter(function ($filter) {
  39. $filter->equal('id', 'ID');
  40. $filter->equal('item_id', '基础物品')->select(
  41. ItemItem::where('is_unique', 1)->pluck('name', 'id')
  42. );
  43. $filter->like('name', '实例名称');
  44. $filter->equal('tradable', '可交易')->radio([
  45. 1 => '是',
  46. 0 => '否',
  47. ]);
  48. $filter->equal('is_bound', '已绑定')->radio([
  49. 1 => '是',
  50. 0 => '否',
  51. ]);
  52. $filter->equal('bound_to', '绑定用户ID');
  53. $filter->between('bind_exp_time', '绑定过期时间')->datetime();
  54. $filter->between('expire_at', '过期时间')->datetime();
  55. });
  56. return $grid;
  57. }
  58. /**
  59. * 详情页
  60. *
  61. * @param mixed $id
  62. * @param Content $content
  63. * @return Content
  64. */
  65. public function show($id, Content $content)
  66. {
  67. return $content
  68. ->header($this->title)
  69. ->description('详情')
  70. ->body($this->detail($id));
  71. }
  72. /**
  73. * 详情页
  74. *
  75. * @param mixed $id
  76. * @return Show
  77. */
  78. protected function detail($id)
  79. {
  80. $show = new Show(ItemInstance::findOrFail($id));
  81. $show->field('id', 'ID');
  82. $show->field('item.name', '基础物品');
  83. $show->field('name', '实例名称');
  84. // 显示显示属性
  85. $show->field('display_attributes', '显示属性')->as(function ($attributes) {
  86. if (empty($attributes)) {
  87. return '无';
  88. }
  89. if (is_string($attributes)) {
  90. $attributes = json_decode($attributes, true);
  91. }
  92. if (is_array($attributes)) {
  93. $html = '<table class="table table-bordered">';
  94. $html .= '<thead><tr><th>属性名</th><th>属性值</th></tr></thead>';
  95. $html .= '<tbody>';
  96. foreach ($attributes as $key => $value) {
  97. $html .= '<tr>';
  98. $html .= '<td>' . $key . '</td>';
  99. $html .= '<td>' . $value . '</td>';
  100. $html .= '</tr>';
  101. }
  102. $html .= '</tbody></table>';
  103. return $html;
  104. }
  105. return $attributes;
  106. })->unescape();
  107. // 显示数值属性
  108. $show->field('numeric_attributes', '数值属性')->as(function ($attributes) {
  109. if (empty($attributes)) {
  110. return '无';
  111. }
  112. if (is_string($attributes)) {
  113. $attributes = json_decode($attributes, true);
  114. }
  115. if (is_array($attributes)) {
  116. $html = '<table class="table table-bordered">';
  117. $html .= '<thead><tr><th>属性名</th><th>属性值</th></tr></thead>';
  118. $html .= '<tbody>';
  119. foreach ($attributes as $key => $value) {
  120. $html .= '<tr>';
  121. $html .= '<td>' . $key . '</td>';
  122. $html .= '<td>' . $value . '</td>';
  123. $html .= '</tr>';
  124. }
  125. $html .= '</tbody></table>';
  126. return $html;
  127. }
  128. return $attributes;
  129. })->unescape();
  130. $show->field('tradable', '可交易')->as(function ($value) {
  131. return $value ? '是' : '否';
  132. });
  133. $show->field('is_bound', '已绑定')->as(function ($value) {
  134. return $value ? '是' : '否';
  135. });
  136. $show->field('bound_to', '绑定用户ID');
  137. $show->field('bind_exp_time', '绑定过期时间');
  138. $show->field('expire_at', '过期时间');
  139. $show->field('created_at', '创建时间');
  140. $show->field('updated_at', '更新时间');
  141. // 显示拥有该物品实例的用户
  142. $show->users('拥有用户', function ($users) {
  143. $users->resource('/admin/game-items-user-items');
  144. $users->id('ID');
  145. $users->user_id('用户ID');
  146. $users->quantity('数量');
  147. $users->expire_at('过期时间');
  148. $users->created_at('获得时间');
  149. });
  150. return $show;
  151. }
  152. /**
  153. * 创建页
  154. *
  155. * @param Content $content
  156. * @return Content
  157. */
  158. public function create(Content $content)
  159. {
  160. return $content
  161. ->header($this->title)
  162. ->description('创建')
  163. ->body($this->form());
  164. }
  165. /**
  166. * 编辑页
  167. *
  168. * @param mixed $id
  169. * @param Content $content
  170. * @return Content
  171. */
  172. public function edit($id, Content $content)
  173. {
  174. return $content
  175. ->header($this->title)
  176. ->description('编辑')
  177. ->body($this->form()->edit($id));
  178. }
  179. /**
  180. * 表单
  181. *
  182. * @return Form
  183. */
  184. protected function form()
  185. {
  186. $form = new Form(new ItemInstance());
  187. $form->select('item_id', '基础物品')
  188. ->options(ItemItem::where('is_unique', 1)->pluck('name', 'id'))
  189. ->required();
  190. $form->text('name', '实例名称')
  191. ->help('留空则使用基础物品名称');
  192. // 显示属性
  193. $form->keyValue('display_attributes', '显示属性')
  194. ->help('用于显示的属性,如:攻击力、防御力等');
  195. // 数值属性
  196. $form->keyValue('numeric_attributes', '数值属性')
  197. ->help('用于计算的属性,如:攻击加成、防御加成等');
  198. $form->switch('tradable', '可交易')
  199. ->default(true);
  200. $form->switch('is_bound', '已绑定')
  201. ->default(false)
  202. ->when(true, function (Form $form) {
  203. $form->text('bound_to', '绑定用户ID')
  204. ->required()
  205. ->help('物品绑定的用户ID');
  206. $form->datetime('bind_exp_time', '绑定过期时间')
  207. ->help('绑定过期时间,为空表示永久绑定');
  208. });
  209. $form->datetime('expire_at', '过期时间')
  210. ->help('物品过期时间,为空表示永不过期');
  211. // 保存前回调
  212. $form->saving(function (Form $form) {
  213. // 如果名称为空,使用基础物品名称
  214. if (empty($form->name)) {
  215. $item = ItemItem::find($form->item_id);
  216. if ($item) {
  217. $form->name = $item->name;
  218. }
  219. }
  220. // 如果未绑定,清空绑定相关字段
  221. if (!$form->is_bound) {
  222. $form->bound_to = null;
  223. $form->bind_exp_time = null;
  224. }
  225. });
  226. return $form;
  227. }
  228. }