ChenJiaHe
2020-12-03 ccd95bd3a438e6f76a0ecf87338a626a12061c1f
提交 | 用户 | 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
106      * @param userId 用户id
107      * @return 返回
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());
131         System.out.println("userId:"+userId);
132         System.out.println("obj:"+obj.toString());
133         if(obj != null) {
134             return obj.optInt("errcode", -1);
135         }
136         return -1;
137     }
138
139
140     /**
141      * 添加部门
142      * @param name 部门名称
143      * @return id
144      */
145     public static int addDepartment(String at, String name, int parentId) {
146         JSONObject obj = new JSONObject();
147         obj.put("name", name);
148         obj.put("parentid", parentId);
149
150         obj = HttpURLUtil(MessageFormat.format(URL_ADD_DEPARTMENT, at), obj.toString());
151
152         if(obj != null && obj.optInt("errcode", -1) == 0) {
153             return obj.optInt("id", -1);
154         }
155         return -1;
156     }
157
158     /**
159      * 更新部门
160      * @param name 部门名称
161      * @param deaprtId 部门id
162      * @param parentid 父类部门id,可以为空
163      * @return  状态,0成功
164      */
165     public static int updateDepartment(String at, String name, int deaprtId,int parentid) {
166         JSONObject obj = new JSONObject();
167         obj.put("name", name);
168         obj.put("id", deaprtId);
169         obj.put("parentid", parentid);
170
171         obj = HttpURLUtil(MessageFormat.format(URL_UPDATE_DEPARTMENT, at), obj.toString());
172         return obj.optInt("errcode", -1);
173     }
174
175     /**删除部门
176      * 删除的部门不能是根部门且没有成员
177      * @param deaprtId 部门id
178      * @return 状态,0成功
179      */
180     public static int deleteDepartment(String at,int deaprtId) {
181         JSONObject obj = new JSONObject();
182         obj.put("id", deaprtId);
183
184         obj = HttpURLUtil(MessageFormat.format(URL_DELETE_DEPARTMENT,at,deaprtId), obj.toString());
185         System.out.println("删除部门:"+obj.toString());
186         return obj.optInt("errcode", -1);
187     }
188
189     /**发送企业应用消息
190      * @param accessToken 企业accessToken
191      * @param data 发送数据结构
192      * @return
193      */
194     public static JSONObject messageSend(String accessToken,String data){
195         return HttpURLUtil(URL_MESSAGE_SEND+accessToken,data);
196     }
197
198     /**添加用户标签,标签名称不能重复
199      * @param at 访问at
200      * @param label 标签名称
201      * @return 返回 -1失败,0成功,1已存在
202      */
203     public static int addLabel(String at, String label) {
204         String url = MessageFormat.format(URL_ADD_LABEL, at);
205         JSONObject obj = new JSONObject();
206         obj.put("tagname", label);
207         obj = HttpURLUtil(url, obj.toString());
208         System.out.println("obj....:"+obj.toString());
209         if(obj != null ) {
210             int errcode = obj.optInt("errcode", -1);
211             if( errcode== 0){
212                 //上传成功
213                 return obj.optInt("tagid", -1);
214             }if(errcode == 40071){
215                 //标签已经存在
216                 return 1;
217             }
218         }
219         return -1;
220     }
221
222
223     /**
224      * 获取应用accessToken
225      * @param corpId 企业id
226      * @param appSecret 应用密钥
227      * @return
228      * @throws Exception
229      */
230     public static JSONObject getApplicationAccessToken(String corpId, String appSecret) {
231         String url = MessageFormat.format(URL_GET_ACCESS_TOKEN, corpId, appSecret);
232         JSONObject obj = HttpURLUtil(url, null);
233         return obj;
234     }
235
236     /**
237      * 使用临时登录凭证code获取 session_key、用户userId以及用户所在企业的corpId等信息
238      * @param accessToken 访问token
239      * @param code 临时code
240      * @return
241      */
242     public static JSONObject code2Session(String accessToken, String code) {
243         String url = MessageFormat.format(URL_CODE_2_SESSION, accessToken, code);
244         JSONObject obj = HttpURLUtil(url, null);
245         return obj;
246     }
247
248
249
250     /** 请求http协议 获取信息工具 **/
251     public static JSONObject HttpURLUtil(String url, String data) {
252         HttpURLConnection con = null;
253         URL u = null;
254         String wxMsgXml = null;
255         JSONObject obj = null;
256         try {
257             u = new URL(url);
258             con = (HttpURLConnection) u.openConnection();
259             con.setRequestMethod("POST");
260             con.setDoOutput(true);
261             con.setDoInput(true);
262             con.setUseCaches(false);
263             con.setReadTimeout(5000);
264             con.setRequestProperty("Charset", "UTF-8");
265             con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
266
267             if (data != null) {
268                 OutputStream os = con.getOutputStream();
269                 os.write(data.getBytes("utf-8"));
270             }
271
272             if (con.getResponseCode() != 200)
273                 throw new RuntimeException("请求url失败");
274             // 读取返回内容
275             wxMsgXml = IOUtils.toString(con.getInputStream(), "utf-8");
276             obj = JSONObject.fromObject(wxMsgXml);
277             // //System.out.println("HttpURLUtil:"+wxMsgXml);
278         } catch (Exception e) {
279             e.printStackTrace();
280             obj = new JSONObject();
281             try {
282                 obj.put("status", 1);
283                 obj.put("errMsg", e.getMessage());
284             } catch (JSONException e1) {
285                 e1.printStackTrace();
286             }
287         } finally {
288             if (con != null) {
289                 con.disconnect();
290             }
291         }
292         return obj;
293     }
294 }