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