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

127 lines
3.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\AdminAgent\Controllers;
  3. use App\AdminAgent\Repositories\Channel;
  4. use App\Models\Channel as ChannelModel;
  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. use Illuminate\Support\Facades\Storage;
  14. class ChannelController extends AdminController
  15. {
  16. public function index(Content $content)
  17. {
  18. return $content->header('产品频道')
  19. ->body(function (Row $row) {
  20. $tree = new Tree(new ChannelModel());
  21. $tree->query(function ($model) {
  22. //agent_id为0是系统分类,其它是代理商分类
  23. return $model->where('agent_id', Admin::user()->id)->orderBy('sort')->orderBy('id');
  24. });
  25. /*$prefix = Storage::disk('public')->url('');
  26. $tree->branch(function ($branch) use ($prefix) {
  27. $src = $prefix . $branch['icon'] ;
  28. $logo = '<img data-action="preview-img" src="'.$src.'" style="max-width:30px;max-height:30px;cursor:pointer;margin-left:20px;" class="img">';
  29. return "{$branch['id']} - {$branch['name']} $logo";
  30. });*/
  31. $row->column(12, $tree);
  32. });
  33. }
  34. /**
  35. * Make a grid builder.
  36. *
  37. * @return Grid
  38. */
  39. protected function grid()
  40. {
  41. return Grid::make(new Channel(['parent']), function (Grid $grid) {
  42. $grid->model()->where('agent_id', Admin::user()->id)
  43. ->orderBy('sort')->orderBy('id', 'desc');
  44. $grid->column('id')->sortable();
  45. $grid->column('icon')->image('', 60, 60);
  46. $grid->column('name');
  47. $grid->column('parent.name');
  48. $grid->column('sort')->editable()->width(120);
  49. $grid->filter(function (Grid\Filter $filter) {
  50. $filter->panel();
  51. $filter->equal('id')->width(2);
  52. $filter->like('name')->width(3);
  53. });
  54. });
  55. }
  56. /**
  57. * Make a show builder.
  58. *
  59. * @param mixed $id
  60. *
  61. * @return Show
  62. */
  63. protected function detail($id)
  64. {
  65. return Show::make($id, new Channel(['parent']), function (Show $show) {
  66. //不允许查看非自己的数据
  67. if ($show->model()->agent_id != Admin::user()->id) {
  68. Admin::exit('数据不存在');
  69. }
  70. $show->field('id');
  71. $show->field('icon')->image('', 80, 80);
  72. $show->field('name');
  73. $show->field('parent.name');
  74. $show->field('sort');
  75. });
  76. }
  77. /**
  78. * Make a form builder.
  79. *
  80. * @return Form
  81. */
  82. protected function form()
  83. {
  84. return Form::make(new Channel(), function (Form $form) {
  85. //不允许修改非自己的数据
  86. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  87. return $form->response()->error('数据不存在');
  88. }
  89. $form->display('id');
  90. $form->image('icon')->uniqueName()->removable(false);
  91. $form->text('name')->required();
  92. $form->select('pid')->options(\App\Models\Channel::selectOptions())->default(0)->required();
  93. $form->text('sort')->default(255);
  94. })->saving(function (Form $form) {
  95. //不允许修改非自己的数据
  96. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  97. return $form->response()->error('数据不存在');
  98. }
  99. //处理特殊字段
  100. $form->hidden(['agent_id']);
  101. $form->agent_id = Admin::user()->id;
  102. $form->sort = $form->sort ?? 255;
  103. //不允许编辑的字段
  104. $form->ignore(['id', 'deleted_at']);
  105. })->deleting(function (Form $form) {
  106. //不允许删除非自己的数据
  107. if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) {
  108. return $form->response()->error('数据不存在');
  109. }
  110. });
  111. }
  112. }