微信企业号开发(五)--成员管理,增删改查
来源:互联网 发布:个人备案域名 编辑:程序博客网 时间:2024/04/30 19:39
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<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;}
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、
/** * 用户基本信息 * */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=0private int gender;// 办公电话private String tel;// 邮箱private String email;// 微信号private String weixinid;// 扩展属性private String extattr;@Overridepublic 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、创建成员结果
0 0
- 微信企业号开发(五)--成员管理,增删改查
- 微信企业号开发(四)-管理部门,增删改查
- 增删改查开发规范
- 增删改查开发规范
- PHP开发增删改查
- 微信企业号开发(六)---邀请成员关注
- Android学习--------实现增删改查数据库操作以及实现类似微信好友对话管理操作
- XML(五)dom4j增删改查
- 微信企业号开发五:回调模式
- 基本表管理和增删改查
- 用户组管理系列(一):增删改查
- 客户管理系统的增删改查
- 学生管理系统(增删改查)
- Java景区管理增删改查模块
- Java管理Cookie增删改查操作。
- 微信企业号开发(4)--管理通讯录(部门)
- 微信企业号开发(5)--管理素材文件
- 微信企业号开发之如何管理多媒体文件
- Oracle SQL查询表的时点记录(咋个办呢 zgbn)
- 修改mysql用户密码
- (12)shell for while until循环
- VS2012 error : Required file tracker.exe is missing 解决办法
- 录音(PCM格式)、播放、lame转码成MP3
- 微信企业号开发(五)--成员管理,增删改查
- 集合框架
- C++小练习题,很有趣!很简单!-- 002 (咋个办呢 zgbn)
- openstack neutron gre网络拓扑实验总结
- GDB 进行调试 使用心得
- jsp 通过流的方式读取图片
- TBB之Exceptions and Cancellation
- 推荐Pyramid
- EDMA使用中遇到cache一致性的问题