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