sendNotice.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace App\Admin\Actions\Community\Notice;
  3. use App\User;
  4. use EasyWeChat\Factory;
  5. use Encore\Admin\Actions\BatchAction;
  6. use Illuminate\Database\Eloquent\Collection;
  7. class sendNotice extends BatchAction
  8. {
  9. public $name = '群发通知';
  10. public function handle(Collection $collection)
  11. {
  12. $info = $collection->toArray();
  13. $ids = array_unique(array_column($info,'id'));
  14. foreach ($ids as $id) {
  15. $openid = User::where('id',$id)->value('openid');
  16. $content = request('content');
  17. $remark= request('remark');
  18. $stime= request('stime');
  19. $type= request('type',1);
  20. // $openid = 'oYmUA5A1OIqtpA1XSrw35tbjtv1w';
  21. $service_arr = [1=>'儿保服务',2=>'疫苗接种服务'];
  22. $service_name = $service_arr[$type];
  23. //没有openid 直接不发送
  24. if(empty($openid)) continue;
  25. $template_arr = [
  26. 1=>'CP3AxrgS-cbW1da8QlIDFcxd-H0RStMEuXdqNRePLoc',
  27. 2=>'xpcaMFXI0Kc9U12o3D6CGPa7ASTpOZJwXJm2mlip6Zo'
  28. ];
  29. $tempId = $template_arr[$type];
  30. $msg['content'] = $content;
  31. $msg['service_date'] = $stime;
  32. $msg['remark'] = $remark;
  33. $msg['service_name'] = $service_name;
  34. $this->send($openid,$tempId,$msg);
  35. }
  36. return $this->response()->success('发送成功')->refresh();
  37. }
  38. public function form()
  39. {
  40. // xpcaMFXI0Kc9U12o3D6CGPa7ASTpOZJwXJm2mlip6Zo 接种服务提醒
  41. // first,keyword1,keword2,remark;
  42. // CP3AxrgS-cbW1da8QlIDFcxd-H0RStMEuXdqNRePLoc 儿保服务提醒
  43. // 服务项目:XX疫苗接种
  44. // 服务日期:后台设置的服务时间
  45. $this->textarea('content','内容')->rows(4);
  46. $this->select('type','服务类型')->options([1=>'儿保',2=>'计免']);
  47. $this->date('stime','服务时间');
  48. $this->textarea('remark','备注')->rows(3);
  49. }
  50. public function send($open_id,$tempId,$msg)
  51. {
  52. $template = [
  53. 'touser' => $open_id,
  54. 'mp_template_msg' => [
  55. 'appid' => env('OFFICIAL_WECHAT_APPID'),
  56. 'template_id' => $tempId,
  57. 'url' => '',
  58. 'miniprogram' => [
  59. 'appid' => env('WECHAT_APPID', 'wxd41dd232837996c4'),
  60. 'pagepath' => '',
  61. ],
  62. 'data' => [
  63. 'first' => [
  64. 'value' => $msg['content'],
  65. ],
  66. 'keyword1' => [
  67. 'value' => $msg['service_name'],
  68. ],
  69. 'keyword2' => [
  70. 'value' => $msg['service_date'],
  71. ],
  72. 'remark' => [
  73. 'value' => $msg['remark'],
  74. ],
  75. ],
  76. ],
  77. ];
  78. $app = Factory::miniProgram(config('config.wechat_small_program'));
  79. $app->uniform_message->send($template);
  80. return true;
  81. }
  82. public function send2($user_ids,$number,$content){
  83. if(!is_array($user_ids)) {
  84. $user_ids = [$user_ids];
  85. }
  86. $app = app('wechat.official_account');
  87. foreach($user_ids as $user_id) {
  88. $user = (new User())->find($user_id);
  89. if(empty($user) || empty($user['open_id'])) continue;
  90. $official_open_id = $user->getOfficialOpenId($user['union_id']);
  91. if(empty($official_open_id)) continue;
  92. $result=$app->template_message->send([
  93. 'touser' => $official_open_id,
  94. 'template_id' => '4x80cGpV2boFdY7fm4QpsPBNo-FLytKjQGJKtZq_nr4',
  95. 'miniprogram' => [
  96. 'appid' => env('WECHAT_MINI_PROGRAM_APPID'),
  97. 'pagepath' => 'pages/index/index',
  98. ],
  99. 'data' => [
  100. 'first' => '作业许可状态改变',
  101. 'keyword1' => $number,
  102. 'keyword2' => $content,
  103. 'keyword3' => $user->name,
  104. 'remark' => '备注'
  105. ],
  106. ]);
  107. var_dump($result);
  108. }
  109. return true;
  110. }
  111. }