WeiXinUtil.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. package util;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.io.OutputStream;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9. import java.util.Map;
  10. import net.sf.json.JSONObject;
  11. public class WeiXinUtil {
  12. public static String whzysb = "1";
  13. public static String fwagentid = "6";
  14. public static String swagentid = "6";
  15. public static String qbagentid = "6";
  16. public static String zwagentid = "4";
  17. public static String ggagentid = "5";
  18. public static String jhrcagentid = "5";
  19. public static String yjagentid = "2";
  20. public static String txlagentid = "5";
  21. public static String wdglagentid = "11";
  22. public static String useragentid = "0";
  23. public static String gwccid = "13";
  24. public static void main(String[] args) throws IOException {
  25. // WeiXinUtil ws = new WeiXinUtil();
  26. // String access_token = ws.findaccess_token();
  27. // System.out.println(access_token);
  28. /**
  29. * 添加测试
  30. // */
  31. // Map<String, Object> map = new HashMap<String, Object>();
  32. // map.put("userid", "lisi");
  33. // map.put("name", "李四");
  34. // map.put("department","[6]");
  35. // map.put("position", "");
  36. // map.put("mobile", "18921780908");
  37. // map.put("gender", "1");
  38. // map.put("email", "");
  39. // map.put("weixinid","");
  40. // map.put("avatar_mediaid","");
  41. // map.put("extattr","");
  42. // System.out.println(ws.createuser(access_token, map));
  43. /**
  44. * 删除测试
  45. */
  46. // System.out.println(ws.deleteuser(access_token, "juke"));
  47. /**
  48. * 修改测试
  49. */
  50. // Map<String, Object> map = new HashMap<String, Object>();
  51. // map.put("userid", "lisi");
  52. // map.put("name", "李四");
  53. // map.put("department","[3]");
  54. // map.put("position", "");
  55. // map.put("mobile", "18921780908");
  56. // map.put("gender", "1");
  57. // map.put("email", "");
  58. // map.put("weixinid","");
  59. // map.put("avatar_mediaid","");
  60. // map.put("extattr","");
  61. // map.put("enable", "1");
  62. // System.out.println(ws.updateuser(access_token, map));
  63. }
  64. /**
  65. * 删除指定通讯录用户
  66. *
  67. * @param access_token
  68. * @param userid
  69. * @return
  70. */
  71. public static String deleteuser(String access_token, String userid) {
  72. String s = HttpUtil.sendGet("https://qyapi.weixin.qq.com/cgi-bin/user/delete","access_token="+access_token+"&userid="+userid);
  73. return s;
  74. }
  75. /**
  76. * 获取access_token
  77. * @return
  78. * @throws IOException
  79. */
  80. public static String findaccess_token() throws IOException {
  81. String s = HttpUtil.sendGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken",
  82. "corpid="+ParamesAPI.corpId+"&corpsecret="+ParamesAPI.secret);
  83. String access_token = JSONObject.fromObject(s).get("access_token").toString();
  84. return access_token;
  85. }
  86. public static String condetouserid(String access_token,String code,String sid) throws IOException {
  87. String s = HttpUtil.sendGet("https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo",
  88. "access_token="+access_token+"&code="+code+"&agentid="+sid);
  89. return s;
  90. }
  91. /**
  92. * 向通讯录添加成员
  93. * @param access_token
  94. * @param json
  95. * @return
  96. */
  97. public static String createuser(String access_token, Map<String, Object> map) {
  98. /**
  99. * @参数 必须 说明
  100. * @access_token 是 调用接口凭证
  101. * @userid 是 成员UserID。对应管理端的帐号,企业内必须唯一。长度为1~64个字节
  102. * @name 是 成员名称。长度为1~64个字节
  103. * @department 否 成员所属部门id列表。注意,每个部门的直属成员上限为1000个
  104. * @position 否 职位信息。长度为0~64个字节
  105. * @mobile 否 手机号码。企业内必须唯一,mobile/weixinid/email三者不能同时为空
  106. * @gender 否 性别。1表示男性,2表示女性
  107. * @email 否 邮箱。长度为0~64个字节。企业内必须唯一
  108. * @weixinid 否 微信号。企业内必须唯一。(注意:是微信号,不是微信的名字)
  109. * @avatar_mediaid 否 成员头像的mediaid,通过多媒体接口上传图片获得的mediaid
  110. * @extattr 否 扩展属性。扩展属性需要在WEB管理端创建后才生效,否则忽略未知属性的赋值
  111. */
  112. String json = "{" + "\"userid\": \"" + map.get("userid") + "\","
  113. + "\"name\": \"" + map.get("name") + "\"," + "\"department\":"
  114. + map.get("department") + "," + "\"position\": \""
  115. + map.get("position") + "\"," + "\"mobile\": \""
  116. + map.get("mobile") + "\"," + "\"gender\": \""
  117. + map.get("gender") + "\"," + "\"email\": \""
  118. + map.get("email") + "\"," + "\"weixinid\": \""
  119. + map.get("weixinid") + "\",avatar_mediaid:\""
  120. + map.get("avatar_mediaid") + "\",extattr:\""
  121. + map.get("extattr") + "\"}";
  122. String s = HttpUtil.sendPost("https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token="
  123. + access_token, json);
  124. return s;
  125. }
  126. /**
  127. * 更新通讯录成员
  128. *
  129. * @param access_token
  130. * @param json
  131. * @return
  132. */
  133. public static String updateuser(String access_token, Map<String, Object> map) {
  134. /**
  135. * @参数 必须 说明
  136. * @access_token 是 调用接口凭证
  137. * @userid 是 成员UserID。对应管理端的帐号,企业内必须唯一。长度为1~64个字节
  138. * @name 是 成员名称。长度为1~64个字节
  139. * @department 否 成员所属部门id列表。注意,每个部门的直属成员上限为1000个
  140. * @position 否 职位信息。长度为0~64个字节
  141. * @mobile 否 手机号码。企业内必须唯一,mobile/weixinid/email三者不能同时为空
  142. * @gender 否 性别。1表示男性,2表示女性
  143. * @email 否 邮箱。长度为0~64个字节。企业内必须唯一
  144. * @weixinid 否 微信号。企业内必须唯一。(注意:是微信号,不是微信的名字)
  145. * @avatar_mediaid 否 成员头像的mediaid,通过多媒体接口上传图片获得的mediaid
  146. * @extattr 否 扩展属性。扩展属性需要在WEB管理端创建后才生效,否则忽略未知属性的赋值
  147. * @enable 否 启用/禁用成员。1表示启用成员,0表示禁用成员
  148. */
  149. String json = "{" + "\"userid\": \"" + map.get("userid") + "\","
  150. + "\"name\": \"" + map.get("name") + "\"," + "\"department\":"
  151. + map.get("department") + "," + "\"position\": \""
  152. + map.get("position") + "\"," + "\"mobile\": \""
  153. + map.get("mobile") + "\"," + "\"gender\": \""
  154. + map.get("gender") + "\"," + "\"email\": \""
  155. + map.get("email") + "\"," + "\"weixinid\": \""
  156. + map.get("weixinid") + "\",avatar_mediaid:\""
  157. + map.get("avatar_mediaid") + "\",extattr:\""
  158. + map.get("extattr") + "\",enable:\""+map.get("enable")+"\"}";
  159. String s = HttpUtil.sendPost(
  160. "https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token="
  161. + access_token, json);
  162. return s;
  163. }
  164. /**
  165. * 更新通讯录成员
  166. *
  167. * @param access_token
  168. * @param json
  169. * @return
  170. */
  171. public static String updatebumen(String access_token,String userid,String department) {
  172. /**
  173. * @参数 必须 说明
  174. * @access_token 是 调用接口凭证
  175. * @userid 是 成员UserID。对应管理端的帐号,企业内必须唯一。长度为1~64个字节
  176. * @name 是 成员名称。长度为1~64个字节
  177. * @department 否 成员所属部门id列表。注意,每个部门的直属成员上限为1000个
  178. * @position 否 职位信息。长度为0~64个字节
  179. * @mobile 否 手机号码。企业内必须唯一,mobile/weixinid/email三者不能同时为空
  180. * @gender 否 性别。1表示男性,2表示女性
  181. * @email 否 邮箱。长度为0~64个字节。企业内必须唯一
  182. * @weixinid 否 微信号。企业内必须唯一。(注意:是微信号,不是微信的名字)
  183. * @avatar_mediaid 否 成员头像的mediaid,通过多媒体接口上传图片获得的mediaid
  184. * @extattr 否 扩展属性。扩展属性需要在WEB管理端创建后才生效,否则忽略未知属性的赋值
  185. * @enable 否 启用/禁用成员。1表示启用成员,0表示禁用成员
  186. */
  187. String json = "{" + "\"userid\": \"" + userid + "\","
  188. + "\"department\":\""+department+"\"}";
  189. String s = HttpUtil.sendPost(
  190. "https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token="
  191. + access_token, json);
  192. return s;
  193. }
  194. /**
  195. * 发起https请求并获取结果
  196. *
  197. * @param requestUrl
  198. * 请求地址
  199. * @param requestMethod
  200. * 请求方式(GET、POST)
  201. * @param outputStr
  202. * 提交的数据
  203. * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值)
  204. */
  205. public static JSONObject httpRequest(String request, String RequestMethod, String output) {
  206. JSONObject jsonObject = null;
  207. StringBuffer buffer = new StringBuffer();
  208. try {
  209. // 建立连接
  210. URL url = new URL(request);
  211. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  212. connection.setDoOutput(true);
  213. connection.setDoInput(true);
  214. connection.setUseCaches(false);
  215. connection.setRequestMethod(RequestMethod);
  216. if (output != null) {
  217. OutputStream out = connection.getOutputStream();
  218. out.write(output.getBytes("UTF-8"));
  219. out.close();
  220. }
  221. // 流处理
  222. InputStream input = connection.getInputStream();
  223. InputStreamReader inputReader = new InputStreamReader(input, "UTF-8");
  224. BufferedReader reader = new BufferedReader(inputReader);
  225. String line;
  226. while ((line = reader.readLine()) != null) {
  227. buffer.append(line);
  228. }
  229. // 关闭连接、释放资源
  230. reader.close();
  231. inputReader.close();
  232. input.close();
  233. input = null;
  234. connection.disconnect();
  235. jsonObject = JSONObject.fromObject(buffer.toString());
  236. } catch (Exception e) {
  237. }
  238. return jsonObject;
  239. }
  240. // 获取access_token的接口地址(GET)
  241. public final static String access_token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=CorpID&corpsecret=SECRET";
  242. /**
  243. * 获取access_token
  244. *
  245. * @param CorpID
  246. * 企业Id
  247. * @param SECRET
  248. * 管理组的凭证密钥,每个secret代表了对应用、通讯录、接口的不同权限;不同的管理组拥有不同的secret
  249. * @return
  250. */
  251. public static AccessToken getAccessToken(String corpID, String secret) {
  252. AccessToken accessToken = null;
  253. String requestUrl = access_token_url.replace("CorpID", corpID).replace("SECRET", secret);
  254. JSONObject jsonObject = httpRequest(requestUrl, "GET", null);
  255. // 如果请求成功
  256. if (null != jsonObject) {
  257. try {
  258. accessToken = new AccessToken();
  259. accessToken.setToken(jsonObject.getString("access_token"));
  260. accessToken.setExpiresIn(jsonObject.getInt("expires_in"));
  261. System.out.println("获取token成功:" + jsonObject.getString("access_token") + "————" + jsonObject.getInt("expires_in"));
  262. } catch (Exception e) {
  263. accessToken = null;
  264. // 获取token失败
  265. String error = String.format("获取token失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
  266. System.out.println(error);
  267. }
  268. }
  269. return accessToken;
  270. }
  271. // 菜单创建(POST)
  272. public static String menu_create_url = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN&agentid=1";
  273. /**
  274. * 创建菜单
  275. *
  276. * @param menu
  277. * 菜单实例
  278. * @param accessToken
  279. * 有效的access_token
  280. * @param agentid
  281. * 企业应用的id,整型,可在应用的设置页面查看
  282. * @return 0表示成功,其他值表示失败
  283. */
  284. // public static int createMenu(Menu menu, String accessToken) {
  285. // int result = 0;
  286. //
  287. // // 拼装创建菜单的url
  288. // String url = menu_create_url.replace("ACCESS_TOKEN", accessToken);
  289. // // 将菜单对象转换成json字符串
  290. // String jsonMenu = JSONObject.fromObject(menu).toString();
  291. // // 调用接口创建菜单
  292. // JSONObject jsonObject = httpRequest(url, "POST", jsonMenu);
  293. //
  294. // if (null != jsonObject) {
  295. // if (0 != jsonObject.getInt("errcode")) {
  296. // result = jsonObject.getInt("errcode");
  297. // String error = String.format("创建菜单失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
  298. // System.out.println(error);
  299. // }
  300. // }
  301. //
  302. // return result;
  303. // }
  304. public static String URLEncoder(String str) {
  305. String result = str;
  306. try {
  307. result = java.net.URLEncoder.encode(result, "UTF-8");
  308. } catch (Exception e) {
  309. e.printStackTrace();
  310. }
  311. return result;
  312. }
  313. /**
  314. * 根据内容类型判断文件扩展名
  315. *
  316. * @param contentType
  317. * 内容类型
  318. * @return
  319. */
  320. public static String getFileEndWitsh(String contentType) {
  321. String fileEndWitsh = "";
  322. if ("image/jpeg".equals(contentType))
  323. fileEndWitsh = ".jpg";
  324. else if ("audio/mpeg".equals(contentType))
  325. fileEndWitsh = ".mp3";
  326. else if ("audio/amr".equals(contentType))
  327. fileEndWitsh = ".amr";
  328. else if ("video/mp4".equals(contentType))
  329. fileEndWitsh = ".mp4";
  330. else if ("video/mpeg4".equals(contentType))
  331. fileEndWitsh = ".mp4";
  332. return fileEndWitsh;
  333. }
  334. //
  335. // /**
  336. // * 数据提交与请求通用方法
  337. // *
  338. // * @param access_token
  339. // * 凭证
  340. // * @param RequestMt
  341. // * 请求方式
  342. // * @param RequestURL
  343. // * 请求地址
  344. // * @param outstr
  345. // * 提交json数据
  346. // * */
  347. // public static int PostMessage(String access_token, String RequestMt, String RequestURL, String outstr) {
  348. // int result = 0;
  349. // RequestURL = RequestURL.replace("ACCESS_TOKEN", access_token);
  350. // JSONObject jsonobject = WeixinUtil.httpRequest(RequestURL, RequestMt, outstr);
  351. // if (null != jsonobject) {
  352. // if (0 != jsonobject.getInt("errcode")) {
  353. // result = jsonobject.getInt("errcode");
  354. // String error = String.format("操作失败 errcode:{} errmsg:{}", jsonobject.getInt("errcode"), jsonobject.getString("errmsg"));
  355. // System.out.println(error);
  356. // }
  357. // }
  358. // return result;
  359. // }
  360. //
  361. //
  362. //
  363. }