sendNotice.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. foreach ($collection as $model) {
  13. $openid = User::where('id',$model->id)->value('openid');
  14. $content = request('content');
  15. $remark= request('remark');
  16. // $openid = 'oVxTzvgYlGktIDZXwfLMLQ01Tr5s'; 自己
  17. if(empty($openid)) continue;
  18. $this->send($openid,$content,$remark);
  19. }
  20. return $this->response()->success('发送成功')->refresh();
  21. }
  22. public function form()
  23. {
  24. $this->text('title','标题')->value('社区通知')->disable();
  25. $this->textarea('content','内容')->rows(4);
  26. $this->textarea('remark','备注')->rows(3);
  27. }
  28. public function send($open_id,$cotent, $remark)
  29. {
  30. if(empty($open_id)) return true;
  31. $config = [
  32. 'app_id' => 'wx1c2357232cd25f65',
  33. 'secret' => 'c8cab53e4e52234ed1bc2abbdeaba57d',
  34. // 'app_id' => 'wx13bedfcc62e9bab0',
  35. // 'secret' => '175e5518b6426dd12d3096f24ca68fb8',
  36. 'response_type' => 'array'
  37. ];
  38. $app = Factory::officialAccount($config);
  39. if(!isset($user['openid'])) return true;
  40. $res = $app->template_message->send([
  41. 'touser' => $open_id,
  42. 'template_id' => 'GWIu1FBBA8SOdOwEbXRAmBL9LlYJP_H6EJUC7rv-YiQ',
  43. 'url' => 'https://t5.9026.com',
  44. // 'miniprogram' => [
  45. // 'appid' => 'wx6131f74e623bf6bf',
  46. // 'pagepath' => '/pages/index/index',
  47. // ],
  48. 'data' => [
  49. 'first'=>'社区通知',
  50. 'cardNumber' => '测试服务服务包',
  51. 'type' => '门诊',
  52. 'address' => '华西医院',
  53. 'VIPPhone' => 18719141830,
  54. 'VIPName' => '测试服务包',
  55. 'expDate' => '2021年12月31号',
  56. 'remark' => '请咨询13912345678。',
  57. ],
  58. ]);
  59. return true;
  60. }
  61. public function send2($user_ids,$number,$content){
  62. if(!is_array($user_ids)) {
  63. $user_ids = [$user_ids];
  64. }
  65. $app = app('wechat.official_account');
  66. foreach($user_ids as $user_id) {
  67. $user = (new User())->find($user_id);
  68. if(empty($user) || empty($user['open_id'])) continue;
  69. $official_open_id = $user->getOfficialOpenId($user['union_id']);
  70. if(empty($official_open_id)) continue;
  71. $result=$app->template_message->send([
  72. 'touser' => $official_open_id,
  73. 'template_id' => '4x80cGpV2boFdY7fm4QpsPBNo-FLytKjQGJKtZq_nr4',
  74. 'miniprogram' => [
  75. 'appid' => env('WECHAT_MINI_PROGRAM_APPID'),
  76. 'pagepath' => 'pages/index/index',
  77. ],
  78. 'data' => [
  79. 'first' => '作业许可状态改变',
  80. 'keyword1' => $number,
  81. 'keyword2' => $content,
  82. 'keyword3' => $user->name,
  83. 'remark' => '备注'
  84. ],
  85. ]);
  86. var_dump($result);
  87. }
  88. return true;
  89. }
  90. }