123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- 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<String, Object> map = new HashMap<String, Object>();
- // 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<String, Object> map = new HashMap<String, Object>();
- // 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<String, Object> 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<String, Object> 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;
- // }
- //
- //
- //
-
-
-
- }
|