|
@@ -7,7 +7,6 @@ use Cache;
|
|
|
use Helpers;
|
|
|
use Http;
|
|
|
use Illuminate\Http\Request;
|
|
|
-use Illuminate\Mail\Markdown;
|
|
|
use Illuminate\Notifications\Notification;
|
|
|
use Log;
|
|
|
use Str;
|
|
@@ -17,29 +16,39 @@ class WeChatChannel
|
|
|
private $access_token;
|
|
|
|
|
|
public function __construct()
|
|
|
+ {
|
|
|
+ $this->access_token = $this->getAccessToken();
|
|
|
+ }
|
|
|
+
|
|
|
+ private function getAccessToken()
|
|
|
{
|
|
|
if (Cache::has('wechat_access_token')) {
|
|
|
- $this->access_token = Cache::get('wechat_access_token');
|
|
|
+ $access_token = Cache::get('wechat_access_token');
|
|
|
} else {
|
|
|
// https://work.weixin.qq.com/api/doc/90000/90135/91039
|
|
|
$response = Http::get('https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid='.sysConfig('wechat_cid').'&corpsecret='.sysConfig('wechat_secret'));
|
|
|
if ($response->ok() && isset($response->json()['access_token'])) {
|
|
|
- $this->access_token = $response->json()['access_token'];
|
|
|
+ $access_token = $response->json()['access_token'];
|
|
|
Cache::put('wechat_access_token', $response->json()['access_token'], 7200); // 2小时
|
|
|
} else {
|
|
|
Log::critical('Wechat消息推送异常:获取access_token失败!'.PHP_EOL.'携带访问参数:'.$response->body());
|
|
|
abort(400);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ return $access_token ?? null;
|
|
|
}
|
|
|
|
|
|
public function send($notifiable, Notification $notification)
|
|
|
- {
|
|
|
+ { // route('message.show', ['type' => 'markdownMsg', 'msgId' => ''])
|
|
|
$message = $notification->toCustom($notifiable);
|
|
|
+ if (! $this->access_token) {
|
|
|
+ $this->access_token = $this->getAccessToken();
|
|
|
+ }
|
|
|
|
|
|
$url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='.$this->access_token;
|
|
|
|
|
|
- if (isset($message['button'])) {
|
|
|
+ if (isset($message['button'])) { // 按钮交互型
|
|
|
// 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
|
|
|
$body = [
|
|
|
'touser' => '@all',
|
|
@@ -66,18 +75,27 @@ class WeChatChannel
|
|
|
],
|
|
|
],
|
|
|
];
|
|
|
- } else {
|
|
|
+ } elseif (isset($message['url_type'])) { // 文本卡片
|
|
|
+ $msgId = Str::uuid(); // 生成对公消息查询URL
|
|
|
$body = [
|
|
|
'touser' => '@all',
|
|
|
'agentid' => sysConfig('wechat_aid'),
|
|
|
'msgtype' => 'textcard',
|
|
|
'textcard' => [
|
|
|
'title' => $message['title'],
|
|
|
- 'description' => Markdown::parse($message['content'])->toHtml(),
|
|
|
- 'url' => route('admin.index'),
|
|
|
- 'btntxt' => '',
|
|
|
+ 'description' => '请点击下方按钮【查看详情】',
|
|
|
+ 'url' => route('message.show', ['type' => $message['url_type'], $msgId]),
|
|
|
+ 'btntxt' => '查看详情',
|
|
|
],
|
|
|
];
|
|
|
+ } else { // 文本消息
|
|
|
+ $body = [
|
|
|
+ 'touser' => '@all',
|
|
|
+ 'agentid' => sysConfig('wechat_aid'),
|
|
|
+ 'msgtype' => 'text',
|
|
|
+ 'text' => ['content' => $message['content']],
|
|
|
+ 'duplicate_check_interval' => 600,
|
|
|
+ ];
|
|
|
}
|
|
|
|
|
|
$response = Http::timeout(15)->withBody(json_encode($body, JSON_UNESCAPED_UNICODE), 'application/json; charset=utf-8')->post($url);
|
|
@@ -86,12 +104,12 @@ class WeChatChannel
|
|
|
if ($response->ok()) {
|
|
|
$ret = $response->json();
|
|
|
if (! $ret['errcode'] && $ret['errmsg'] === 'ok') {
|
|
|
- Helpers::addNotificationLog($message['title'], $message['content'] ?? var_export($message['body'], true), 5);
|
|
|
+ Helpers::addNotificationLog($message['title'], $message['content'] ?? var_export($message['body'], true), 5, 1, null, $msgId ?? null);
|
|
|
|
|
|
return $ret;
|
|
|
}
|
|
|
// 发送失败
|
|
|
- Helpers::addNotificationLog($message['title'], $message['content'] ?? var_export($message['body'], true), 5, 'admin', -1, $ret ? $ret['errmsg'] : '未知');
|
|
|
+ Helpers::addNotificationLog($message['title'], $message['content'] ?? var_export($message['body'], true), 5, -1, $ret ? $ret['errmsg'] : '未知');
|
|
|
|
|
|
return false;
|
|
|
}
|