fhx
2023-11-06 1821a09470849995a07825d1ae2ae37b30ca82e8
提交 | 用户 | age
5c5945 1 package com.hx.mp.util;
E 2
0c97aa 3 import com.hx.util.StringUtils;
F 4 import com.hz.util.http.HttpHzUtil;
5 import com.hz.util.http.dto.HttpHzResponse;
5c5945 6 import net.sf.json.JSONArray;
E 7 import net.sf.json.JSONException;
8 import net.sf.json.JSONObject;
9 import org.apache.commons.io.IOUtils;
10
11 import java.io.OutputStream;
12 import java.net.HttpURLConnection;
13 import java.net.URL;
0c97aa 14 import java.util.List;
5c5945 15
E 16 /**
17  * 企业微信客户工具
18  */
19 public class CorpMpClientUtil {
20
155ce4 21     /**
A 22      * 生成联系我按钮参数的链接(生成config_id)
5c5945 23      */
155ce4 24     public static final String CREATE_CONTACT_ID_URL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_contact_way?access_token=";
A 25     /**
26      * 获取企业客户详情链接
27      */
28     public static final String GET_CLIENT_DETAIL_URL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get";
29     /**
30      * 获取获取企业标签库链接
31      */
32     public static final String GET_CORP_TAG_LIST = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_corp_tag_list?access_token=";
33     /**
34      * 添加客户联系人标签
35      */
36     public static final String ADD_TAG = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_corp_tag?access_token=";
37     /**
38      * 编辑企业客户标签
39      */
40     public static final String EDIT_TAG = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/edit_corp_tag?access_token=";
41     /**
42      * 删除客户联系人标签
43      */
44     public static final String DEL_TAG = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_corp_tag?access_token=";
45     /**
46      * 客户关联企业微信的标签url
47      */
48     public static final String RELATION_TAG = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/mark_tag?access_token=";
5c5945 49
4a23ba 50     /**
A 51      * 获取员工客户列表url
52      */
53     public static final String EXTERNAL_CONTACT_LIST = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/list";
5c5945 54
155ce4 55     /**
6b479c 56      * 创建获客链接url
A 57      */
58     public static final String CUSTOMER_ACQUISITION_CREATE_LINK = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_acquisition/create_link?access_token=";
59
60     /**
61      * 获取获客客户列表
62      */
63     public static final String CUSTOMER_ACQUISITION_CUSTOMER_LIST = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/customer_acquisition/customer?access_token=";
0c97aa 64
F 65     /**
66      * 获取配置过客户群管理的客户群列表
67      */
68     public static final String GROUP_CHAT_LIST = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/list?access_token=";
69
70
71     /**
72      * 通过客户群ID,获取详情。包括群名、群成员列表、群成员入群时间、入群方式。(客户群是由具有客户群使用权限的成员创建的外部群)
73      */
74     public static final String GROUP_CHAT_DETAIL = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/get?access_token=";
a53802 75
A 76     /**
77      * 企业微信发送普通邮件
78      */
79     public static final String GROUP_SENT_EMAIL = "https://qyapi.weixin.qq.com/cgi-bin/exmail/app/compose_send?access_token=";
80
0c97aa 81
6b479c 82
A 83     /**
155ce4 84      * 生成企业成员联系我的id-单人
A 85      *
86      * @param accessToken 企业的accessToken
87      * @param userId      企业成员的userId
88      * @param state       企业自定义的state参数,用于区分不同的添加渠道,在调用“获取外部联系人详情”时会返回该参数值,不超过30个字符
89      * @param remark      联系方式的备注信息,用于助记,不超过30个字符
90      * @return 返回
91      */
92     public static JSONObject createContactId(String accessToken, String userId, String state, String remark) {
93
94         String configId = null;
95         JSONObject data = new JSONObject();
96         data.put("type", 1);
97         data.put("scene", 1);
98         data.put("state", state);
99         data.put("remark", remark);
100         JSONArray userIds = new JSONArray();
101         userIds.add(userId);
102         data.put("user", userIds);
103
104         //请求,返回格式
5c5945 105        /*{
E 106            "errcode": 0,
107                "errmsg": "ok",
108                "config_id":"42b34949e138eb6e027c123cba77fAAA"  
109        }*/
110
155ce4 111         return HttpURLUtil(CREATE_CONTACT_ID_URL + accessToken, data.toString());
A 112     }
5c5945 113
155ce4 114     /**
4a23ba 115      * 获取客户列表
A 116      * @param accessToken 企业的accessToken
117      * @param userId 企业成员的userId
118      * @return 返回
119      */
120     public static JSONObject getExternalContactList(String accessToken, String userId) {
121         return HttpURLUtil(EXTERNAL_CONTACT_LIST + "?access_token=" + accessToken + "&userid=" + userId, null);
122     }
123
124     /**
155ce4 125      * 获取企业客户详情信息
A 126      *
127      * @param accessToken    企业的accessToken
5c5945 128      * @param externalUserId 外部人员的userId
155ce4 129      * @return 返回
5c5945 130      */
155ce4 131     public static JSONObject getClientData(String accessToken, String externalUserId) {
A 132         return HttpURLUtil(GET_CLIENT_DETAIL_URL + "?access_token=" + accessToken + "&external_userid=" + externalUserId, null);
133     }
5c5945 134
155ce4 135     /**
A 136      * 企业可通过此接口获取企业客户标签详情
137      *
5c5945 138      * @param accessToken 企业的accessToken
155ce4 139      * @param tagId  标签id
A 140      * @param groupId 标签组id
141      * @return 返回
5c5945 142      */
155ce4 143     public static JSONObject getCorpTagList(String accessToken, JSONArray tagId, JSONArray groupId) {
A 144         JSONObject data = new JSONObject();
145         data.put("tag_id", tagId);
146         data.put("group_id", groupId);
147         return HttpURLUtil(GET_CORP_TAG_LIST + accessToken, data.toString());
148     }
5c5945 149
155ce4 150     /**
A 151      * 添加客户联系人标签,
152      * 如果要向指定的标签组下添加标签,需要填写group_id参数;如果要创建一个全新的标签组以及标签,
153      * 则需要通过group_name参数指定新标签组名称,如果填写的groupname已经存在,则会在此标签组下新建标签
154      *
155      * @param accessToken 企业的accessToken
156      * @param groupId     组id(组名称和组id必填一个)
157      * @param groupName   组名称 组名称和组id必填一个)
158      * @param groupOrder  组排序,不填默认企业微信生成规则
159      * @param tagArray    数组,格式:[{ "name": "TAG_NAME_1", "order": 1 }]
160      * @return 返回
161      */
162     public static JSONObject addTable(String accessToken, String groupId, String groupName, String groupOrder, JSONArray tagArray) {
163         JSONObject data = new JSONObject();
164         data.put("group_id", groupId);
165         data.put("group_name", groupName);
166         data.put("order", groupOrder);
167         data.put("tag", tagArray);
168         return HttpURLUtil(ADD_TAG + accessToken, data.toString());
169     }
170
171     /**
172      * 编辑企业客户标签,
173      * 注意:修改后的标签组不能和已有的标签组重名,标签也不能和同一标签组下的其他标签重名。
174      *
175      * @param accessToken 企业的accessToken
176      * @param id     标签或标签组的id
177      * @param name   新的标签或标签组名称,最长为30个字符
178      * @param order  标签/标签组的次序值。order值大的排序靠前。有效的值范围是[0, 2^32)
179      * @return 返回
180      */
181     public static JSONObject editTable(String accessToken, String id, String name, String order) {
182         JSONObject data = new JSONObject();
183         data.put("id", id);
184         data.put("name", name);
185         data.put("order", order);
186         return HttpURLUtil(EDIT_TAG + accessToken, data.toString());
187     }
188
189     /**
190      * 删除客户联系人标签
5c5945 191      * groupArr和tagArr不可同时为空。
E 192      * 如果一个标签组下所有的标签均被删除,则标签组会被自动删除。
155ce4 193      *
5c5945 194      * @param accessToken 企业的accessToken
155ce4 195      * @param groupArr    组id数组
A 196      * @param tagArr      标签id数组
197      * @return 返回
5c5945 198      */
155ce4 199     public static JSONObject delTable(String accessToken, JSONArray groupArr, JSONArray tagArr) {
A 200         JSONObject data = new JSONObject();
201         data.put("tag_id", tagArr);
202         data.put("group_id", groupArr);
203         return HttpURLUtil(DEL_TAG + accessToken, data.toString());
204     }
5c5945 205
E 206     /**
207      * 客户关联企业微信的标签
208      * 注意:请确保external_userid是userid的外部联系人。
209      * add_tag和remove_tag不可同时为空。
155ce4 210      *
A 211      * @param accessToken    企业的accessToken
212      * @param userId         企业成员的userid
5c5945 213      * @param externalUserId 外部联系人的id
155ce4 214      * @param addTag         新增的标签id(企业标签的id)数组
A 215      * @param removeTag      删除的标签id(企业标签的id)数组
216      * @return 返回
5c5945 217      */
155ce4 218     public static JSONObject relationTag(String accessToken, String userId, String externalUserId, JSONArray addTag, JSONArray removeTag) {
5c5945 219         JSONObject data = new JSONObject();
155ce4 220         data.put("userid", userId);
A 221         data.put("external_userid", externalUserId);
222         data.put("add_tag", addTag);
223         data.put("remove_tag", removeTag);
224         return HttpURLUtil(RELATION_TAG + accessToken, data.toString());
5c5945 225     }
E 226
155ce4 227     /**
A 228      * 请求http协议 获取信息工具
229      **/
5c5945 230     public static JSONObject HttpURLUtil(String url, String data) {
E 231         HttpURLConnection con = null;
232         URL u = null;
233         String wxMsgXml = null;
234         JSONObject obj = null;
235         try {
236             u = new URL(url);
237             con = (HttpURLConnection) u.openConnection();
238             con.setRequestMethod("POST");
239             con.setDoOutput(true);
240             con.setDoInput(true);
241             con.setUseCaches(false);
242             con.setReadTimeout(5000);
243             con.setRequestProperty("Charset", "UTF-8");
244             con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
245
246             if (data != null) {
247                 OutputStream os = con.getOutputStream();
248                 os.write(data.getBytes("utf-8"));
249             }
250
251             if (con.getResponseCode() != 200)
252                 throw new RuntimeException("请求url失败");
253             // 读取返回内容
254             wxMsgXml = IOUtils.toString(con.getInputStream(), "utf-8");
255             obj = JSONObject.fromObject(wxMsgXml);
256             // //System.out.println("HttpURLUtil:"+wxMsgXml);
257         } catch (Exception e) {
258             e.printStackTrace();
259             obj = new JSONObject();
260             try {
261                 obj.put("status", 1);
262                 obj.put("errMsg", e.getMessage());
263             } catch (JSONException e1) {
264                 e1.printStackTrace();
265             }
266         } finally {
267             if (con != null) {
268                 con.disconnect();
269             }
270         }
271         return obj;
272     }
3f9abe 273
W 274     /**生成企业成员联系我的id-单人
275      * @param accessToken 企业的accessToken
276      * @param userId 企业成员的userId
277      * @param scene 场景,1-在小程序中联系,2-通过二维码联系
278      * @param state 企业自定义的state参数,用于区分不同的添加渠道,在调用“获取外部联系人详情”时会返回该参数值,不超过30个字符
279      * @param remark 联系方式的备注信息,用于助记,不超过30个字符
4a23ba 280      * @return 返回
3f9abe 281      */
W 282     public static JSONObject createContactId(String accessToken,String userId,int scene,String state,String remark){
283         String configId = null;
284         JSONObject data = new JSONObject();
285         data.put("type",1);
286         data.put("scene",scene);
287         data.put("state",state);
288         data.put("remark",remark);
289         JSONArray userIds = new JSONArray();
290         userIds.add(userId);
291         data.put("user",userIds);
292
293         //请求,返回格式
294        /*{
295            "errcode": 0,
296                "errmsg": "ok",
297                "config_id":"42b34949e138eb6e027c123cba77fAAA"  
298        }*/
299
300         return HttpURLUtil(CREATE_CONTACT_ID_URL+accessToken,data.toString());
301     }
6b479c 302
A 303     /**
304      * 创建获客链接
305      *
306      * @param accessToken 企业的accessToken
307      * @param linkName  链接名称
308      * @param userList 员工用户userId
309      * @return 返回
310      */
311     public static JSONObject createCustomerAcquisitionLink(String accessToken, String linkName, JSONArray userList) {
312         JSONObject data = new JSONObject();
313         data.put("link_name", linkName);
314         JSONObject subData = new JSONObject();
315         subData.accumulate("user_list", userList);
316         data.put("range", subData);
317         /** 返回数据
318          * {
319          *    "link_id":"LINK_ID",
320          *    "link_name":"获客链接1号",
321          *    "range":
322          *    {
323          *            "user_list":["zhangsan","lisi"],
324          *         "department_list":[2,3]
325          *    },
326          *    "skip_verify":true
327          * }
328          */
329         return HttpURLUtil(CUSTOMER_ACQUISITION_CREATE_LINK + accessToken, data.toString());
330     }
331
332     /**
333      * 获取获客客户列表
334      *
335      * @param accessToken 企业的accessToken
336      * @param linkId  链接id
ff57ac 337      * @param cursor  分页游标
6b479c 338      * @return 返回
A 339      */
ff57ac 340     public static JSONObject getCustomerAcquisitionCustomerList(String accessToken, String linkId, String cursor) {
6b479c 341         JSONObject data = new JSONObject();
A 342         data.put("link_id", linkId);
ff57ac 343         data.put("limit", 1000);
A 344         // 分页游标
345         if (!StringUtils.isEmpty(cursor)) {
346             data.put("cursor", cursor);
347         }
6b479c 348         /** 返回数据
A 349          * {
350          *     "errcode": 0,
351          *     "errmsg": "ok",
352          *     "customer_list":
353          *     [
354          *                {
355          *             "external_userid":"woAJ2GCAAAXtWyujaWJHDDGi0mACAAA",
356          *             "userid":"zhangsan",
357          *             "chat_status":0,
358          *             "state":"CHANNEL_A"
359          *        }
360          *     ],
361          *     "next_cursor":"CURSOR"
362          * }
363          */
364         return HttpURLUtil(CUSTOMER_ACQUISITION_CUSTOMER_LIST + accessToken, data.toString());
365     }
0c97aa 366
F 367
368     /**
369      * 该接口用于获取配置过客户群管理的客户群列表。
370      *
371      * @param accessToken  企业的accessToken
372      * @param statusFilter 客户群跟进状态过滤。0 - 所有列表(即不过滤) 1 - 离职待继承 2 - 离职继承中 3 - 离职继承完成
373      * @param userIdList   群主过滤 如果不填,表示获取应用可见范围内全部群主的数据可见范围人数超过1000人,为了防止数据包过大,会报错 81017 用户ID列表。最多100个
374      * @param cursor       分页下标
375      * @param limit        列表
376      * @return 返回
377      */
378     public static HttpHzResponse getGroupChatList(String accessToken, Integer statusFilter, List<String> userIdList, String cursor, Integer limit) {
379         JSONObject bodyData = new JSONObject();
380         bodyData.put("status_filter", statusFilter);
381         //分页下标
382         if (StringUtils.noNull(cursor)) {
383             bodyData.put("cursor", cursor);
384         }
385         //默认100条
386         if (limit == null) {
387             limit = 100;
388         }
389         bodyData.put("limit", limit);
390         //过滤
391         if (userIdList != null && userIdList.size() > 0) {
392             JSONObject filter = new JSONObject();
393             filter.put("userid_list", userIdList);
394             bodyData.put("owner_filter", filter);
395         }
396         return HttpHzUtil.HttpURLUtilJson(GROUP_CHAT_LIST + accessToken, bodyData.toString(), null, null, "GET", null);
397     }
398
399     /**
400      * 通过客户群ID,获取详情。包括群名、群成员列表、群成员入群时间、入群方式。(客户群是由具有客户群使用权限的成员创建的外部群)
401      *
402      * @param chatId   客户群ID
403      * @param needName 是否需要返回群成员的名字0-不返回;1-返回
404      */
405     public static HttpHzResponse getGroupChatDetail(String accessToken, String chatId, Integer needName) {
406         JSONObject bodyData = new JSONObject();
407         bodyData.put("chat_id", chatId);
408         if (needName == null) {
409             needName = 0;
410         }
411         bodyData.put("need_name", needName);
412         return HttpHzUtil.HttpURLUtilJson(GROUP_CHAT_DETAIL + accessToken, bodyData.toString(), null, null, "GET", null);
413     }
414
a53802 415     /**
A 416      * 企业微信发送普通邮件
417      * 接口地址: https://developer.work.weixin.qq.com/document/path/97445
418      * @param bodyData  参数
419      */
420     public static JSONObject sentEmail(String accessToken, JSONObject bodyData) {
421         return HttpURLUtil(GROUP_SENT_EMAIL + accessToken, bodyData.toString());
422     }
5c5945 423 }