sendNotice.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. $user = $app->user->get('oVxTzvgYlGktIDZXwfLMLQ01Tr5sss');
  40. if(!isset($user['openid'])) return true;
  41. $app->template_message->send([
  42. 'touser' => 'oVxTzvgYlGktIDZXwfLMLQ01Tr5s',
  43. 'template_id' => '3LUhWGlyiljxrT3Jh8orwQZ2LSHjfRs9SIHaB40O6q0',
  44. // 'url' => 'https://t5.9026.com',
  45. 'data' => [
  46. 'first'=>'社区通知',
  47. 'keyword1' => '王海军',
  48. 'keyword2' => 18719141830,
  49. 'keyword3' => '开发工程师',
  50. 'keyword4' => '思维定制',
  51. 'keyword5' => '2020-12-12',
  52. ],
  53. ]);
  54. return true;
  55. }
  56. public function send2($user_ids,$number,$content){
  57. if(!is_array($user_ids)) {
  58. $user_ids = [$user_ids];
  59. }
  60. $app = app('wechat.official_account');
  61. foreach($user_ids as $user_id) {
  62. $user = (new User())->find($user_id);
  63. if(empty($user) || empty($user['open_id'])) continue;
  64. $official_open_id = $user->getOfficialOpenId($user['union_id']);
  65. if(empty($official_open_id)) continue;
  66. $result=$app->template_message->send([
  67. 'touser' => $official_open_id,
  68. 'template_id' => '4x80cGpV2boFdY7fm4QpsPBNo-FLytKjQGJKtZq_nr4',
  69. 'miniprogram' => [
  70. 'appid' => env('WECHAT_MINI_PROGRAM_APPID'),
  71. 'pagepath' => 'pages/index/index',
  72. ],
  73. 'data' => [
  74. 'first' => '作业许可状态改变',
  75. 'keyword1' => $number,
  76. 'keyword2' => $content,
  77. 'keyword3' => $user->name,
  78. 'remark' => '备注'
  79. ],
  80. ]);
  81. var_dump($result);
  82. }
  83. return true;
  84. }
  85. }