您的位置:首页 > 移动开发 > 微信开发

微信企业号开发(四)-管理部门,增删改查

2016-02-02 17:47 573 查看
1、微信企业号开发中管理部门(增删改查),无需回调接口,只需要要获得accessToken值,main方法便能测试

2、参数类需要用自己企业号中的值

package com.kp.util;
/**
* 参数API类*/
public class ParamesAPI {
// token
public static String token = "Hs4MdnsN4lsygv2kmAZh";
// 随机戳
public static String encodingAESKey = "lKNJEsA4vPm9BDsbWUjA8txdLHpyUGKgzP8XU8gwkOKyj";
//你的企业号ID
public static String corpId = "wx45be3e764a73bb24";
// 管理组的凭证密钥,每个secret代表了对应用、通讯录、接口的不同权限;不同的管理组拥有不同的secret
public static String corpsecret = "ds7IkOMfDYFs4JJBztKub0z23pI9ZgIlL6sv1IjYkHmqeVEXkbxcR3drUkZ3iKGr";

}


3、获取token对象,token对象中含有accesstoken,以及有效时间(毫秒,一般为7200),实际项目中需要缓存起来,以免超过接口调用次数。

public static Token getToken(String corpid, String corpsecret) {
Token token = null;
String requestUrl = token_url.replace("CORPID", corpid).replace("CORPSECRET", corpsecret);
// 发起GET请求获取凭证
JSONObject jsonObject = httpsRequest(requestUrl, "GET", null);

if (null != jsonObject) {
try {
token = new Token();
token.setAccessToken(jsonObject.getString("access_token"));
token.setExpiresIn(jsonObject.getInt("expires_in"));
} catch (JSONException e) {
token = null;
// 获取token失败
log.error("获取token失败 errcode:{} errmsg:{}", jsonObject.getInt("errcode"), jsonObject.getString("errmsg"));
}
}
return token;
}


4、管理企业号部门常用的URL

// 创建部门地址
public static String CREATE_URL = "https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=ACCESS_TOKEN";
// 更新部门地址
public static String UPDATE_URL = "https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token=ACCESS_TOKEN";
// 删除部门地址
public static String DELETE_URL = "https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token=ACCESS_TOKEN&id=DEPTID";
// 获取部门列表地址
public static String GETLIST_URL = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=ACCESS_TOKEN";


5、增删改查企业号部门,增加部门有两种方式,一种是自定义id值,另一种随微信按照id值递增而分配

/**
* 创建部门
* @param accesstoken
* @param name 部门名称。长度限制为1~64个字符
* @param parentid 父亲部门id。根部门id为1
* */
public static int CreateByParentid(String accesstoken , String name , String parentid){
int errCode=0;
//拼接请求地址
String requestUrl=CREATE_URL.replace("ACCESS_TOKEN", accesstoken);
//需要提交的数据
String postJson = "{\"name\":\" %s\",\"parentid\": %s}";
String outputStr=String.format(postJson, name,parentid);
//创建部门
JSONObject jsonObject=CommonUtil.httpsRequest(requestUrl, "POST", outputStr);

if(null!=jsonObject){
if(0==jsonObject.getInt("errcode")){
log.info("创建部门成功");
}else{
errCode=jsonObject.getInt("errcode");
String errMsg=jsonObject.getString("errmsg");
log.error("创建部门失败  errorcode:{} errmsg:{}",errCode,errMsg);
}
}
return errCode;
}
public static int CreateByCustomid(String accesstoken , String name , String parentid,String id){
int errCode=0;
//拼接请求地址
String requestUrl=CREATE_URL.replace("ACCESS_TOKEN", accesstoken);
//需要提交的数据
String postJson = "{\"name\":\"%s\",\"parentid\": %s,\"id\": %s}";
String outputStr=String.format(postJson, name,parentid,id);
//创建部门
JSONObject jsonObject=CommonUtil.httpsRequest(requestUrl, "POST", outputStr);

if(null!=jsonObject){
if(0==jsonObject.getInt("errcode")){
log.info("创建部门成功");
}else{
errCode=jsonObject.getInt("errcode");
String errMsg=jsonObject.getString("errmsg");
log.error("创建部门失败  errorcode:{} errmsg:{}",errCode,errMsg);
}
}
return errCode;
}

/**
* 更新部门
* @param accesstoken
* @param name 部门名称,长度限制为1~64个字符
* @param id   部门id
* */
public static int Update(String accesstoken , String id , String name){
int errCode=0;
//拼接请求地址
String requestUrl=UPDATE_URL.replace("ACCESS_TOKEN", accesstoken);
//需要提交的数据
String postJson = "{\"id\":\" %s\",\"name\":\"%s\"}";
String outputStr=String.format(postJson, id, name);
//创建部门
JSONObject jsonObject=CommonUtil.httpsRequest(requestUrl, "POST", outputStr);

if(null!=jsonObject){
if(0==jsonObject.getInt("errcode")){
log.info("更新部门成功");
}else{
errCode=jsonObject.getInt("errcode");
String errMsg=jsonObject.getString("errmsg");
log.error("更新部门失败  errorcode:{} errmsg:{}",errCode,errMsg);
}
}
return errCode;
}
/**
* 删除部门
* @param accesstoken
* @param id 部门id
* */
public static int Delete(String accesstoken , String id){
int errCode=0;
//拼接请求地址
String requestUrl=DELETE_URL.replace("ACCESS_TOKEN", accesstoken).replace("DEPTID", id);
//删除部门
JSONObject jsonObject=CommonUtil.httpsRequest(requestUrl, "GET", null);

if(null!=jsonObject){
if(0==jsonObject.getInt("errcode")){
log.info("删除部门成功");
}else{
errCode=jsonObject.getInt("errcode");
String errMsg=jsonObject.getString("errmsg");
log.error("删除部门失败  errorcode:{} errmsg:{}",errCode,errMsg);
}
}
return errCode;
}
/**
* 获取部门列表
* @param accesstoken
* */
@SuppressWarnings({"unchecked","deprecation"})
public static List<WeixinDepartment> GetList(String accesstoken){
List<WeixinDepartment> weixinDepartment=null;
//拼接请求地址
String requestUrl=GETLIST_URL.replace("ACCESS_TOKEN", accesstoken);
//获取部门列表
JSONObject jsonObject=CommonUtil.httpsRequest(requestUrl, "GET", null);
if(null!=jsonObject){
if(0==jsonObject.getInt("errcode")){
log.info("获取部门列表成功");
weixinDepartment=JSONArray.toList
(jsonObject.getJSONArray("department"),WeixinDepartment.class);
}else{
weixinDepartment=null;
int errCode=jsonObject.getInt("errcode");
String errMsg=jsonObject.getString("errmsg");
log.error("获取部门列表失败  errorcode:{} errmsg:{}",errCode,errMsg);
}
}
return weixinDepartment;
}


6、结果:





7、源代码地址:http://download.csdn.net/detail/u014520797/9425942
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: