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

138 lines
3.9 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
4 years ago
4 years ago
  1. <?php
  2. namespace App\AdminAgent\Controllers;
  3. use App\AdminAgent\Renderable\SelectUser;
  4. use App\AdminAgent\Repositories\Message;
  5. use App\Models\User;
  6. use Dcat\Admin\Admin;
  7. use Dcat\Admin\Form;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Show;
  10. use Dcat\Admin\Http\Controllers\AdminController;
  11. class MessageController extends AdminController
  12. {
  13. /**
  14. * Make a grid builder.
  15. *
  16. * @return Grid
  17. */
  18. protected function grid()
  19. {
  20. return Grid::make(new Message(), function (Grid $grid) {
  21. $grid->model()->where('agent_id', Admin::user()->id);
  22. $grid->column('id')->sortable();
  23. $grid->column('author');
  24. $grid->column('user_id')
  25. ->if(fn() => $this->user_id == 0)
  26. ->display('所有人');
  27. $grid->column('title');
  28. // $grid->column('is_read')->if(fn($v) => $this->user_id != 0)->bool()->else()->display('所有人接收');
  29. $grid->column('created_at');
  30. $grid->filter(function (Grid\Filter $filter) {
  31. $filter->panel();
  32. $filter->model()->where('agent_id', Admin::user()->id);
  33. $filter->equal('id')->width(2);
  34. $filter->like('title')->width(3);
  35. });
  36. });
  37. }
  38. /**
  39. * Make a show builder.
  40. *
  41. * @param mixed $id
  42. *
  43. * @return Show
  44. */
  45. protected function detail($id)
  46. {
  47. return Show::make($id, new Message(), function (Show $show) {
  48. //不允许查看非自己的数据
  49. if ($show->model()->agent_id != Admin::user()->id) {
  50. Admin::exit('数据不存在');
  51. }
  52. $show->field('id');
  53. $show->field('author');
  54. $show->field('user_id');
  55. $show->field('title');
  56. $show->field('content')->unescape();
  57. /*$show->field('is_read')
  58. ->as(function () {
  59. if ($this->user_id) {
  60. return $this->is_read ? '已读' : '未读';
  61. }
  62. return '所有人接收';
  63. });*/
  64. $show->field('created_at');
  65. });
  66. }
  67. /**
  68. * Make a form builder.
  69. *
  70. * @return Form
  71. */
  72. protected function form()
  73. {
  74. return Form::make(new Message(), function (Form $form) {
  75. //不允许查看非自己的数据
  76. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  77. return $form->response()->error('数据不存在');
  78. }
  79. $form->display('id');
  80. $form->text('author')->default(Admin::user()->name);
  81. $form->selectTable('user_id')
  82. ->from(new SelectUser())
  83. ->model(User::class, 'id', 'nickname')
  84. ->default(0)
  85. ->help('不选择则表示所有人都收到消息');
  86. $form->text('title')->required();
  87. $form->editor('content');
  88. /*if ($form->isCreating()) {
  89. $form->confirm('发布确认', '为了保证消费者利益,所有人接收的消息发布后禁止编辑,确定发布?');
  90. }*/
  91. })->saving(function (Form $form) {
  92. //不允许修改非自己的数据
  93. if ($form->isEditing() && $form->model()->agent_id != Admin::user()->id) {
  94. return $form->response()->error('数据不存在');
  95. }
  96. //消息已读后禁止编辑
  97. /*if ($form->isEditing()) {
  98. if ($form->model()->user_id == 0) {
  99. return $form->response()->error('为了保证消费者利益,所有人接收的消息禁止编辑');
  100. }
  101. if ($form->model()->is_read) {
  102. return $form->response()->error('消息已读,禁止修改');
  103. }
  104. }*/
  105. //特殊字段处理
  106. $form->hidden(['agent_id']);
  107. $form->agent_id = Admin::user()->id;
  108. $form->user_id = $form->user_id ?? 0;
  109. foreach ($form->input() as $k => $v) {
  110. if (is_null($v)) {
  111. $form->$k = '';
  112. }
  113. }
  114. //不允许编辑的字段
  115. $form->ignore(['id', 'status', 'created_at', 'updated_at', 'deleted_at']);
  116. })->deleting(function (Form $form) {
  117. //不允许删除非自己的数据
  118. if (array_filter($form->model()->toArray(), fn($v) => $v['agent_id'] != Admin::user()->id)) {
  119. return $form->response()->error('数据不存在');
  120. }
  121. });
  122. }
  123. }