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

115 lines
3.0 KiB

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