ソースを参照

Merge pull request #211 from abwuge/main

新增企业微信用户ID
luolongfei 2 年 前
コミット
c826a7fc09
3 ファイル変更11 行追加1 行削除
  1. 3 0
      .env.example
  2. 1 0
      config.php
  3. 7 1
      libs/MessageServices/WeChat.php

+ 3 - 0
.env.example

@@ -89,6 +89,9 @@ WECHAT_CORP_SECRET=''
 # 应用 ID
 WECHAT_AGENT_ID=''
 
+# 用户 ID 指定接收消息的成员,成员ID列表(多个接收者用‘|’分隔,最多支持1000个)。指定为"@all",则向该企业应用的全部成员发送
+WECHAT_USER_ID='@all'
+
 # 是否启用企业微信送信功能 1:启用 0:不启用 Whether to enable the enterprise WeChat message push function 1: Enable 0: Do not enable
 WECHAT_ENABLE=0
 ######################  end 企业微信  #########################

+ 1 - 0
config.php

@@ -53,6 +53,7 @@ return [
             'corp_id' => env('WECHAT_CORP_ID'), // 企业 ID
             'corp_secret' => env('WECHAT_CORP_SECRET'), // 企业微信应用的凭证密钥
             'agent_id' => (int)env('WECHAT_AGENT_ID'), // 企业微信应用 ID
+            'user_id' => env('WECHAT_USER_ID'), // 企业微信用户ID
             'enable' => (int)env('WECHAT_ENABLE'), // 是否启用,默认不启用
             'not_enabled_tips' => env('WECHAT_CORP_ID') && env('WECHAT_CORP_SECRET') && env('WECHAT_AGENT_ID'), // 提醒未启用
             'class' => \Luolongfei\Libs\MessageServices\WeChat::class,

+ 7 - 1
libs/MessageServices/WeChat.php

@@ -31,6 +31,11 @@ class WeChat extends MessageGateway
      */
     protected $agentId;
 
+        /**
+     * @var integer 企业微信用户 ID
+     */
+    protected $userId;
+
     /**
      * @var Client
      */
@@ -46,6 +51,7 @@ class WeChat extends MessageGateway
         $this->corpId = config('message.wechat.corp_id');
         $this->corpSecret = config('message.wechat.corp_secret');
         $this->agentId = config('message.wechat.agent_id');
+        $this->userId = config('message.wechat.user_id');
 
         $this->accessTokenFile = DATA_PATH . DS . 'wechat_access_token.txt';
 
@@ -292,7 +298,7 @@ class WeChat extends MessageGateway
             $accessToken = $this->getAccessToken();
 
             $body = [
-                'touser' => '@all', // 可直接通过此地址获取 userId,指定接收用户,多个用户用“|”分割 https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=ACCESS_TOKEN&fetch_child=FETCH_CHILD&department_id=1
+                'touser' => $this->userId, // 可直接通过此地址获取 userId,指定接收用户,多个用户用“|”分割 https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=ACCESS_TOKEN&fetch_child=FETCH_CHILD&department_id=1
                 'msgtype' => 'text', // 消息类型,text 类型支持 a 标签以及 \n 换行,基本满足需求。由于腾讯要求 markdown 语法必须使用 企业微信APP 才能查看,不想安装,故弃之
                 'agentid' => $this->agentId, // 企业应用的 ID,整型,可在应用的设置页面查看
                 'text' => [