InstanceController.php 8.5 KB

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