sendNotice.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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','内容')->value('社区通州')->rows(4);
  26. $this->textarea('remark','备注')->value('社区通州')->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' => 'wx1c2357232cd25f65',
  35. // 'secret' => 'c8cab53e4e52234ed1bc2abbdeaba57d',
  36. // 'app_id' => 'wx13bedfcc62e9bab0',
  37. // 'secret' => '175e5518b6426dd12d3096f24ca68fb8',
  38. 'response_type' => 'array'
  39. ];
  40. $app = Factory::officialAccount($config);
  41. $app->template_message->send([
  42. 'touser' => $open_id,
  43. 'template_id' => '3LUhWGlyiljxrT3Jh8orwQZ2LSHjfRs9SIHaB40O6q0',
  44. 'url' => 'http://47.94.228.245',
  45. 'miniprogram' => [
  46. 'appid' => 'wx1c2357232cd25f65',
  47. 'pagepath' => 'pages/index/index',
  48. ],
  49. 'data' => [
  50. 'first'=>'社区通知',
  51. 'key1' => $cotent,
  52. 'key2' => 18719141830,
  53. 'key3' => '开发工程师',
  54. 'key4' => '思维定制',
  55. 'key5' => '2020-12-12',
  56. ],
  57. ]);
  58. }
  59. public function send2($user_ids,$number,$content){
  60. if(!is_array($user_ids)) {
  61. $user_ids = [$user_ids];
  62. }
  63. $app = app('wechat.official_account');
  64. foreach($user_ids as $user_id) {
  65. $user = (new User())->find($user_id);
  66. if(empty($user) || empty($user['open_id'])) continue;
  67. $official_open_id = $user->getOfficialOpenId($user['union_id']);
  68. if(empty($official_open_id)) continue;
  69. $result=$app->template_message->send([
  70. 'touser' => $official_open_id,
  71. 'template_id' => '4x80cGpV2boFdY7fm4QpsPBNo-FLytKjQGJKtZq_nr4',
  72. 'miniprogram' => [
  73. 'appid' => env('WECHAT_MINI_PROGRAM_APPID'),
  74. 'pagepath' => 'pages/index/index',
  75. ],
  76. 'data' => [
  77. 'first' => '作业许可状态改变',
  78. 'keyword1' => $number,
  79. 'keyword2' => $content,
  80. 'keyword3' => $user->name,
  81. 'remark' => '备注'
  82. ],
  83. ]);
  84. var_dump($result);
  85. }
  86. return true;
  87. }
  88. }