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、公共代码 请参考---微信企业号开发(四)部门管理http://blog.csdn.net/u014520797/article/details/50625012
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 GetGroup(String accesstoken, String departmentid){
List 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;
}
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 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、
/**
* 用户基本信息
*
*/
public class WeixinUserInfo {
// 员工Userid。对应管理端的帐号,企业内必须唯一。长度为1~64个字符
private String userid;
// 成员名称
private String name;
//成员所属部门id列表
private Object department;
//职位信息
private String position;
//手机
private String mobile;
// 性别。gender=0表示男,=1表示女。默认gender=0
private int gender;
// 办公电话
private String tel;
// 邮箱
private String email;
// 微信号
private String weixinid;
// 扩展属性
private String extattr;
@Override
public String toString() {
return "WeixinUserInfo [userid=" + userid + ", name=" + name + ", department=" + department + ", position="
+ position + ", mobile=" + mobile + ", gender=" + gender + ", tel=" + tel + ", email=" + email
+ ", weixinid=" + weixinid + ", extattr=" + extattr + "]";
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object getDepartment() {
return department;
}
public void setDepartment(Object department) {
this.department = department;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getWeixinid() {
return weixinid;
}
public void setWeixinid(String weixinid) {
this.weixinid = weixinid;
}
public String getExtattr() {
return extattr;
}
public void setExtattr(String extattr) {
this.extattr = extattr;
}
}
8、创建成员结果