sendNotice.php 4.4 KB

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