WeChatChannel.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Channels;
  3. use App\Channels\Components\WeChat;
  4. use Cache;
  5. use Helpers;
  6. use Http;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Mail\Markdown;
  9. use Illuminate\Notifications\Notification;
  10. use Log;
  11. use Str;
  12. class WeChatChannel
  13. {
  14. private $access_token;
  15. public function __construct()
  16. {
  17. if (Cache::has('wechat_access_token')) {
  18. $this->access_token = Cache::get('wechat_access_token');
  19. } else {
  20. // https://work.weixin.qq.com/api/doc/90000/90135/91039
  21. $response = Http::get('https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid='.sysConfig('wechat_cid').'&corpsecret='.sysConfig('wechat_secret'));
  22. if ($response->ok() && isset($response->json()['access_token'])) {
  23. $this->access_token = $response->json()['access_token'];
  24. Cache::put('wechat_access_token', $response->json()['access_token'], 7200); // 2小时
  25. } else {
  26. Log::critical('Wechat消息推送异常:获取access_token失败!'.PHP_EOL.'携带访问参数:'.$response->body());
  27. abort(400);
  28. }
  29. }
  30. }
  31. public function send($notifiable, Notification $notification)
  32. {
  33. $message = $notification->toCustom($notifiable);
  34. $url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='.$this->access_token;
  35. if (isset($message['button'])) {
  36. // https://work.weixin.qq.com/api/doc/90000/90135/90236#%E6%8C%89%E9%92%AE%E4%BA%A4%E4%BA%92%E5%9E%8B
  37. $body = [
  38. 'touser' => '@all',
  39. 'msgtype' => 'template_card',
  40. 'agentid' => sysConfig('wechat_aid'),
  41. 'template_card' => [
  42. 'card_type' => 'button_interaction',
  43. 'main_title' => ['title' => $message['title']],
  44. 'horizontal_content_list' => $message['body'],
  45. 'task_id' => time().Str::random(),
  46. 'button_list' => [
  47. [
  48. 'type' => 1,
  49. 'text' => '否 決',
  50. 'style' => 3,
  51. 'url' => $message['button'][0],
  52. ],
  53. [
  54. 'type' => 1,
  55. 'text' => '确 认',
  56. 'style' => 1,
  57. 'url' => $message['button'][1],
  58. ],
  59. ],
  60. ],
  61. ];
  62. } else {
  63. $body = [
  64. 'touser' => '@all',
  65. 'agentid' => sysConfig('wechat_aid'),
  66. 'msgtype' => 'text',
  67. 'text' => ['content' => Markdown::parse($message['content'])->toHtml()],
  68. ];
  69. }
  70. $response = Http::timeout(15)->post($url, $body);
  71. // 发送成功
  72. if ($response->ok()) {
  73. $ret = $response->json();
  74. if (! $ret['errcode'] && $ret['errmsg'] === 'ok') {
  75. Helpers::addNotificationLog($message['title'], $message['content'] ?? var_export($message['body'], true), 5);
  76. return $ret;
  77. }
  78. // 发送失败
  79. Helpers::addNotificationLog($message['title'], $message['content'] ?? var_export($message['body'], true), 5, 'admin', -1, $ret ? $ret['errmsg'] : '未知');
  80. return false;
  81. }
  82. // 发送错误
  83. Log::critical('Wechat消息推送异常:'.var_export($response, true));
  84. return false;
  85. }
  86. public function verify(Request $request)
  87. {
  88. $errCode = (new WeChat())->VerifyURL($request->input('msg_signature'), $request->input('timestamp'), $request->input('nonce'), $request->input('echostr'), $sEchoStr);
  89. if ($errCode === 0) {
  90. exit($sEchoStr);
  91. }
  92. Log::critical('Wechat互动消息推送异常:'.var_export($errCode, true));
  93. }
  94. }