E1ED922C1E9526DD63272D7EC5C6CB77
2021-02-07 2ad84337e1ae42b56a7b8e463bca9ac06e457826
提交 | 用户 | age
5c5945 1 package com.hx.mp.util;
E 2
3 import com.hx.exception.TipsException;
4 import com.hx.util.StringUtils;
5 import net.sf.json.JSONException;
6 import net.sf.json.JSONObject;
7 import org.apache.commons.io.IOUtils;
8 import org.apache.poi.util.SystemOutLogger;
9
10 import java.io.OutputStream;
11 import java.net.HttpURLConnection;
12 import java.net.URL;
13 import java.text.MessageFormat;
14
15 /**
16  * 企业微信工具类
17  */
18 public class CorpMpUtil {
19
20     /**链接-获取应用accessToken*/
21     public static final String URL_GET_ACCESS_TOKEN = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}";
22     /**链接-获取session信息*/
23     public static final String URL_CODE_2_SESSION = "https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session?access_token={0}&js_code={1}&grant_type=authorization_code";
24     /**链接-添加标签*/
25     public static final String URL_ADD_LABEL = "https://qyapi.weixin.qq.com/cgi-bin/tag/create?access_token={0}";
26     /**链接-添加部门*/
27     public static final String URL_ADD_DEPARTMENT = "https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={0}";
28     /**链接-修改部门*/
29     public static final String URL_UPDATE_DEPARTMENT = "https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token={0}";
30     /**链接-删除部门*/
31     public static final String URL_DELETE_DEPARTMENT = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token={0}&id={1}";
32     /**链接-添加成员*/
33     public static final String URL_ADD_USER = "https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token={0}";
34     /**链接-更新成员*/
35     public static final String URL_UPDATE_USER = "https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token={0}";
36     /**链接-删除成员*/
37     public static final String URL_DELETE_USER = "https://qyapi.weixin.qq.com/cgi-bin/user/delete?access_token={0}&userid={1}";
38     /**链接-成员详情*/
39     public static final String URL_INFO_USER = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token={0}&userid={1}";
40     /**发送消息*/
41     public static final String URL_MESSAGE_SEND = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";
42
43     /**
44      * 添加工作人员
45      * @param at 访问at
46      * @param userId 用户id
47      * @param name 名称
48      * @param depId 部门id
49      * @param position 职位
50      * @param tel 电话
51      * @param email 邮箱
52      * @return 返回
53      */
54     public static int addUser(String at, String userId, String name, int depId
55             , String position,String tel,String email) {
56
57         if(StringUtils.isEmpty(tel)&&StringUtils.isEmpty(email)){
58             throw new TipsException("添加企业微信手机号和邮箱不能都为空!");
59         }
60
61         JSONObject obj = new JSONObject();
62         obj.put("userid", userId);
63         obj.put("name", name);
64         obj.put("department", depId);
65         obj.put("position", position);
66         obj.put("mobile", tel);
67         obj.put("email", email);
68         obj = HttpURLUtil(MessageFormat.format(URL_ADD_USER, at), obj.toString());
69         if(obj != null) {
70             return obj.optInt("errcode", -1);
71         }
72         return -1;
73     }
74     /**
75      * 更新工作人员
76      * @param at 访问at
77      * @param userId 用户id
78      * @param name 名称
79      * @param depId 部门id
80      * @param position 职位
81      * @param tel 电话
82      * @param email 邮箱
83      * @return 返回
84      */
85     public static int updateUser(String at, String userId, String name, int depId
86             , String position,String tel,String email) {
87         JSONObject obj = new JSONObject();
88         obj.put("userid", userId);
89         obj.put("name", name);
90         obj.put("department", depId);
91         obj.put("position", position);
92         obj.put("mobile", tel);
93         obj.put("email", email);
94
95         obj = HttpURLUtil(MessageFormat.format(URL_UPDATE_USER, at), obj.toString());
96         if(obj != null && obj.optInt("errcode", -1) == 0) {
97             return obj.optInt("errcode", -1);
98         }
99
100         return -1;
101     }
102
103     /**
104      * 删除工作人员
105      * @param at 访问at
6e7bbe 106      * @param userId 企业用户id
5c5945 107      * @return 返回
E 108      */
109     public static int deleteUser(String at, String userId) {
110         JSONObject obj = new JSONObject();
111         obj.put("userid", userId);
112
113         obj = HttpURLUtil(MessageFormat.format(URL_DELETE_USER, at,userId), obj.toString());
114         if(obj != null) {
115             return obj.optInt("errcode", -1);
116         }
117         return -1;
118     }
119
120     /**
121      * 工作人员判断是否存在企业微信
122      * @param at 访问at
123      * @param userId 用户id
124      * @return 返回
125      */
126     public static int infoUser(String at, String userId) {
127         JSONObject obj = new JSONObject();
128         obj.put("userid", userId);
129
130         obj = HttpURLUtil(MessageFormat.format(URL_INFO_USER, at,userId), obj.toString());
6e7bbe 131
5c5945 132         if(obj != null) {
E 133             return obj.optInt("errcode", -1);
134         }
135         return -1;
136     }
137
6e7bbe 138     /**
C 139      * 获取企业用户信息
140      * @param at 访问at
141      * @param userId 用户id
142      * @return 返回
143      */
144     public static JSONObject userData(String at, String userId) {
145         JSONObject obj = new JSONObject();
146         obj.put("userid", userId);
147
148         return obj = HttpURLUtil(MessageFormat.format(URL_INFO_USER, at,userId), obj.toString());
149     }
150
5c5945 151
E 152     /**
153      * 添加部门
154      * @param name 部门名称
155      * @return id
156      */
157     public static int addDepartment(String at, String name, int parentId) {
158         JSONObject obj = new JSONObject();
159         obj.put("name", name);
160         obj.put("parentid", parentId);
161
162         obj = HttpURLUtil(MessageFormat.format(URL_ADD_DEPARTMENT, at), obj.toString());
163
164         if(obj != null && obj.optInt("errcode", -1) == 0) {
165             return obj.optInt("id", -1);
166         }
167         return -1;
168     }
169
170     /**
171      * 更新部门
172      * @param name 部门名称
173      * @param deaprtId 部门id
174      * @param parentid 父类部门id,可以为空
175      * @return  状态,0成功
176      */
177     public static int updateDepartment(String at, String name, int deaprtId,int parentid) {
178         JSONObject obj = new JSONObject();
179         obj.put("name", name);
180         obj.put("id", deaprtId);
181         obj.put("parentid", parentid);
182
183         obj = HttpURLUtil(MessageFormat.format(URL_UPDATE_DEPARTMENT, at), obj.toString());
184         return obj.optInt("errcode", -1);
185     }
186
187     /**删除部门
188      * 删除的部门不能是根部门且没有成员
189      * @param deaprtId 部门id
190      * @return 状态,0成功
191      */
192     public static int deleteDepartment(String at,int deaprtId) {
193         JSONObject obj = new JSONObject();
194         obj.put("id", deaprtId);
195
196         obj = HttpURLUtil(MessageFormat.format(URL_DELETE_DEPARTMENT,at,deaprtId), obj.toString());
197         System.out.println("删除部门:"+obj.toString());
198         return obj.optInt("errcode", -1);
199     }
200
201     /**发送企业应用消息
202      * @param accessToken 企业accessToken
203      * @param data 发送数据结构
204      * @return
205      */
206     public static JSONObject messageSend(String accessToken,String data){
207         return HttpURLUtil(URL_MESSAGE_SEND+accessToken,data);
208     }
209
210     /**添加用户标签,标签名称不能重复
211      * @param at 访问at
212      * @param label 标签名称
213      * @return 返回 -1失败,0成功,1已存在
214      */
215     public static int addLabel(String at, String label) {
216         String url = MessageFormat.format(URL_ADD_LABEL, at);
217         JSONObject obj = new JSONObject();
218         obj.put("tagname", label);
219         obj = HttpURLUtil(url, obj.toString());
220         System.out.println("obj....:"+obj.toString());
221         if(obj != null ) {
222             int errcode = obj.optInt("errcode", -1);
223             if( errcode== 0){
224                 //上传成功
225                 return obj.optInt("tagid", -1);
226             }if(errcode == 40071){
227                 //标签已经存在
228                 return 1;
229             }
230         }
231         return -1;
232     }
233
234
235     /**
236      * 获取应用accessToken
237      * @param corpId 企业id
238      * @param appSecret 应用密钥
239      * @return
240      * @throws Exception
241      */
242     public static JSONObject getApplicationAccessToken(String corpId, String appSecret) {
243         String url = MessageFormat.format(URL_GET_ACCESS_TOKEN, corpId, appSecret);
244         JSONObject obj = HttpURLUtil(url, null);
245         return obj;
246     }
247
248     /**
249      * 使用临时登录凭证code获取 session_key、用户userId以及用户所在企业的corpId等信息
250      * @param accessToken 访问token
251      * @param code 临时code
252      * @return
253      */
254     public static JSONObject code2Session(String accessToken, String code) {
255         String url = MessageFormat.format(URL_CODE_2_SESSION, accessToken, code);
256         JSONObject obj = HttpURLUtil(url, null);
257         return obj;
258     }
259
260
261
262     /** 请求http协议 获取信息工具 **/
263     public static JSONObject HttpURLUtil(String url, String data) {
264         HttpURLConnection con = null;
265         URL u = null;
266         String wxMsgXml = null;
267         JSONObject obj = null;
268         try {
269             u = new URL(url);
270             con = (HttpURLConnection) u.openConnection();
271             con.setRequestMethod("POST");
272             con.setDoOutput(true);
273             con.setDoInput(true);
274             con.setUseCaches(false);
275             con.setReadTimeout(5000);
276             con.setRequestProperty("Charset", "UTF-8");
277             con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
278
279             if (data != null) {
280                 OutputStream os = con.getOutputStream();
281                 os.write(data.getBytes("utf-8"));
282             }
283
284             if (con.getResponseCode() != 200)
285                 throw new RuntimeException("请求url失败");
286             // 读取返回内容
287             wxMsgXml = IOUtils.toString(con.getInputStream(), "utf-8");
288             obj = JSONObject.fromObject(wxMsgXml);
289             // //System.out.println("HttpURLUtil:"+wxMsgXml);
290         } catch (Exception e) {
291             e.printStackTrace();
292             obj = new JSONObject();
293             try {
294                 obj.put("status", 1);
295                 obj.put("errMsg", e.getMessage());
296             } catch (JSONException e1) {
297                 e1.printStackTrace();
298             }
299         } finally {
300             if (con != null) {
301                 con.disconnect();
302             }
303         }
304         return obj;
305     }
306 }