【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();
    }
}

發佈了1023 篇原創文章 · 獲贊 273 · 訪問量 1030萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章