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