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

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