ShopItemController.php 8.7 KB

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