【fastweixin框架教程5】企业号成员部门管理


  下面这个类我对fastweixin 框架的简单封装调用,大家可以参考,如果需要QYAPIConfigExt和MainServernSupport,请参考本教程以前几篇文章

  目标:通讯录的部门的相关操作管理,通讯录管理包括部门管理、成员管理、标签管理三个部分,

             成员的管理操作,包括创建、删除、更新、获取、获取部门成员几个操作要点封装。


注意事项:1、参考官方文档,确保有相应的权限进行操作

                   2、注意所有名字长度是字节,不是字数,ID长度是int

                   3、截至写稿为止,疼讯已经禁止对成员进行邀请,所以返回 错误值。

package com.nsjs;

import com.fastwixinextend.QYAPIConfigExt;

import com.github.sd4324530.fastweixin.api.OauthAPI;
import com.github.sd4324530.fastweixin.api.config.ApiConfig;
import com.github.sd4324530.fastweixin.api.response.GetUserInfoResponse;
import com.github.sd4324530.fastweixin.company.api.QYDepartmentAPI;
import com.github.sd4324530.fastweixin.company.api.QYUserAPI;
import com.github.sd4324530.fastweixin.company.api.config.QYAPIConfig;
import com.github.sd4324530.fastweixin.company.api.entity.QYDepartment;

import com.github.sd4324530.fastweixin.company.api.entity.QYUser;
import com.github.sd4324530.fastweixin.company.api.enums.QYResultType;
import com.github.sd4324530.fastweixin.company.api.response.CreateDepartmentResponse;
import com.github.sd4324530.fastweixin.company.api.response.GetDepartmentListResponse;

import com.github.sd4324530.fastweixin.company.api.response.GetQYUserInfo4DepartmentResponse;
import com.github.sd4324530.fastweixin.company.api.response.GetQYUserInfoResponse;
import com.github.sd4324530.fastweixin.company.api.response.GetQYUserInviteResponse;
import com.fastwixinextend.QYOauthGetTokenResponse;
import java.util.List;

//封装by luozhuang
public class MemberManage {

    public MemberManage() {
        super();
    }

    public QYAPIConfigExt config = null;

    public QYAPIConfigExt initConfig() {
        if (config == null) {
            config = new QYAPIConfigExt(MainServernSupport.getCropId(), MainServernSupport.getAPPSecret(),
                    "luozhuang", System.currentTimeMillis());
        }
        return config;
    }

    public QYAPIConfigExt initConfig(String CropId, String APPSecret, String AccessToken, long AccessTokenTime) {
        QYAPIConfigExt config;
        config = new QYAPIConfigExt(CropId, APPSecret, AccessToken, AccessTokenTime);
        return config;
    }

    public List<QYDepartment> getDepartmentList() {
        QYDepartmentAPI departmentAPI = new QYDepartmentAPI(initConfig());
        GetDepartmentListResponse response = departmentAPI.getList(1);
        for (QYDepartment department : response.getDepartments()) {
            System.out.println(department.toString());
        }
        return response.getDepartments();
    }

    /**
     * 创建部门
     *
     * @param id 部门id,整型。指定时必须大于1,不指定时则自动生成
     * @param name 名称 部门名称。长度限制为1~64个字节,字符不能包括\:*?"<>|
     * @param parentId 上级ID 父亲部门id。根部门id为1
     * @param order 在父部门中的次序值。order值小的排序靠前。
     */
    public String createDepartment(int id, String name, int parentId, int order) {
        QYDepartmentAPI departmentAPI = new QYDepartmentAPI(initConfig());
        QYDepartment department = new QYDepartment(id, name, parentId, order);
        CreateDepartmentResponse response = departmentAPI.create(department);
        return response.getErrcode();
    }

    /**
     * @param id 部门id,整型。指定时必须大于1,不指定时则自动生成
     * @param name 名称 部门名称。长度限制为1~64个字节,字符不能包括\:*?"<>|
     * @param parentId 上级ID 父亲部门id。根部门id为1
     * @return
     */
    public String createDepartment(int id, String name, int parentId) {
        QYDepartmentAPI departmentAPI = new QYDepartmentAPI(initConfig());
        QYDepartment department = new QYDepartment(id, name, parentId, 1);
        CreateDepartmentResponse response = departmentAPI.create(department);
        return response.getErrcode();
    }

    /**
     * 更新部门
     *
     * @param id 部门id,整型。指定时必须大于1,不指定时则自动生成
     * @param name 名称 部门名称。长度限制为1~64个字节,字符不能包括\:*?"<>|
     * @param parentId 上级ID 父亲部门id。根部门id为1
     * @param order 在父部门中的次序值。order值小的排序靠前。
     */
    public String updateDepartment(int id, String name, int parentId, int order) {
        QYDepartmentAPI departmentAPI = new QYDepartmentAPI(initConfig());
        QYDepartment department = new QYDepartment(id, name, parentId, order);
        QYResultType resultType = departmentAPI.update(department);
        return String.valueOf(resultType.getCode());
    }

    /**
     * 删除部门
     *
     * @param id 部门ID
     * @return
     */
    public String deleteDepartment(int id) {
        QYDepartmentAPI departmentAPI = new QYDepartmentAPI(initConfig());
        QYResultType resultType = departmentAPI.delete(id);
        return String.valueOf(resultType.getCode());
    }

    /**
     * @param userId 成员UserID。对应管理端的帐号,企业内必须唯一。长度为1~64个字节
     * @param name 成员名称。长度为1~64个字节
     * @param department 成员所属部门id列表
     * @param position 职位信息。长度为0~64个字节
     * @param mobile 手机号码。企业内必须唯一,mobile/weixinid/email三者不能同时为空
     * @param gender 性别。1表示男性,2表示女性
     * @param email 邮箱。长度为0~64个字节。企业内必须唯一
     * @param weixinid 微信号。企业内必须唯一。(注意:是微信号,不是微信的名字)
     */
    public String createUser(String userId, String name, Integer[] department, String position, String mobile,
            String gender, String email, String weixinid) {
        QYUserAPI userAPI = new QYUserAPI(initConfig());
        QYUser user = new QYUser(userId, name, department, position, mobile, QYUser.Gender.MAN, email, weixinid, null);
        QYResultType resultType = userAPI.create(user);
        return String.valueOf(resultType.getCode());
    }

    /**
     * @param userId 成员UserID。对应管理端的帐号,企业内必须唯一。长度为1~64个字节
     * @param name 成员名称。长度为1~64个字节
     * @param department 成员所属部门id列表
     * @param position 职位信息。长度为0~64个字节
     * @param mobile 手机号码。企业内必须唯一,mobile/weixinid/email三者不能同时为空
     * @param gender 性别。1表示男性,2表示女性
     * @param email 邮箱。长度为0~64个字节。企业内必须唯一
     * @param weixinid 微信号。企业内必须唯一。(注意:是微信号,不是微信的名字)
     */
    public String updateUser(String userId, String name, Integer[] department, String position, String mobile,
            String gender, String email, String weixinid) {
        QYUserAPI userAPI = new QYUserAPI(initConfig());
        QYUser user = new QYUser(userId, name, department, position, mobile, QYUser.Gender.MAN, email, weixinid, null);
        QYResultType resultType = userAPI.update(user);
        return String.valueOf(resultType.getCode());
    }

    /**
     *
     * @param userId
     * @return
     */
    public String GetUserExist(String userId) {
        QYUserAPI userAPI = new QYUserAPI(initConfig());
        GetQYUserInfoResponse response = userAPI.get(userId);
        return response.getErrcode();
    }

    /**
     *
     * @param userId
     * @return
     */
    public QYUser GetUserInfo(String userId) {
        QYUserAPI userAPI = new QYUserAPI(initConfig());
        GetQYUserInfoResponse response = userAPI.get(userId);
        return response.getUser();
    }

    /**
     *
     * @param userId
     * @return
     */
    public String deleteUser(String userId) {
        QYUserAPI userAPI = new QYUserAPI(initConfig());
        QYResultType resultType = userAPI.delete(userId);
        return String.valueOf(resultType.getCode());
    }

    /**
     * 邀请成员关注。返回值type为1时表示微信邀请,2为邮件邀请
     *
     * @param userId 用户ID
     * @return 邀请结果
     */
    public String inviteUser(String userId) {
        QYUserAPI userAPI = new QYUserAPI(initConfig());
        GetQYUserInviteResponse resultType = userAPI.invite(userId);
        return resultType.getErrcode();
    }

    public List<QYUser> getUserList() {
        QYUserAPI userAPI = new QYUserAPI(initConfig());
        GetQYUserInfo4DepartmentResponse response = userAPI.getList(1, false, 0);
        List<QYUser> users = null;
        if ("0".equals(response.getErrcode())) {
            users = response.getUserList();
            for (QYUser user : users) {
                Integer[] departments = user.getDepartment();
                for (int departmentId : departments) {
                    System.out.println(user.getUserId() + ":\t" + user.getName() + ":\t" + departmentId + ":\t"
                            + user.getWeixinid());
                }
            }
        } else {
            System.out.println(QYResultType.get(response.getErrcode()).getDescription());
        }
        return users;
    }

  
    public static void main(String[] arg) {
        MemberManage men = new MemberManage();
        men.getUserList();
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值