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

101 lines
2.8 KiB

4 years ago
  1. <?php
  2. namespace App\AdminAgent\Controllers;
  3. use App\AdminAgent\Repositories\Channel;
  4. use Dcat\Admin\Admin;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Show;
  8. use Dcat\Admin\Http\Controllers\AdminController;
  9. class ChannelController extends AdminController
  10. {
  11. /**
  12. * Make a grid builder.
  13. *
  14. * @return Grid
  15. */
  16. protected function grid()
  17. {
  18. return Grid::make(new Channel(['parent']), function (Grid $grid) {
  19. $grid->model()->where('agent_id', Admin::user()->id)
  20. ->orderBy('sort')->orderBy('id', 'desc');
  21. $grid->column('id')->sortable();
  22. $grid->column('icon')->image('', 60, 60);
  23. $grid->column('name');
  24. $grid->column('parent.name');
  25. $grid->column('sort')->editable()->width(120);
  26. $grid->filter(function (Grid\Filter $filter) {
  27. $filter->panel();
  28. $filter->equal('id')->width(2);
  29. $filter->like('name')->width(3);
  30. });
  31. });
  32. }
  33. /**
  34. * Make a show builder.
  35. *
  36. * @param mixed $id
  37. *
  38. * @return Show
  39. */
  40. protected function detail($id)
  41. {
  42. return Show::make($id, new Channel(['parent']), function (Show $show) {
  43. //不允许查看非自己的数据
  44. if ($show->model()->agent_id != Admin::user()->id) {
  45. Admin::exit('数据不存在');
  46. }
  47. $show->field('id');
  48. $show->field('icon')->image('', 80, 80);
  49. $show->field('name');
  50. $show->field('parent.name');
  51. $show->field('sort');
  52. });
  53. }
  54. /**
  55. * Make a form builder.
  56. *
  57. * @return Form
  58. */
  59. protected function form()
  60. {
  61. return Form::make(new Channel(), function (Form $form) {
  62. //不允许修改非自己的数据
  63. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  64. return $form->response()->error('数据不存在');
  65. }
  66. $form->display('id');
  67. $form->image('icon')->uniqueName()->removable(false);
  68. $form->text('name')->required();
  69. $form->select('pid')->options(\App\Models\Channel::selectOptions())->default(0)->required();
  70. $form->text('sort')->default(255);
  71. })->saving(function (Form $form) {
  72. //不允许修改非自己的数据
  73. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  74. return $form->response()->error('数据不存在');
  75. }
  76. //处理特殊字段
  77. $form->hidden(['agent_id']);
  78. $form->agent_id = Admin::user()->id;
  79. $form->sort = $form->sort ?? 255;
  80. //不允许编辑的字段
  81. $form->ignore(['id', 'deleted_at']);
  82. })->deleting(function (Form $form) {
  83. //不允许删除非自己的数据
  84. if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) {
  85. return $form->response()->error('数据不存在');
  86. }
  87. });
  88. }
  89. }