Browse Source

Fix 企业微信卡片消息中文乱码的问题

兔姬桑 4 years ago
parent
commit
6467bacad5
4 changed files with 236 additions and 159 deletions
  1. 9 4
      app/Channels/Components/WeChat.php
  2. 10 5
      app/Channels/WeChatChannel.php
  3. 1 2
      composer.json
  4. 216 148
      composer.lock

+ 9 - 4
app/Channels/Components/WeChat.php

@@ -2,7 +2,9 @@
 
 namespace App\Channels\Components;
 
+use DOMDocument;
 use Exception;
+use Log;
 
 class WeChat
 {
@@ -39,7 +41,7 @@ class WeChat
 
             return [0, sha1(implode($array))];
         } catch (Exception $e) {
-            echo $e->__toString()."\n";
+            Log::critical('企业微信消息推送异常:'.var_export($e->getMessage(), true));
 
             return [-40003, null]; // ComputeSignatureError
         }
@@ -146,7 +148,7 @@ class WeChat
 
             return [0, $encrypt];
         } catch (Exception $e) {
-            echo $e."\n";
+            Log::critical('企业微信消息推送异常:'.var_export($e->getMessage(), true));
 
             return [-40002, null]; // ParseXmlError
         }
@@ -218,7 +220,7 @@ class Prpcrypt
 
             return [0, $encrypted];
         } catch (Exception $e) {
-            echo $e->__toString();
+            Log::critical('企业微信消息推送异常:'.var_export($e->getMessage(), true));
 
             return [-40006, null]; // EncryptAESError
         }
@@ -230,6 +232,8 @@ class Prpcrypt
             //解密
             $decrypted = openssl_decrypt($encrypted, 'AES-256-CBC', $this->key, OPENSSL_ZERO_PADDING, $this->iv);
         } catch (Exception $e) {
+            Log::critical('企业微信消息推送异常:'.var_export($e->getMessage(), true));
+
             return [-40007, null]; // DecryptAESError
         }
         try {
@@ -245,7 +249,8 @@ class Prpcrypt
             $xml_content = substr($content, 4, $xml_len);
             $from_receiveId = substr($content, $xml_len + 4);
         } catch (Exception $e) {
-            echo $e->__toString();
+            // 发送错误
+            Log::critical('企业微信消息推送异常:'.var_export($e->getMessage(), true));
 
             return [-40008, null]; // IllegalBuffer
         }

+ 10 - 5
app/Channels/WeChatChannel.php

@@ -68,14 +68,19 @@ class WeChatChannel
             ];
         } else {
             $body = [
-                'touser'  => '@all',
-                'agentid' => sysConfig('wechat_aid'),
-                'msgtype' => 'text',
-                'text'    => ['content' => Markdown::parse($message['content'])->toHtml()],
+                'touser'   => '@all',
+                'agentid'  => sysConfig('wechat_aid'),
+                'msgtype'  => 'textcard',
+                'textcard' => [
+                    'title'       => $message['title'],
+                    'description' => Markdown::parse($message['content'])->toHtml(),
+                    'url'         => route('admin.index'),
+                    'btntxt'      => '',
+                ],
             ];
         }
 
-        $response = Http::timeout(15)->post($url, $body);
+        $response = Http::timeout(15)->withBody(json_encode($body, JSON_UNESCAPED_UNICODE), 'application/json; charset=utf-8')->post($url);
 
         // 发送成功
         if ($response->ok()) {

+ 1 - 2
composer.json

@@ -45,8 +45,7 @@
     "zbrettonye/hcaptcha": "^1.1",
     "zbrettonye/no-captcha": "^1.1",
     "zoujingli/ip2region": "^1.0",
-    "ext-dom": "*",
-    "ext-mcrypt": "*"
+    "ext-dom": "*"
   },
   "require-dev": {
     "arcanedev/laravel-lang": "^8.0",

File diff suppressed because it is too large
+ 216 - 148
composer.lock


Some files were not shown because too many files changed in this diff