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

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('title');
  23. $grid->column('sort')->editable()->width(120);
  24. $grid->column('created_at');
  25. $grid->column('updated_at');
  26. $grid->filter(function (Grid\Filter $filter) {
  27. $filter->panel();
  28. $filter->equal('id')->width(2);
  29. $filter->like('title')->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 Notice(), function (Show $show) {
  43. //不允许查看非自己的数据
  44. if ($show->model()->agent_id != Admin::user()->id) {
  45. Admin::exit('数据不存在');
  46. }
  47. $show->field('id');
  48. $show->field('title');
  49. $show->field('content')->unescape();
  50. $show->field('sort');
  51. $show->field('created_at');
  52. $show->field('updated_at');
  53. });
  54. }
  55. /**
  56. * Make a form builder.
  57. *
  58. * @return Form
  59. */
  60. protected function form()
  61. {
  62. return Form::make(new Notice(), function (Form $form) {
  63. //不允许查看非自己的数据
  64. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  65. return $form->response()->error('数据不存在');
  66. }
  67. $form->display('id');
  68. $form->text('title');
  69. $form->editor('content');
  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', 'created_at', 'updated_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. }