海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
4.0 KiB

  1. <?php
  2. namespace App\AdminAgent\Controllers;
  3. use App\AdminAgent\Actions\Tree\LoadSystemCategory;
  4. use App\Models\AgentProduct;
  5. use App\Models\Category;
  6. use Dcat\Admin\Admin;
  7. use Dcat\Admin\Form;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Layout\Content;
  10. use Dcat\Admin\Layout\Row;
  11. use Dcat\Admin\Show;
  12. use Dcat\Admin\Http\Controllers\AdminController;
  13. use Dcat\Admin\Tree;
  14. class CategoryController extends AdminController
  15. {
  16. public function index(Content $content)
  17. {
  18. return $content->header('产品分类')
  19. ->body(function (Row $row) {
  20. $tree = new Tree(new Category);
  21. $tree->query(function ($model) {
  22. //agent_id为0是系统分类,其它是代理商分类
  23. return $model->where('agent_id', Admin::user()->id);
  24. });
  25. $tree->tools(function (Tree\Tools $tools) {
  26. $tools->add(new LoadSystemCategory());
  27. });
  28. $row->column(12, $tree);
  29. });
  30. }
  31. /**
  32. * Make a grid builder.
  33. *
  34. * @return Grid
  35. */
  36. protected function grid()
  37. {
  38. return Grid::make(new Category(), function (Grid $grid) {
  39. $grid->model()->where('agent_id', Admin::user()->id);
  40. $grid->column('id')->sortable();
  41. $grid->column('name');
  42. $grid->column('pid');
  43. $grid->column('sort');
  44. $grid->column('publish_type')->using(admin_trans('product.options.publish_type'));
  45. });
  46. }
  47. /**
  48. * Make a show builder.
  49. *
  50. * @param mixed $id
  51. *
  52. * @return Show
  53. */
  54. protected function detail($id)
  55. {
  56. return Show::make($id, new Category(), function (Show $show) {
  57. //不允许查看非自己的数据
  58. if ($show->model()->agent_id != Admin::user()->id) {
  59. Admin::exit('数据不存在');
  60. }
  61. $show->field('id');
  62. $show->field('name');
  63. $show->field('pid');
  64. $show->field('sort');
  65. $show->field('publish_type')->using(admin_trans('product.options.publish_type'));
  66. });
  67. }
  68. /**
  69. * Make a form builder.
  70. *
  71. * @return Form
  72. */
  73. protected function form()
  74. {
  75. return Form::make(new Category(), function (Form $form) {
  76. //不允许查看非自己的数据
  77. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  78. return $form->response()->error('数据不存在');
  79. }
  80. $agent_id = Admin::user()->id;
  81. $options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
  82. $form->display('id');
  83. $form->select('pid')->options($options)->required();
  84. $form->text('name')->required();
  85. $form->text('sort')->default(255)->help('越小越靠前');
  86. $form->radio('publish_type')->options(admin_trans('product.options.publish_type'))->required();
  87. })->saving(function (Form $form) {
  88. //不允许修改非自己的数据
  89. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  90. return $form->response()->error('数据不存在');
  91. }
  92. //不允许编辑的字段
  93. $form->ignore(['id', 'deleted_at']);
  94. $form->hidden(['agent_id']);
  95. $form->agent_id = Admin::user()->id;
  96. if (array_key_exists('sort', $form->input())) {
  97. $form->sort = $form->sort ?? 255;
  98. }
  99. })->deleting(function (Form $form) {
  100. //不允许修改非自己的数据
  101. if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) {
  102. return $form->response()->error('数据不存在');
  103. }
  104. //获取要删除类目的所有下级ID
  105. $agent_id = Admin::user()->id;
  106. $category = Category::where('agent_id', $agent_id)->pluck('pid', 'id')->toArray();
  107. $ids = getChildCate((int)$form->getKey(), $category);
  108. if (AgentProduct::where('agent_id', $agent_id)->whereIn('category_id', $ids)->exists()) {
  109. return $form->response()->error('该分类下已经发布产品,不允许删除');
  110. }
  111. });
  112. }
  113. }
  114. //获取$pid下的所有子类目
  115. function getChildCate($pid, $category): array
  116. {
  117. $cate_arr = [$pid];
  118. foreach ($category as $k => $v) {
  119. if ($v == $pid) {
  120. $cate_arr = array_merge($cate_arr, getChildCate($k, $category));
  121. }
  122. }
  123. return $cate_arr;
  124. }