企業微信批量根據姓名查找工號


公司產品中有一個導入excel發送消息的功能,客戶習慣了之前的根據姓名匹配的方式,自從企業微信屏蔽了姓名之后,這些用戶感覺使用超級不方便,所以嘗試通過批量查找工號方式來匹配。

用戶發送通知時,先根據名字查處工號,再根據工號發送信息,實際使用中要注意重名、不存在問題處理。

一、 通訊錄批量搜索使用文檔

通訊錄批量搜索
請求方式:POST(HTTPS)
請求地址:https://qyapi.weixin.qq.com/cgi-bin/service/contact/batchsearch?provider_access_token=ACCESS_TOKEN

請求包體:

{
    "auth_corpid":"wwxxxxxx",
    "agentid": 1000046,
    "query_request_list":[
        {
            "query_word": "zhangsan",
            "query_type":1,
            "offset":0,
            "limit":50,
            "full_match_field":1
        }
    ]
}
參數說明:

參數	必須	說明
provider_access_token	是	應用提供商的provider_access_token,獲取方法參見服務商的憑證
auth_corpid	是	查詢的企業corpid
agentid	否	應用id,若非0則只返回應用可見范圍內的用戶或者部門信息
query_request_list	是	搜索請求列表,每次搜索列表數量不超過50
query_word	是	搜索關鍵詞。當查詢用戶時應為用戶名稱、名稱拼音或者英文名;當查詢部門時應為部門名稱或者部門名稱拼音
query_type	否	查詢類型 1:查詢用戶,返回用戶userid列表 2:查詢部門,返回部門id列表。 不填該字段或者填0代表同時查詢部門跟用戶
offset	否	查詢的偏移量,每次調用的offset在上一次offset基礎上加上limit
limit	否	查詢返回的最大數量,默認為50,最多為200,查詢返回的數量可能小於limit指定的值
full_match_field	否	如果需要精確匹配用戶名稱或者部門名稱或者英文名,不填則默認為模糊匹配;1:匹配用戶名稱或者部門名稱 2:匹配用戶英文名
權限說明:

agentid為0則返回該服務商授權通訊錄權限范圍內的用戶信息或者部門信息,否則返回指定agentid應用可見范圍內的信息

返回結果:

{
   "errcode": 0,
   "errmsg": "ok",
   "query_result_list":[
    {
            "query_request":
            {
                "query_word": "zhangsan",
                "query_type":1,
                "offset":0,
                "limit":50
            },
            "is_last":false,
            "query_result":{
                "user":{
                    "userid":["zhangshan","lisi"],
                    "open_userid":["wwxxxx","wwxxxa"]
                  },
                "party":{
                    "department_id":[1,2,3]
                  }
            }
        }
   ]
}
參數說明:

參數	說明
errcode	返回碼
errmsg	對返回碼的文本描述內容
is_last	根據該字段判斷是否是最后一頁,若為false,開發者需要使用offset+limit繼續調用
query_result_list	搜索結果列表
query_request	原搜索請求報文
query_result	搜索請求對應的查詢結果
user	返回的用戶信息(通過用戶名稱,拼音匹配)
userid	查詢到的用戶userid
open_userid	查詢到的用戶open_userid
party	返回的部門信息 (通過部門名稱,拼音匹配)
department_id	返回的部門id

  二、封裝C#代碼如下

public class TXLBatchSearchUtil
    {
        //根據姓名查找工號,返回形成姓名:賬號1,賬號2形式
        public static Dictionary<string,List<string>> SearchUserIdByNames(List<string> names, string agentid,string corpid)
        {
            int batchCount = 50;
            Dictionary<string, List<string>> nameUserIdDict = new Dictionary<string, List<string>>();
            List<string> tempNameList = new List<string>();
            for (int i = 0; i < names.Count; i++)
            {
                tempNameList.Add(names[i]);
                if ((i + 1) % batchCount == 0 || (i + 1) == names.Count)
                {
                    var result= Txl_BatchSearchByWX(tempNameList, 0, 200, 1, agentid,corpid);
                    if (result.errcode==0)
                    {
                        var queryResultList = result.query_result_list;
                        if (queryResultList != null) 
                        {
                            foreach (var item in queryResultList)
                            {
                                if (!nameUserIdDict.ContainsKey(item.query_request.query_word)
                                    && item.query_result.user!= null)
                                {
                                    nameUserIdDict.Add(item.query_request.query_word, item.query_result.user.userid);
                                }

                            }
                        }
                    }
                    
                    tempNameList.Clear();
                }
            }
            return nameUserIdDict;

        }
        //通訊錄中查找
        public static BatchSearchResponse Txl_BatchSearchByWX(List<string> names, int offset, int limit, int type, string agentid,string corpid)
        {
            string token = common.provider_access_token;// your access token
            string url = "https://qyapi.weixin.qq.com/cgi-bin/service/contact/batchsearch?provider_access_token=" + token;
             
            BatchSearchRequest batchSearchRequest = new BatchSearchRequest();
            batchSearchRequest.agentid = agentid;
            batchSearchRequest.auth_corpid =corpid;

            var query_request_list = new List<Query_request_list_single>();
            foreach (var name in names)
            {
                Query_request_list_single query_Request_List_Single = new Query_request_list_single();
                query_Request_List_Single.full_match_field = 1;
                query_Request_List_Single.limit = limit;
                query_Request_List_Single.offset = offset;
                query_Request_List_Single.query_type = type;
                query_Request_List_Single.query_word = name;
                query_request_list.Add(query_Request_List_Single);
            }

            batchSearchRequest.query_request_list = query_request_list;
            string postStr = JsonConvert.SerializeObject(batchSearchRequest);
            string retStr = TXLShowUtil.HttpPostData(url, postStr);
            BatchSearchResponse res = JsonConvert.DeserializeObject<BatchSearchResponse>(retStr);
            return res;
        }

    }
    public class Query_request_list_single
    {
        /// <summary>
        /// 
        /// </summary>
        public string query_word { get; set; }
        /// <summary>
        /// 
        /// </summary> 
        public int query_type { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int offset { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int limit { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int full_match_field { get; set; }
    }

    public class Query_request
    {
        /// <summary>
        /// 
        /// </summary>
        public string query_word { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int query_type { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int offset { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int limit { get; set; }
    }

    public class User
    {
        /// <summary>
        /// 
        /// </summary>
        public List<string> userid { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<string> open_userid { get; set; }
    }

    public class Party
    {
        /// <summary>
        /// 
        /// </summary>
        public List<int> department_id { get; set; }
    }

    public class Query_result
    {
        /// <summary>
        /// 
        /// </summary>
        public User user { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Party party { get; set; }
    }

    public class Query_result_list_single
    {
        /// <summary>
        /// 
        /// </summary>
        public Query_request query_request { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public bool is_last { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Query_result query_result { get; set; }
    }
    //批量檢索查詢數據
    public class BatchSearchRequest
    {
        /// <summary>
        /// 
        /// </summary>
        public string auth_corpid { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public int agentid { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<Query_request_list_single> query_request_list { get; set; }
    }
    //批量檢索返回數據
    public class BatchSearchResponse
    {
        /// <summary>
        /// 
        /// </summary>
        public int errcode { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string errmsg { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public List<Query_result_list_single> query_result_list { get; set; }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM