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

87 lines
2.2 KiB

  1. <?php
  2. namespace App\AdminAgent\Controllers;
  3. use App\Models\Category;
  4. use Dcat\Admin\Admin;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Layout\Content;
  8. use Dcat\Admin\Layout\Row;
  9. use Dcat\Admin\Show;
  10. use Dcat\Admin\Http\Controllers\AdminController;
  11. use Dcat\Admin\Tree;
  12. class CategoryController extends AdminController
  13. {
  14. public function index(Content $content)
  15. {
  16. return $content->header('产品分类')
  17. ->body(function (Row $row) {
  18. $tree = new Tree(new Category);
  19. $tree->query(function ($model) {
  20. //agent_id为0是系统分类,其它是代理商分类
  21. return $model->where('agent_id', Admin::user()->id);
  22. });
  23. $row->column(12, $tree);
  24. });
  25. }
  26. /**
  27. * Make a grid builder.
  28. *
  29. * @return Grid
  30. */
  31. protected function grid()
  32. {
  33. return Grid::make(new Category(), function (Grid $grid) {
  34. $grid->column('id')->sortable();
  35. $grid->column('name');
  36. $grid->column('pid');
  37. $grid->column('sort');
  38. $grid->column('template');
  39. });
  40. }
  41. /**
  42. * Make a show builder.
  43. *
  44. * @param mixed $id
  45. *
  46. * @return Show
  47. */
  48. protected function detail($id)
  49. {
  50. return Show::make($id, new Category(), function (Show $show) {
  51. $show->field('id');
  52. $show->field('name');
  53. $show->field('pid');
  54. $show->field('sort');
  55. $show->field('template');
  56. });
  57. }
  58. /**
  59. * Make a form builder.
  60. *
  61. * @return Form
  62. */
  63. protected function form()
  64. {
  65. return Form::make(new Category(), function (Form $form) {
  66. $agent_id = Admin::user()->id;
  67. $options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
  68. $form->display('id');
  69. $form->hidden('agent_id')->value($agent_id)->required();
  70. $form->select('pid')->options($options)->required();
  71. $form->text('name')->required();
  72. $form->text('sort')->default(255)->help('越小越靠前');
  73. // $form->text('template');
  74. })->saving(function (Form $form) {
  75. $form->agent_id = Admin::user()->id;
  76. $form->sort = $form->sort ?? 255;
  77. });
  78. }
  79. }