WeChatChannel.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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' => 'textcard',
  67. 'textcard' => [
  68. 'title' => $message['title'],
  69. 'description' => Markdown::parse($message['content'])->toHtml(),
  70. 'url' => route('admin.index'),
  71. 'btntxt' => '',
  72. ],
  73. ];
  74. }
  75. $response = Http::timeout(15)->withBody(json_encode($body, JSON_UNESCAPED_UNICODE), 'application/json; charset=utf-8')->post($url);
  76. // 发送成功
  77. if ($response->ok()) {
  78. $ret = $response->json();
  79. if (! $ret['errcode'] && $ret['errmsg'] === 'ok') {
  80. Helpers::addNotificationLog($message['title'], $message['content'] ?? var_export($message['body'], true), 5);
  81. return $ret;
  82. }
  83. // 发送失败
  84. Helpers::addNotificationLog($message['title'], $message['content'] ?? var_export($message['body'], true), 5, 'admin', -1, $ret ? $ret['errmsg'] : '未知');
  85. return false;
  86. }
  87. // 发送错误
  88. Log::critical('Wechat消息推送异常:'.var_export($response, true));
  89. return false;
  90. }
  91. public function verify(Request $request)
  92. {
  93. $errCode = (new WeChat())->VerifyURL($request->input('msg_signature'), $request->input('timestamp'), $request->input('nonce'), $request->input('echostr'), $sEchoStr);
  94. if ($errCode === 0) {
  95. exit($sEchoStr);
  96. }
  97. Log::critical('Wechat互动消息推送异常:'.var_export($errCode, true));
  98. }
  99. }