sendNotice.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. dd($model->toArray());
  14. $openid = User::where('id',$model->id)->value('openid');
  15. $content = request('content');
  16. $remark= request('remark');
  17. // $openid = 'oVxTzvgYlGktIDZXwfLMLQ01Tr5s'; 自己
  18. if(empty($openid)) continue;
  19. // $this->send($openid,$content,$remark);
  20. }
  21. return $this->response()->success('发送成功')->refresh();
  22. }
  23. public function form()
  24. {
  25. $this->text('title','标题')->value('社区通知')->disable();
  26. $this->textarea('content','内容')->rows(4);
  27. $this->textarea('remark','备注')->rows(3);
  28. }
  29. public function send($open_id,$cotent, $remark)
  30. {
  31. if(empty($open_id)) return true;
  32. $config = [
  33. 'app_id' => 'wx1c2357232cd25f65',
  34. 'secret' => 'c8cab53e4e52234ed1bc2abbdeaba57d',
  35. // 'app_id' => 'wx13bedfcc62e9bab0',
  36. // 'secret' => '175e5518b6426dd12d3096f24ca68fb8',
  37. 'response_type' => 'array'
  38. ];
  39. $app = Factory::officialAccount($config);
  40. $user = $app->user->get('oVxTzvgYlGktIDZXwfLMLQ01Tr5sss');
  41. if(!isset($user['openid'])) return true;
  42. $app->template_message->send([
  43. 'touser' => 'oVxTzvgYlGktIDZXwfLMLQ01Tr5s',
  44. 'template_id' => '3LUhWGlyiljxrT3Jh8orwQZ2LSHjfRs9SIHaB40O6q0',
  45. // 'url' => 'https://t5.9026.com',
  46. 'data' => [
  47. 'first'=>'社区通知',
  48. 'keyword1' => '王海军',
  49. 'keyword2' => 18719141830,
  50. 'keyword3' => '开发工程师',
  51. 'keyword4' => '思维定制',
  52. 'keyword5' => '2020-12-12',
  53. ],
  54. ]);
  55. return true;
  56. }
  57. public function send2($user_ids,$number,$content){
  58. if(!is_array($user_ids)) {
  59. $user_ids = [$user_ids];
  60. }
  61. $app = app('wechat.official_account');
  62. foreach($user_ids as $user_id) {
  63. $user = (new User())->find($user_id);
  64. if(empty($user) || empty($user['open_id'])) continue;
  65. $official_open_id = $user->getOfficialOpenId($user['union_id']);
  66. if(empty($official_open_id)) continue;
  67. $result=$app->template_message->send([
  68. 'touser' => $official_open_id,
  69. 'template_id' => '4x80cGpV2boFdY7fm4QpsPBNo-FLytKjQGJKtZq_nr4',
  70. 'miniprogram' => [
  71. 'appid' => env('WECHAT_MINI_PROGRAM_APPID'),
  72. 'pagepath' => 'pages/index/index',
  73. ],
  74. 'data' => [
  75. 'first' => '作业许可状态改变',
  76. 'keyword1' => $number,
  77. 'keyword2' => $content,
  78. 'keyword3' => $user->name,
  79. 'remark' => '备注'
  80. ],
  81. ]);
  82. var_dump($result);
  83. }
  84. return true;
  85. }
  86. }