提交 | 用户 | 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 |
} |
0511a5
|
74 |
|
C |
75 |
/** |
|
76 |
* 添加工作人员 |
|
77 |
* @param at 访问at |
|
78 |
* @param userId 用户id |
|
79 |
* @param name 名称 |
|
80 |
* @param depId 部门id |
|
81 |
* @param position 职位 |
|
82 |
* @param tel 电话 |
|
83 |
* @param email 邮箱 |
|
84 |
* @return 返回 |
|
85 |
*/ |
|
86 |
public static JSONObject addUserObj(String at, String userId, String name, int depId |
|
87 |
, String position,String tel,String email) { |
|
88 |
|
|
89 |
if(StringUtils.isEmpty(tel)&&StringUtils.isEmpty(email)){ |
|
90 |
throw new TipsException("添加企业微信手机号和邮箱不能都为空!"); |
|
91 |
} |
|
92 |
|
|
93 |
JSONObject obj = new JSONObject(); |
|
94 |
obj.put("userid", userId); |
|
95 |
obj.put("name", name); |
|
96 |
obj.put("department", depId); |
|
97 |
obj.put("position", position); |
|
98 |
obj.put("mobile", tel); |
|
99 |
obj.put("email", email); |
|
100 |
obj = HttpURLUtil(MessageFormat.format(URL_ADD_USER, at), obj.toString()); |
|
101 |
|
|
102 |
if(obj == null){ |
|
103 |
obj = new JSONObject(); |
|
104 |
} |
|
105 |
|
|
106 |
return obj; |
|
107 |
|
|
108 |
} |
|
109 |
|
5c5945
|
110 |
/** |
E |
111 |
* 更新工作人员 |
|
112 |
* @param at 访问at |
|
113 |
* @param userId 用户id |
|
114 |
* @param name 名称 |
|
115 |
* @param depId 部门id |
|
116 |
* @param position 职位 |
|
117 |
* @param tel 电话 |
|
118 |
* @param email 邮箱 |
|
119 |
* @return 返回 |
|
120 |
*/ |
|
121 |
public static int updateUser(String at, String userId, String name, int depId |
|
122 |
, String position,String tel,String email) { |
|
123 |
JSONObject obj = new JSONObject(); |
|
124 |
obj.put("userid", userId); |
|
125 |
obj.put("name", name); |
|
126 |
obj.put("department", depId); |
|
127 |
obj.put("position", position); |
|
128 |
obj.put("mobile", tel); |
|
129 |
obj.put("email", email); |
|
130 |
|
|
131 |
obj = HttpURLUtil(MessageFormat.format(URL_UPDATE_USER, at), obj.toString()); |
|
132 |
if(obj != null && obj.optInt("errcode", -1) == 0) { |
|
133 |
return obj.optInt("errcode", -1); |
|
134 |
} |
|
135 |
|
|
136 |
return -1; |
|
137 |
} |
|
138 |
|
|
139 |
/** |
|
140 |
* 删除工作人员 |
|
141 |
* @param at 访问at |
6e7bbe
|
142 |
* @param userId 企业用户id |
5c5945
|
143 |
* @return 返回 |
E |
144 |
*/ |
|
145 |
public static int deleteUser(String at, String userId) { |
|
146 |
JSONObject obj = new JSONObject(); |
|
147 |
obj.put("userid", userId); |
|
148 |
|
|
149 |
obj = HttpURLUtil(MessageFormat.format(URL_DELETE_USER, at,userId), obj.toString()); |
|
150 |
if(obj != null) { |
|
151 |
return obj.optInt("errcode", -1); |
|
152 |
} |
|
153 |
return -1; |
|
154 |
} |
|
155 |
|
|
156 |
/** |
|
157 |
* 工作人员判断是否存在企业微信 |
|
158 |
* @param at 访问at |
|
159 |
* @param userId 用户id |
|
160 |
* @return 返回 |
|
161 |
*/ |
|
162 |
public static int infoUser(String at, String userId) { |
|
163 |
JSONObject obj = new JSONObject(); |
|
164 |
obj.put("userid", userId); |
|
165 |
|
|
166 |
obj = HttpURLUtil(MessageFormat.format(URL_INFO_USER, at,userId), obj.toString()); |
6e7bbe
|
167 |
|
5c5945
|
168 |
if(obj != null) { |
E |
169 |
return obj.optInt("errcode", -1); |
|
170 |
} |
|
171 |
return -1; |
|
172 |
} |
|
173 |
|
6e7bbe
|
174 |
/** |
C |
175 |
* 获取企业用户信息 |
|
176 |
* @param at 访问at |
|
177 |
* @param userId 用户id |
|
178 |
* @return 返回 |
|
179 |
*/ |
|
180 |
public static JSONObject userData(String at, String userId) { |
|
181 |
JSONObject obj = new JSONObject(); |
|
182 |
obj.put("userid", userId); |
|
183 |
|
|
184 |
return obj = HttpURLUtil(MessageFormat.format(URL_INFO_USER, at,userId), obj.toString()); |
|
185 |
} |
|
186 |
|
5c5945
|
187 |
|
E |
188 |
/** |
|
189 |
* 添加部门 |
|
190 |
* @param name 部门名称 |
|
191 |
* @return id |
|
192 |
*/ |
|
193 |
public static int addDepartment(String at, String name, int parentId) { |
|
194 |
JSONObject obj = new JSONObject(); |
|
195 |
obj.put("name", name); |
|
196 |
obj.put("parentid", parentId); |
|
197 |
|
|
198 |
obj = HttpURLUtil(MessageFormat.format(URL_ADD_DEPARTMENT, at), obj.toString()); |
|
199 |
|
|
200 |
if(obj != null && obj.optInt("errcode", -1) == 0) { |
|
201 |
return obj.optInt("id", -1); |
|
202 |
} |
|
203 |
return -1; |
|
204 |
} |
|
205 |
|
|
206 |
/** |
|
207 |
* 更新部门 |
|
208 |
* @param name 部门名称 |
|
209 |
* @param deaprtId 部门id |
|
210 |
* @param parentid 父类部门id,可以为空 |
|
211 |
* @return 状态,0成功 |
|
212 |
*/ |
|
213 |
public static int updateDepartment(String at, String name, int deaprtId,int parentid) { |
|
214 |
JSONObject obj = new JSONObject(); |
|
215 |
obj.put("name", name); |
|
216 |
obj.put("id", deaprtId); |
|
217 |
obj.put("parentid", parentid); |
|
218 |
|
|
219 |
obj = HttpURLUtil(MessageFormat.format(URL_UPDATE_DEPARTMENT, at), obj.toString()); |
|
220 |
return obj.optInt("errcode", -1); |
|
221 |
} |
|
222 |
|
|
223 |
/**删除部门 |
|
224 |
* 删除的部门不能是根部门且没有成员 |
|
225 |
* @param deaprtId 部门id |
|
226 |
* @return 状态,0成功 |
|
227 |
*/ |
|
228 |
public static int deleteDepartment(String at,int deaprtId) { |
|
229 |
JSONObject obj = new JSONObject(); |
|
230 |
obj.put("id", deaprtId); |
|
231 |
|
|
232 |
obj = HttpURLUtil(MessageFormat.format(URL_DELETE_DEPARTMENT,at,deaprtId), obj.toString()); |
|
233 |
System.out.println("删除部门:"+obj.toString()); |
|
234 |
return obj.optInt("errcode", -1); |
|
235 |
} |
|
236 |
|
|
237 |
/**发送企业应用消息 |
|
238 |
* @param accessToken 企业accessToken |
|
239 |
* @param data 发送数据结构 |
|
240 |
* @return |
|
241 |
*/ |
|
242 |
public static JSONObject messageSend(String accessToken,String data){ |
|
243 |
return HttpURLUtil(URL_MESSAGE_SEND+accessToken,data); |
|
244 |
} |
|
245 |
|
|
246 |
/**添加用户标签,标签名称不能重复 |
|
247 |
* @param at 访问at |
|
248 |
* @param label 标签名称 |
|
249 |
* @return 返回 -1失败,0成功,1已存在 |
|
250 |
*/ |
|
251 |
public static int addLabel(String at, String label) { |
|
252 |
String url = MessageFormat.format(URL_ADD_LABEL, at); |
|
253 |
JSONObject obj = new JSONObject(); |
|
254 |
obj.put("tagname", label); |
|
255 |
obj = HttpURLUtil(url, obj.toString()); |
|
256 |
System.out.println("obj....:"+obj.toString()); |
|
257 |
if(obj != null ) { |
|
258 |
int errcode = obj.optInt("errcode", -1); |
|
259 |
if( errcode== 0){ |
|
260 |
//上传成功 |
|
261 |
return obj.optInt("tagid", -1); |
|
262 |
}if(errcode == 40071){ |
|
263 |
//标签已经存在 |
|
264 |
return 1; |
|
265 |
} |
|
266 |
} |
|
267 |
return -1; |
|
268 |
} |
|
269 |
|
|
270 |
|
|
271 |
/** |
|
272 |
* 获取应用accessToken |
|
273 |
* @param corpId 企业id |
|
274 |
* @param appSecret 应用密钥 |
|
275 |
* @return |
|
276 |
* @throws Exception |
|
277 |
*/ |
|
278 |
public static JSONObject getApplicationAccessToken(String corpId, String appSecret) { |
|
279 |
String url = MessageFormat.format(URL_GET_ACCESS_TOKEN, corpId, appSecret); |
|
280 |
JSONObject obj = HttpURLUtil(url, null); |
|
281 |
return obj; |
|
282 |
} |
|
283 |
|
|
284 |
/** |
|
285 |
* 使用临时登录凭证code获取 session_key、用户userId以及用户所在企业的corpId等信息 |
|
286 |
* @param accessToken 访问token |
|
287 |
* @param code 临时code |
|
288 |
* @return |
|
289 |
*/ |
|
290 |
public static JSONObject code2Session(String accessToken, String code) { |
|
291 |
String url = MessageFormat.format(URL_CODE_2_SESSION, accessToken, code); |
|
292 |
JSONObject obj = HttpURLUtil(url, null); |
|
293 |
return obj; |
|
294 |
} |
|
295 |
|
|
296 |
|
|
297 |
|
|
298 |
/** 请求http协议 获取信息工具 **/ |
|
299 |
public static JSONObject HttpURLUtil(String url, String data) { |
|
300 |
HttpURLConnection con = null; |
|
301 |
URL u = null; |
|
302 |
String wxMsgXml = null; |
|
303 |
JSONObject obj = null; |
|
304 |
try { |
|
305 |
u = new URL(url); |
|
306 |
con = (HttpURLConnection) u.openConnection(); |
|
307 |
con.setRequestMethod("POST"); |
|
308 |
con.setDoOutput(true); |
|
309 |
con.setDoInput(true); |
|
310 |
con.setUseCaches(false); |
|
311 |
con.setReadTimeout(5000); |
|
312 |
con.setRequestProperty("Charset", "UTF-8"); |
|
313 |
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); |
|
314 |
|
|
315 |
if (data != null) { |
|
316 |
OutputStream os = con.getOutputStream(); |
|
317 |
os.write(data.getBytes("utf-8")); |
|
318 |
} |
|
319 |
|
|
320 |
if (con.getResponseCode() != 200) |
|
321 |
throw new RuntimeException("请求url失败"); |
|
322 |
// 读取返回内容 |
|
323 |
wxMsgXml = IOUtils.toString(con.getInputStream(), "utf-8"); |
|
324 |
obj = JSONObject.fromObject(wxMsgXml); |
|
325 |
// //System.out.println("HttpURLUtil:"+wxMsgXml); |
|
326 |
} catch (Exception e) { |
|
327 |
e.printStackTrace(); |
|
328 |
obj = new JSONObject(); |
|
329 |
try { |
|
330 |
obj.put("status", 1); |
|
331 |
obj.put("errMsg", e.getMessage()); |
|
332 |
} catch (JSONException e1) { |
|
333 |
e1.printStackTrace(); |
|
334 |
} |
|
335 |
} finally { |
|
336 |
if (con != null) { |
|
337 |
con.disconnect(); |
|
338 |
} |
|
339 |
} |
|
340 |
return obj; |
|
341 |
} |
|
342 |
} |