ShopItemController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace App\Module\Shop\AdminControllers;
  3. use App\Module\Shop\Models\ShopCategory;
  4. use App\Module\Shop\Repositorys\ShopItemRepository;
  5. use App\Module\Game\Models\GameConsumeGroup;
  6. use App\Module\Game\Models\GameRewardGroup;
  7. use Dcat\Admin\Form;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Show;
  10. use Dcat\Admin\Http\Controllers\AdminController;
  11. use Spatie\RouteAttributes\Attributes\Resource;
  12. /**
  13. * 商店商品管理控制器
  14. */
  15. #[Resource('shop/items', names: 'dcat.admin.shop.items')]
  16. class ShopItemController 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 ShopItemRepository(), function (Grid $grid) {
  32. // 预加载关联数据,包括消耗组和奖励组的详细项目
  33. $grid->model()->with(['category', 'consumeGroup.consumeItems', 'rewardGroup.rewardItems']);
  34. $grid->column('id', 'ID')->sortable();
  35. $grid->column('name', '商品名称');
  36. $grid->column('image', '商品图片')->image('', 50, 50);
  37. $grid->column('category.name', '所属分类');
  38. $grid->column('category_name', '分类名称');
  39. // 消耗组列 - 可点击跳转
  40. $grid->column('consumeGroup.name', '消耗组')->display(function ($name) {
  41. if (!$name || !$this->consume_group_id) {
  42. return '无';
  43. }
  44. return $name;
  45. })->link(function () {
  46. if (!$this->consume_group_id) {
  47. return '';
  48. }
  49. return admin_url('game-consume-groups/' . $this->consume_group_id);
  50. });
  51. // 奖励组列 - 可点击跳转
  52. $grid->column('rewardGroup.name', '奖励组')->display(function ($name) {
  53. if (!$name || !$this->reward_group_id) {
  54. return '无';
  55. }
  56. return $name;
  57. })->link(function () {
  58. if (!$this->reward_group_id) {
  59. return '';
  60. }
  61. return admin_url('game-reward-groups/' . $this->reward_group_id);
  62. });
  63. // 消耗组详情列
  64. $grid->column('consume_group_details', '消耗组详情')->display(function () {
  65. if (!$this->consumeGroup) {
  66. return '<span class="text-muted">无消耗组</span>';
  67. }
  68. return $this->consumeGroup->formatConsumeDetails();
  69. })->width('200px');
  70. // 奖励组详情列
  71. $grid->column('reward_group_details', '奖励组详情')->display(function () {
  72. if (!$this->rewardGroup) {
  73. return '<span class="text-muted">无奖励组</span>';
  74. }
  75. return $this->rewardGroup->formatRewardDetails();
  76. })->width('200px');
  77. $grid->column('max_single_buy', '单次限购')->display(function ($maxSingleBuy) {
  78. return $maxSingleBuy > 0 ? $maxSingleBuy : '无限制';
  79. });
  80. $grid->column('sort_order', '排序权重')->sortable();
  81. $grid->column('is_active', '状态')->switch();
  82. $grid->column('start_time', '上架时间')->sortable();
  83. $grid->column('end_time', '下架时间')->sortable();
  84. // 过滤器
  85. $grid->filter(function (Grid\Filter $filter) {
  86. $filter->equal('id', 'ID');
  87. $filter->like('name', '商品名称');
  88. $filter->equal('category_id', '所属分类')->select(
  89. ShopCategory::pluck('name', 'id')
  90. );
  91. $filter->like('category_name', '分类名称');
  92. $filter->equal('consume_group_id', '消耗组')->select(
  93. GameConsumeGroup::pluck('name', 'id')
  94. );
  95. $filter->equal('reward_group_id', '奖励组')->select(
  96. GameRewardGroup::pluck('name', 'id')
  97. );
  98. $filter->equal('is_active', '状态')->select([
  99. 0 => '禁用',
  100. 1 => '启用',
  101. ]);
  102. $filter->between('start_time', '上架时间')->datetime();
  103. $filter->between('end_time', '下架时间')->datetime();
  104. });
  105. // 操作列
  106. $grid->actions(function (Grid\Displayers\Actions $actions) {
  107. // 添加限购配置按钮
  108. $actions->append('<a href="' . admin_url('shop/purchase-limits?shop_item_id=' . $actions->getKey()) . '" class="btn btn-sm btn-outline-primary" title="限购配置">
  109. <i class="fa fa-cog"></i> 限购配置
  110. </a>');
  111. });
  112. // 工具栏
  113. $grid->toolsWithOutline(false);
  114. $grid->disableViewButton();
  115. $grid->showQuickEditButton();
  116. $grid->enableDialogCreate();
  117. $grid->enableDialogEdit();
  118. $grid->setDialogFormDimensions('800px', '720px');
  119. });
  120. }
  121. /**
  122. * 创建详情页
  123. *
  124. * @param mixed $id
  125. * @return Show
  126. */
  127. protected function detail($id)
  128. {
  129. return Show::make($id, new ShopItemRepository(), function (Show $show) {
  130. $show->field('id', 'ID');
  131. $show->field('name', '商品名称');
  132. $show->field('description', '商品描述');
  133. $show->field('image', '商品图片')->image();
  134. $show->field('category.name', '所属分类');
  135. $show->field('category_name', '分类名称');
  136. $show->field('consumeGroup.name', '消耗组')->as(function ($name) {
  137. return $name ?: '无';
  138. });
  139. $show->field('rewardGroup.name', '奖励组')->as(function ($name) {
  140. return $name ?: '无';
  141. });
  142. $show->field('max_single_buy', '单次购买限制')->as(function ($maxSingleBuy) {
  143. return $maxSingleBuy > 0 ? $maxSingleBuy : '无限制';
  144. });
  145. $show->field('sort_order', '排序权重');
  146. $show->field('is_active', '状态')->as(function ($isActive) {
  147. return $isActive ? '启用' : '禁用';
  148. });
  149. $show->field('start_time', '上架时间');
  150. $show->field('end_time', '下架时间');
  151. $show->field('created_at', '创建时间');
  152. $show->field('updated_at', '更新时间');
  153. });
  154. }
  155. /**
  156. * 创建表单
  157. *
  158. * @return Form
  159. */
  160. protected function form()
  161. {
  162. return Form::make(new ShopItemRepository(), function (Form $form) {
  163. $form->display('id', 'ID');
  164. $form->text('name', '商品名称')->required();
  165. $form->textarea('description', '商品描述')->rows(3);
  166. $form->image('image', '商品图片')->autoUpload()->uniqueName()->help('建议尺寸:200x200');
  167. $form->select('category_id', '所属分类')
  168. ->options(ShopCategory::pluck('name', 'id'))
  169. ->required();
  170. $form->text('category_name', '分类名称')->help('字符串格式的分类名称,区别于上面的分类ID');
  171. $form->select('consume_group_id', '消耗组')
  172. ->options(GameConsumeGroup::pluck('name', 'id'))
  173. ->help('选择购买此商品需要消耗的资源组');
  174. $form->select('reward_group_id', '奖励组')
  175. ->options(GameRewardGroup::pluck('name', 'id'))
  176. ->help('选择购买此商品获得的奖励组');
  177. $form->number('max_single_buy', '单次购买限制')->min(0)->default(0)->help('用户单次可购买的最大数量,0表示无限制');
  178. $form->number('sort_order', '排序权重')->default(0)->help('数字越小越靠前');
  179. $form->switch('is_active', '状态')->default(true);
  180. $form->datetime('start_time', '上架时间')->help('留空表示不限制上架时间');
  181. $form->datetime('end_time', '下架时间')->help('留空表示不限制下架时间');
  182. $form->display('created_at', '创建时间');
  183. $form->display('updated_at', '更新时间');
  184. });
  185. }
  186. }