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

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