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

128 lines
3.4 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. $agent_id = Admin::user()->id;
  73. $options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
  74. $form->display('id');
  75. $form->select('pid')->options($options)->required();
  76. $form->text('name')->required();
  77. $form->text('sort')->default(255)->help('越小越靠前');
  78. // $form->text('template');
  79. })->saving(function (Form $form) {
  80. //不允许修改非自己的数据
  81. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  82. return $form->response()->error('数据不存在');
  83. }
  84. //不允许编辑的字段
  85. $form->ignore(['id', 'deleted_at']);
  86. $form->agent_id = Admin::user()->id;
  87. $form->sort = $form->sort ?? 255;
  88. })->deleting(function (Form $form) {
  89. //不允许修改非自己的数据
  90. if ($form->model()[0]['agent_id'] != Admin::user()->id) {
  91. return $form->response()->error('数据不存在');
  92. }
  93. //获取要删除类目的所有下级ID
  94. $agent_id = Admin::user()->id;
  95. $category = Category::where('agent_id', $agent_id)->pluck('pid', 'id')->toArray();
  96. $ids = getChildCate((int)$form->getKey(), $category);
  97. if (AgentProduct::where('agent_id', $agent_id)->whereIn('category_id', $ids)->exists()) {
  98. return $form->response()->error('该分类下已经发布产品,不允许删除');
  99. }
  100. });
  101. }
  102. }
  103. //获取$pid下的所有子类目
  104. function getChildCate($pid, $category): array
  105. {
  106. $cate_arr = [$pid];
  107. foreach ($category as $k => $v) {
  108. if ($v == $pid) {
  109. $cate_arr = array_merge($cate_arr, getChildCate($k, $category));
  110. }
  111. }
  112. return $cate_arr;
  113. }