公司产品中有一个导入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; }
}