Browse Source

消息提示

cqp 1 day ago
parent
commit
3afbb99f38

+ 28 - 0
app/Http/Controllers/Api/TestController.php

@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Api;
 
 
 use App\Jobs\ProcessDataJob;
 use App\Jobs\ProcessDataJob;
 use App\Model\Record;
 use App\Model\Record;
+use App\Service\EnterpriseWechatService;
 use App\Service\U8ThirdPartyService;
 use App\Service\U8ThirdPartyService;
 
 
 class TestController extends BaseController
 class TestController extends BaseController
@@ -27,4 +28,31 @@ class TestController extends BaseController
         }
         }
         dd('ok');
         dd('ok');
     }
     }
+
+    public function sendNotification()
+    {
+        $wechatService = new EnterpriseWechatService();
+
+        // 1. 目标员工的企业微信 userid(可在企业微信通讯录查看)
+        $userId = 'chen';
+
+        // 2. 组装卡片标题
+        $title = '台风防汛紧急通知';
+
+        // 3. 组装卡片内容(可以自由排版,比微信模板消息爽多了)
+        $description = "<div class=\"gray\">2026年7月10日</div>"
+            . "<div class=\"highlight\">【重要提醒】第9号台风“巴威”正在逼近!</div>"
+            . "<br/>"
+            . "当前状态:温州市已启动防台风Ⅱ级应急响应<br/>"
+            . "防御要求:沿海大闸已密集泄洪,请各部门排查低洼处积水隐患,检查门窗是否关紧。<br/>"
+            . "<br/>"
+            . "通知部门:行政安全部";
+
+        // 4. 点击卡片要跳转的系统系统链接
+        $url = 'https://your-system.com/dashboard';
+
+        // 5. 调用你刚才写好的方法
+        list($status, $msg) = $wechatService->sendTextCard($userId, $title, $description, $url);
+        dd($status, $msg);
+    }
 }
 }

+ 48 - 0
app/Service/EnterpriseWechatService.php

@@ -3,11 +3,14 @@
 namespace App\Service;
 namespace App\Service;
 
 
 use EasyWeChat\Factory;
 use EasyWeChat\Factory;
+use EasyWeChat\Kernel\Messages\TextCard;
 use Exception;
 use Exception;
+use Illuminate\Support\Facades\Log;
 
 
 class EnterpriseWechatService
 class EnterpriseWechatService
 {
 {
     private $app;
     private $app;
+    private $agentId; // 提出来作为类属性,发消息时需要用到
 
 
     public function __construct()
     public function __construct()
     {
     {
@@ -36,4 +39,49 @@ class EnterpriseWechatService
     {
     {
         return $this->app->oa;
         return $this->app->oa;
     }
     }
+
+    /**
+     * 发送文本卡片消息 (返回详细的错误提示)
+     *
+     * @param string $userId 接收人的企业微信userid(多个用竖线隔开)
+     * @param string $title 标题
+     * @param string $description 内容
+     * @param string $url 跳转链接
+     * @return array [bool, string]
+     */
+    public function sendTextCard($userId, $title, $description, $url)
+    {
+        try {
+            // EasyWeChat 4.x 的 TextCard 构造函数只接收一个规范的数组
+            $attributes = [
+                'title'       => $title,
+                'description' => $description,
+                'url'         => $url ?? '',
+            ];
+
+            // 如果 url 不为空,才加入 btntxt
+            if (!empty($url)) {
+                $attributes['btntxt'] = '点击查看详情';
+            }
+
+            // 实例化时,将整个数组作为第 1 个参数传入
+            $cardElement = new TextCard($attributes);
+
+            // 直接把对象传给 message() 方法发送
+            $response = $this->app->messenger
+                ->message($cardElement)
+                ->toUser($userId)
+                ->send();
+
+            // 如果企业微信返回 errcode 且为 0,说明成功
+            if (isset($response['errcode']) && $response['errcode'] === 0)  return [true, ''];
+
+            $errorCode = $response['errcode'] ?? '未知代码';
+            $errorMsg = $response['errmsg'] ?? '未知错误';
+            return [false, "企业微信返回错误 [Code: {$errorCode}]: {$errorMsg}"];
+
+        } catch (Exception $e) {
+            return [false,  $e->getMessage() . '|' . $e->getLine() . '|' . $e->getFile()];
+        }
+    }
 }
 }

+ 3 - 0
config/enterprise_wechat.php

@@ -8,6 +8,9 @@ return [
         'secret'   => env('WEWORK_SECRET'),
         'secret'   => env('WEWORK_SECRET'),
         'token'    => env('WEWORK_TOKEN'),
         'token'    => env('WEWORK_TOKEN'),
         'aes_key'  => env('WEWORK_AES_KEY'),
         'aes_key'  => env('WEWORK_AES_KEY'),
+        'http' => [
+            'verify' => false,
+        ],
     ],
     ],
 ];
 ];