NoticeController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Admin\Actions\Notice\NoticeSend;
  4. use App\Models\Notice;
  5. use App\Models\User;
  6. use App\Services\JPushService;
  7. use Dcat\Admin\Form;
  8. use Dcat\Admin\Grid;
  9. use Dcat\Admin\Show;
  10. use Dcat\Admin\Http\Controllers\AdminController;
  11. use Faker\Factory;
  12. class NoticeController extends AdminController
  13. {
  14. /**
  15. * Make a grid builder.
  16. *
  17. * @return Grid
  18. */
  19. protected function grid()
  20. {
  21. $grid = new Grid(new Notice());
  22. $grid->model()->orderBy('id','desc');
  23. $grid->column('id')->sortable();
  24. $grid->column('title');
  25. $grid->column('content')->limit(60);
  26. $grid->column('users')->using(['全部用户','男性用户','女性用户'])->label(['success','primary','warning']);
  27. $grid->column('created_at');
  28. //$grid->column('updated_at')->sortable();
  29. $grid->filter(function (Grid\Filter $filter) {
  30. $filter->equal('id');
  31. });
  32. //操作管理
  33. // $grid->actions(function (Grid\Displayers\Actions $actions) {
  34. // $actions->append(new NoticeSend(User::class));
  35. // });
  36. return $grid;
  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 Notice(), function (Show $show) {
  48. $show->field('id');
  49. $show->field('title');
  50. $show->field('content')->unescape();
  51. $show->field('users')->using(['全部用户','男性用户','女性用户']);
  52. $show->field('created_at');
  53. $show->field('updated_at');
  54. });
  55. }
  56. /**
  57. * Make a form builder.
  58. *
  59. * @return Form
  60. */
  61. protected function form()
  62. {
  63. return Form::make(new Notice(), function (Form $form) {
  64. $form->display('id');
  65. $form->text('title');
  66. $form->textarea('content');
  67. $form->radio('users')->options([0=>'全部用户'
  68. // ,1=>'男性用户',2=>"女性用户"
  69. ])->default(0);
  70. $form->display('created_at');
  71. $form->display('updated_at');
  72. $form->saved(function ($res){
  73. dd($res);
  74. //推送消息
  75. JPushService::pushNotify([
  76. //标题
  77. 'title' => $res->title,
  78. //内容
  79. 'content' => $res->content,
  80. //设备标识,跟设备相关
  81. 'reg_id' => 'xxxxxxxxxxx',
  82. //扩展字段
  83. 'extras' => [
  84. 'type' => 'notice',
  85. ],
  86. //推送类型
  87. 'type' => JPushService::PUSH_TYPE_ALL,
  88. ]);
  89. //给用户添加消息记录
  90. User::query()->get(['id']);
  91. });
  92. });
  93. }
  94. }