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

微信企业号开发(五)--成员管理,增删改查

2016-02-03 10:21 531 查看
1、打开企业号开发文档之成员管理http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98

若打不开,链接地址为:http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AE%A1%E7%90%86%E6%88%90%E5%91%98

2、微信提供关于成员的接口如下:



3、公共代码 请参考---微信企业号开发(四)部门管理/article/9799830.html

4、微信成员管理URL

//创建成员地址
public static String CREATE_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token=ACCESS_TOKEN";
//更新成员地址
public static String UPDATE_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token=ACCESS_TOKEN";
//删滁成员地址
public static String DELETE_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/delete?access_token=ACCESS_TOKEN&userid=USERID";
//获取成员地址
public static String GET_PERSON_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&userid=USERID";
//获取部门成员地址
public static String GET_GROUP_URL = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token=ACCESS_TOKEN&department_id=DEPTID&fetch_child=0&status=0";


5、部分代码

/**
* 创建成员
* @param userid 员工UserID。对应管理端的帐号,企业内必须唯一。长度为1~64个字符
* @param name 成员名称。长度为1~64个字符
* @param departmentId 成员所属部门id列表 格式: "department": [x, y]
* @param position 职位信息
* @param mobile 手机号码。企业内必须唯一,mobile/weixinid/email三者不能同时为空
* @param gender 性别。gender=0表示男,=1表示女。默认gender=0
* @param tel 办公电话。长度为0~64个字符
* @param email 邮箱。长度为0~64个字符。企业内必须唯一
* @param weixinid 微信号。企业内必须唯一
* */
public static int Create(String accesstoken , String  userid,String name,String departmentId, String position ,String mobile ,String gender,String tel ,String email,String weixinid){
int errCode=0;
//拼接请求地址
String requestUrl=CREATE_URL.replace("ACCESS_TOKEN", accesstoken);
//需要提交的数据
String postJson = "{\"userid\":\"%s\",\"name\":\"%s\",\"department\": [1, \"%s\"],\"position\": \"%s\",\"mobile\": \"%s\",\"gender\": \"%s\",\"tel\":\"%s\",\"email\":\"%s\",\"weixinid\":\"%s\"}";
String outputStr=String.format(postJson, userid,name,departmentId,position,mobile,gender,tel,email,weixinid);
//创建成员
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 userid 员工UserID。对应管理端的帐号,企业内必须唯一。长度为1~64个字符
* @param name 成员名称。长度为1~64个字符
* @param department 成员所属部门id列表 格式: "department": [x]
* @param position 职位信息
* @param mobile 手机号码。企业内必须唯一,mobile/weixinid/email三者不能同时为空
* @param gender 性别。gender=0表示男,=1表示女。默认gender=0
* @param tel 办公电话。长度为0~64个字符
* @param email 邮箱。长度为0~64个字符。企业内必须唯一
* @param weixinid 微信号。企业内必须唯一
* @param enable 启用/禁用成员。1表示启用成员,0表示禁用成员
* */
public static int Update(String accesstoken ,String  userid,String name ,String position ,String mobile ,String gender,String tel ,String email,String weixinid,String enable){
int errCode=0;
//拼接请求地址
String requestUrl=UPDATE_URL.replace("ACCESS_TOKEN", accesstoken);
//需要提交的数据
String postJson = "{\"userid\":\"%s\",\"name\":\"%s\",\"department\": [1],\"position\":\"%s\",\"mobile\":\"%s\",\"gender\":\"%s\",\"tel\":\"%s\",\"email\":\"%s\",\"weixinid\":\"%s\",\"enable\":\"%s\"}";
String outputStr=String.format(postJson, userid,name,position,mobile,gender,tel,email,weixinid,enable);
//更新成员
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 userid 员工UserID。对应管理端的帐号
* */
public static int Delete(String accesstoken , String userid){
int errCode=0;
//拼接请求地址
String requestUrl=DELETE_URL.replace("ACCESS_TOKEN", accesstoken).replace("USERID", userid);
//删除成员
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
* */
public static WeixinUserInfo GetPerson(String accesstoken,String userId){
WeixinUserInfo weixinUserList=new WeixinUserInfo();
//拼接请求地址
String requestUrl=GET_PERSON_URL.replace("ACCESS_TOKEN", accesstoken).replace("USERID", userId);

//获取成员
JSONObject jsonObject=CommonUtil.httpsRequest(requestUrl, "GET", null);
if(null!=jsonObject){
if(0==jsonObject.getInt("errcode")){
log.info("获取成员成功");
weixinUserList.setUserid(jsonObject.getString("userid"));
weixinUserList.setName(jsonObject.getString("name"));
weixinUserList.setDepartment(jsonObject.get("department"));
System.out.println(jsonObject.get("department"));
}else{

int errCode=jsonObject.getInt("errcode");
String errMsg=jsonObject.getString("errmsg");
log.error("获取成员失败  errorcode:{} errmsg:{}",errCode,errMsg);
}
}
return weixinUserList;
}
/**
* 获取部门成员
* @param accesstoken
* @param department_id 获取的部门id
* @param fetch_child 1/0:是否递归获取子部门下面的成员 (可选)
* @param status 0获取全部员工,1获取已关注成员列表,2获取禁用成员列表,4获取未关注成员列表。status可叠加 (可选)
* */
@SuppressWarnings({"unchecked","deprecation"})
public static List<WeixinUserInfo> GetGroup(String accesstoken, String departmentid){
List<WeixinUserInfo> WeixinUserInfo=null;
//拼接请求地址
String requestUrl=GET_GROUP_URL.replace("ACCESS_TOKEN", accesstoken).replace("DEPTID", departmentid);;
//获取成员
JSONObject jsonObject=CommonUtil.httpsRequest(requestUrl, "GET", null);
if(null!=jsonObject){
if(0==jsonObject.getInt("errcode")){
log.info("获取部门成员成功");
WeixinUserInfo=JSONArray.toList
(jsonObject.getJSONArray("userlist"),WeixinUserInfo.class);
}else{
WeixinUserInfo=null;
int errCode=jsonObject.getInt("errcode");
String errMsg=jsonObject.getString("errmsg");
log.error("获取部门成员失败  errorcode:{} errmsg:{}",errCode,errMsg);
}
}
return WeixinUserInfo;
}


6、测试类
package com.kp.test;

import java.util.List;

import com.kp.bo.WeixinUserInfo;
import com.kp.oper.UserManagement;
import com.kp.util.CommonUtil;
import com.kp.util.ParamesAPI;

/**
* Description:测试添加成员 Author:py
*/
public class TestUser {

/**
* @Description : 测试更新成员(根据userid进行修改)
* @return void
* @author : py
*/
private static void testUpdate(String accesstoken) {
String userid = "user8";
String name = "name8";
String position = "";
String mobile = "15578789833";
String gender = "0";
String tel = "";
String email = "";
String weixinid = "";
String enable = "1";
int resultID = UserManagement.Update(accesstoken, userid, name, position, mobile, gender, tel, email, weixinid, enable );
System.out.println("TestUser testUpdate resultID:" + resultID);
}

/**
* @Description : 测试查询某个部门的全部成员
* @return void
* @author : py
*/
private static void testgetGroup(String accesstoken) {
List<WeixinUserInfo> getGroup = UserManagement.GetGroup(accesstoken, "1");
for (WeixinUserInfo wi : getGroup) {
System.out.println("testgetGroup :" + wi.toString());
}
}

/**
* @Description : 测试删除成员
* @return void
* @author : py
*/
private static void testDetele(String accesstoken) {
int resultID = UserManagement.Delete(accesstoken, "user8");
System.out.println("TestUser testDetele resultID:" + resultID);
}
/**
* @Description : 测试查询单个成员
* @return void
* @author : py
*/
private static void testQuery(String accesstoken) {
WeixinUserInfo getPerson = UserManagement.GetPerson(accesstoken, "user8");
System.out.println("testQuery :" + getPerson.toString());

}
/**
* @Description : 测试添加成员
* @return void
* @author : py
*/
private static void testInsert(String accesstoken) {
String userid = "user8";
String name = "name8";
String departmentId = "1";
String position = "";
String mobile = "15578789888";
String gender = "0";
String tel = "";
String email = "";
String weixinid = "";
int resultID = UserManagement.Create(accesstoken, userid, name, departmentId, position, mobile, gender, tel,
email, weixinid);
System.out.println("TestUser testInsert resultID:" + resultID);
}

public static void main(String[] args) {
String accesstoken = CommonUtil.getToken(ParamesAPI.corpId, ParamesAPI.corpsecret).getAccessToken();
System.out.println("accesstoken="+accesstoken);
//		testInsert(accesstoken);
testQuery(accesstoken);
//		testDetele(accesstoken);
//		testgetGroup(accesstoken);
//		testUpdate(accesstoken);
}
}


7、创建成员结果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: