海南旅游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.

136 lines
3.7 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. });
  45. }
  46. /**
  47. * Make a show builder.
  48. *
  49. * @param mixed $id
  50. *
  51. * @return Show
  52. */
  53. protected function detail($id)
  54. {
  55. return Show::make($id, new Category(), function (Show $show) {
  56. //不允许查看非自己的数据
  57. if ($show->model()->agent_id != Admin::user()->id) {
  58. Admin::exit('数据不存在');
  59. }
  60. $show->field('id');
  61. $show->field('name');
  62. $show->field('pid');
  63. $show->field('sort');
  64. });
  65. }
  66. /**
  67. * Make a form builder.
  68. *
  69. * @return Form
  70. */
  71. protected function form()
  72. {
  73. return Form::make(new Category(), function (Form $form) {
  74. //不允许查看非自己的数据
  75. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  76. return $form->response()->error('数据不存在');
  77. }
  78. $agent_id = Admin::user()->id;
  79. $options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
  80. $form->display('id');
  81. $form->select('pid')->options($options)->required();
  82. $form->text('name')->required();
  83. $form->text('sort')->default(255)->help('越小越靠前');
  84. })->saving(function (Form $form) {
  85. //不允许修改非自己的数据
  86. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  87. return $form->response()->error('数据不存在');
  88. }
  89. //不允许编辑的字段
  90. $form->ignore(['id', 'deleted_at']);
  91. $form->hidden(['agent_id']);
  92. $form->agent_id = Admin::user()->id;
  93. if (array_key_exists('sort', $form->input())) {
  94. $form->sort = $form->sort ?? 255;
  95. }
  96. })->deleting(function (Form $form) {
  97. //不允许修改非自己的数据
  98. if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) {
  99. return $form->response()->error('数据不存在');
  100. }
  101. //获取要删除类目的所有下级ID
  102. $agent_id = Admin::user()->id;
  103. $category = Category::where('agent_id', $agent_id)->pluck('pid', 'id')->toArray();
  104. $ids = getChildCate((int)$form->getKey(), $category);
  105. if (AgentProduct::where('agent_id', $agent_id)->whereIn('category_id', $ids)->exists()) {
  106. return $form->response()->error('该分类下已经发布产品,不允许删除');
  107. }
  108. });
  109. }
  110. }
  111. //获取$pid下的所有子类目
  112. function getChildCate($pid, $category): array
  113. {
  114. $cate_arr = [$pid];
  115. foreach ($category as $k => $v) {
  116. if ($v == $pid) {
  117. $cate_arr = array_merge($cate_arr, getChildCate($k, $category));
  118. }
  119. }
  120. return $cate_arr;
  121. }