package util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.Map; import net.sf.json.JSONObject; public class WeiXinUtil { public static String whzysb = "1"; public static String fwagentid = "6"; public static String swagentid = "6"; public static String qbagentid = "6"; public static String zwagentid = "4"; public static String ggagentid = "5"; public static String jhrcagentid = "5"; public static String yjagentid = "2"; public static String txlagentid = "5"; public static String wdglagentid = "11"; public static String useragentid = "0"; public static String gwccid = "13"; public static void main(String[] args) throws IOException { // WeiXinUtil ws = new WeiXinUtil(); // String access_token = ws.findaccess_token(); // System.out.println(access_token); /** * 添加测试 // */ // Map map = new HashMap(); // map.put("userid", "lisi"); // map.put("name", "李四"); // map.put("department","[6]"); // map.put("position", ""); // map.put("mobile", "18921780908"); // map.put("gender", "1"); // map.put("email", ""); // map.put("weixinid",""); // map.put("avatar_mediaid",""); // map.put("extattr",""); // System.out.println(ws.createuser(access_token, map)); /** * 删除测试 */ // System.out.println(ws.deleteuser(access_token, "juke")); /** * 修改测试 */ // Map map = new HashMap(); // map.put("userid", "lisi"); // map.put("name", "李四"); // map.put("department","[3]"); // map.put("position", ""); // map.put("mobile", "18921780908"); // map.put("gender", "1"); // map.put("email", ""); // map.put("weixinid",""); // map.put("avatar_mediaid",""); // map.put("extattr",""); // map.put("enable", "1"); // System.out.println(ws.updateuser(access_token, map)); } /** * 删除指定通讯录用户 * * @param access_token * @param userid * @return */ public static String deleteuser(String access_token, String userid) { String s = HttpUtil.sendGet("https://qyapi.weixin.qq.com/cgi-bin/user/delete","access_token="+access_token+"&userid="+userid); return s; } /** * 获取access_token * @return * @throws IOException */ public static String findaccess_token() throws IOException { String s = HttpUtil.sendGet("https://qyapi.weixin.qq.com/cgi-bin/gettoken", "corpid="+ParamesAPI.corpId+"&corpsecret="+ParamesAPI.secret); String access_token = JSONObject.fromObject(s).get("access_token").toString(); return access_token; } public static String condetouserid(String access_token,String code,String sid) throws IOException { String s = HttpUtil.sendGet("https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo", "access_token="+access_token+"&code="+code+"&agentid="+sid); return s; } /** * 向通讯录添加成员 * @param access_token * @param json * @return */ public static String createuser(String access_token, Map map) { /** * @参数 必须 说明 * @access_token 是 调用接口凭证 * @userid 是 成员UserID。对应管理端的帐号,企业内必须唯一。长度为1~64个字节 * @name 是 成员名称。长度为1~64个字节 * @department 否 成员所属部门id列表。注意,每个部门的直属成员上限为1000个 * @position 否 职位信息。长度为0~64个字节 * @mobile 否 手机号码。企业内必须唯一,mobile/weixinid/email三者不能同时为空 * @gender 否 性别。1表示男性,2表示女性 * @email 否 邮箱。长度为0~64个字节。企业内必须唯一 * @weixinid 否 微信号。企业内必须唯一。(注意:是微信号,不是微信的名字) * @avatar_mediaid 否 成员头像的mediaid,通过多媒体接口上传图片获得的mediaid * @extattr 否 扩展属性。扩展属性需要在WEB管理端创建后才生效,否则忽略未知属性的赋值 */ String json = "{" + "\"userid\": \"" + map.get("userid") + "\"," + "\"name\": \"" + map.get("name") + "\"," + "\"department\":" + map.get("department") + "," + "\"position\": \"" + map.get("position") + "\"," + "\"mobile\": \"" + map.get("mobile") + "\"," + "\"gender\": \"" + map.get("gender") + "\"," + "\"email\": \"" + map.get("email") + "\"," + "\"weixinid\": \"" + map.get("weixinid") + "\",avatar_mediaid:\"" + map.get("avatar_mediaid") + "\",extattr:\"" + map.get("extattr") + "\"}"; String s = HttpUtil.sendPost("https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token=" + access_token, json); return s; } /** * 更新通讯录成员 * * @param access_token * @param json * @return */ public static String updateuser(String access_token, Map map) { /** * @参数 必须 说明 * @access_token 是 调用接口凭证 * @userid 是 成员UserID。对应管理端的帐号,企业内必须唯一。长度为1~64个字节 * @name 是 成员名称。长度为1~64个字节 * @department 否 成员所属部门id列表。注意,每个部门的直属成员上限为1000个 * @position 否 职位信息。长度为0~64个字节 * @mobile 否 手机号码。企业内必须唯一,mobile/weixinid/email三者不能同时为空 * @gender 否 性别。1表示男性,2表示女性 * @email 否 邮箱。长度为0~64个字节。企业内必须唯一 * @weixinid 否 微信号。企业内必须唯一。(注意:是微信号,不是微信的名字) * @avatar_mediaid 否 成员头像的mediaid,通过多媒体接口上传图片获得的mediaid * @extattr 否 扩展属性。扩展属性需要在WEB管理端创建后才生效,否则忽略未知属性的赋值 * @enable 否 启用/禁用成员。1表示启用成员,0表示禁用成员 */ String json = "{" + "\"userid\": \"" + map.get("userid") + "\"," + "\"name\": \"" + map.get("name") + "\"," + "\"department\":" + map.get("department") + "," + "\"position\": \"" + map.get("position") + "\"," + "\"mobile\": \"" + map.get("mobile") + "\"," + "\"gender\": \"" + map.get("gender") + "\"," + "\"email\": \"" + map.get("email") + "\"," + "\"weixinid\": \"" + map.get("weixinid") + "\",avatar_mediaid:\"" + map.get("avatar_mediaid") + "\",extattr:\"" + map.get("extattr") + "\",enable:\""+map.get("enable")+"\"}"; String s = HttpUtil.sendPost( "https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token=" + access_token, json); return s; } /** * 更新通讯录成员 * * @param access_token * @param json * @return */ public static String updatebumen(String access_token,String userid,String department) { /** * @参数 必须 说明 * @access_token 是 调用接口凭证 * @userid 是 成员UserID。对应管理端的帐号,企业内必须唯一。长度为1~64个字节 * @name 是 成员名称。长度为1~64个字节 * @department 否 成员所属部门id列表。注意,每个部门的直属成员上限为1000个 * @position 否 职位信息。长度为0~64个字节 * @mobile 否 手机号码。企业内必须唯一,mobile/weixinid/email三者不能同时为空 * @gender 否 性别。1表示男性,2表示女性 * @email 否 邮箱。长度为0~64个字节。企业内必须唯一 * @weixinid 否 微信号。企业内必须唯一。(注意:是微信号,不是微信的名字) * @avatar_mediaid 否 成员头像的mediaid,通过多媒体接口上传图片获得的mediaid * @extattr 否 扩展属性。扩展属性需要在WEB管理端创建后才生效,否则忽略未知属性的赋值 * @enable 否 启用/禁用成员。1表示启用成员,0表示禁用成员 */ String json = "{" + "\"userid\": \"" + userid + "\"," + "\"department\":\""+department+"\"}"; String s = HttpUtil.sendPost( "https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token=" + access_token, json); return s; } /** * 发起https请求并获取结果 * * @param requestUrl * 请求地址 * @param requestMethod * 请求方式(GET、POST) * @param outputStr * 提交的数据 * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值) */ public static JSONObject httpRequest(String request, String RequestMethod, String output) { JSONObject jsonObject = null; StringBuffer buffer = new StringBuffer(); try { // 建立连接 URL url = new URL(request); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); connection.setRequestMethod(RequestMethod); if (output != null) { OutputStream out = connection.getOutputStream(); out.write(output.getBytes("UTF-8")); out.close(); } // 流处理 InputStream input = connection.getInputStream(); InputStreamReader inputReader = new InputStreamReader(input, "UTF-8"); BufferedReader reader = new BufferedReader(inputReader); String line; while ((line = reader.readLine()) != null) { buffer.append(line); } // 关闭连接、释放资源 reader.close(); inputReader.close(); input.close(); input = null; connection.disconnect(); jsonObject = JSONObject.fromObject(buffer.toString()); } catch (Exception e) { } return jsonObject; } // 获取access_token的接口地址(GET) public final static String access_token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=CorpID&corpsecret=SECRET"; /** * 获取access_token * * @param CorpID * 企业Id * @param SECRET * 管理组的凭证密钥,每个secret代表了对应用、通讯录、接口的不同权限;不同的管理组拥有不同的secret * @return */ public static AccessToken getAccessToken(String corpID, String secret) { AccessToken accessToken = null; String requestUrl = access_token_url.replace("CorpID", corpID).replace("SECRET", secret); JSONObject jsonObject = httpRequest(requestUrl, "GET", null); // 如果请求成功 if (null != jsonObject) { try { accessToken = new AccessToken(); accessToken.setToken(jsonObject.getString("access_token")); accessToken.setExpiresIn(jsonObject.getInt("expires_in")); System.out.println("获取token成功:" + jsonObject.getString("access_token") + "————" + jsonObject.getInt("expires_in")); } catch (Exception e) { accessToken = null; // 获取token失败 String error = String.format("获取token失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg")); System.out.println(error); } } return accessToken; } // 菜单创建(POST) public static String menu_create_url = "https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN&agentid=1"; /** * 创建菜单 * * @param menu * 菜单实例 * @param accessToken * 有效的access_token * @param agentid * 企业应用的id,整型,可在应用的设置页面查看 * @return 0表示成功,其他值表示失败 */ // public static int createMenu(Menu menu, String accessToken) { // int result = 0; // // // 拼装创建菜单的url // String url = menu_create_url.replace("ACCESS_TOKEN", accessToken); // // 将菜单对象转换成json字符串 // String jsonMenu = JSONObject.fromObject(menu).toString(); // // 调用接口创建菜单 // JSONObject jsonObject = httpRequest(url, "POST", jsonMenu); // // if (null != jsonObject) { // if (0 != jsonObject.getInt("errcode")) { // result = jsonObject.getInt("errcode"); // String error = String.format("创建菜单失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg")); // System.out.println(error); // } // } // // return result; // } public static String URLEncoder(String str) { String result = str; try { result = java.net.URLEncoder.encode(result, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } return result; } /** * 根据内容类型判断文件扩展名 * * @param contentType * 内容类型 * @return */ public static String getFileEndWitsh(String contentType) { String fileEndWitsh = ""; if ("image/jpeg".equals(contentType)) fileEndWitsh = ".jpg"; else if ("audio/mpeg".equals(contentType)) fileEndWitsh = ".mp3"; else if ("audio/amr".equals(contentType)) fileEndWitsh = ".amr"; else if ("video/mp4".equals(contentType)) fileEndWitsh = ".mp4"; else if ("video/mpeg4".equals(contentType)) fileEndWitsh = ".mp4"; return fileEndWitsh; } // // /** // * 数据提交与请求通用方法 // * // * @param access_token // * 凭证 // * @param RequestMt // * 请求方式 // * @param RequestURL // * 请求地址 // * @param outstr // * 提交json数据 // * */ // public static int PostMessage(String access_token, String RequestMt, String RequestURL, String outstr) { // int result = 0; // RequestURL = RequestURL.replace("ACCESS_TOKEN", access_token); // JSONObject jsonobject = WeixinUtil.httpRequest(RequestURL, RequestMt, outstr); // if (null != jsonobject) { // if (0 != jsonobject.getInt("errcode")) { // result = jsonobject.getInt("errcode"); // String error = String.format("操作失败 errcode:{} errmsg:{}", jsonobject.getInt("errcode"), jsonobject.getString("errmsg")); // System.out.println(error); // } // } // return result; // } // // // }