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

110 lines
3.0 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
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\AdminAgent\Controllers;
  3. use App\AdminAgent\Repositories\Notice;
  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 NoticeController extends AdminController
  10. {
  11. /**
  12. * Make a grid builder.
  13. *
  14. * @return Grid
  15. */
  16. protected function grid()
  17. {
  18. return Grid::make(new Notice(), 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('author');
  23. $grid->column('title');
  24. $grid->column('status','是否启用')->switch();
  25. $grid->column('sort')->editable()->width(120);
  26. $grid->column('created_at');
  27. $grid->column('updated_at');
  28. $grid->filter(function (Grid\Filter $filter) {
  29. $filter->panel();
  30. $filter->equal('id')->width(2);
  31. $filter->like('title')->width(3);
  32. });
  33. });
  34. }
  35. /**
  36. * Make a show builder.
  37. *
  38. * @param mixed $id
  39. *
  40. * @return Show
  41. */
  42. protected function detail($id)
  43. {
  44. return Show::make($id, new Notice(), function (Show $show) {
  45. //不允许查看非自己的数据
  46. if ($show->model()->agent_id != Admin::user()->id) {
  47. Admin::exit('数据不存在');
  48. }
  49. $show->field('id');
  50. $show->field('author');
  51. $show->field('title');
  52. $show->field('content')->unescape();
  53. $show->field('status','是否启用');
  54. $show->field('sort');
  55. $show->field('created_at');
  56. $show->field('updated_at');
  57. });
  58. }
  59. /**
  60. * Make a form builder.
  61. *
  62. * @return Form
  63. */
  64. protected function form()
  65. {
  66. return Form::make(new Notice(), function (Form $form) {
  67. //不允许查看非自己的数据
  68. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  69. return $form->response()->error('数据不存在');
  70. }
  71. $form->display('id');
  72. $form->text('author')->default(Admin::user()->name);
  73. $form->text('title')->required();
  74. $form->editor('content');
  75. $form->switch('status','是否启用');
  76. $form->text('sort')->default(255);
  77. })->saving(function (Form $form) {
  78. //不允许修改非自己的数据
  79. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  80. return $form->response()->error('数据不存在');
  81. }
  82. //特殊字段处理
  83. $form->hidden(['agent_id']);
  84. $form->agent_id = Admin::user()->id;
  85. $form->sort = $form->sort ?? 255;
  86. if (array_key_exists('author', $form->input())) {
  87. $form->author = $form->author ?? '';
  88. }
  89. //不允许编辑的字段
  90. $form->ignore(['id', 'created_at', 'updated_at']);
  91. })->deleting(function (Form $form) {
  92. //不允许删除非自己的数据
  93. if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) {
  94. return $form->response()->error('数据不存在');
  95. }
  96. });
  97. }
  98. }