guang
2023-07-27 6e713d8102775b555fff5607158c9cfab76fa678
增加企业微盘操作工具类
2个文件已添加
405 ■■■■■ 已修改文件
src/main/java/com/hx/api/CorpMpSpaceApi.java 182 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/hx/mp/util/CorpMpSpaceUtil.java 223 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/hx/api/CorpMpSpaceApi.java
New file
@@ -0,0 +1,182 @@
package com.hx.api;
import com.alibaba.fastjson.JSONObject;
import com.dtflys.forest.annotation.DataVariable;
import com.dtflys.forest.annotation.JSONBody;
import com.dtflys.forest.annotation.Post;
/**
 * 企业微信微盘接口API
 * @Author: cmg
 * @Date: 2023-7-13 11:08
 */
public interface CorpMpSpaceApi {
    /**
     * 增加空间
     * @param accessToken
     * @param param
     * @return
     */
    @Post(
            url = "https://qyapi.weixin.qq.com/cgi-bin/wedrive/space_create?access_token=${accessToken}",
            headers = {
                    "Content-Type: application/json"
            }
    )
    JSONObject addSpace(@DataVariable("accessToken") String accessToken, @JSONBody String param);
    /**
     * 重命名空间
     * @param accessToken
     * @param param
     * @return
     */
    @Post(
            url = "https://qyapi.weixin.qq.com/cgi-bin/wedrive/space_rename?access_token=${accessToken}",
            headers = {
                    "Content-Type: application/json"
            }
    )
    JSONObject renameSpace(@DataVariable("accessToken") String accessToken, @JSONBody String param);
    /**
     * 解散空间
     * @param accessToken
     * @param param
     * @return
     */
    @Post(
            url = "https://qyapi.weixin.qq.com/cgi-bin/wedrive/space_dismiss?access_token=${accessToken}",
            headers = {
                    "Content-Type: application/json"
            }
    )
    JSONObject dismissSpace(@DataVariable("accessToken") String accessToken, @JSONBody String param);
    /**
     * 空间安全设置
     * @param accessToken
     * @param param
     * @return
     */
    @Post(
            url = "https://qyapi.weixin.qq.com/cgi-bin/wedrive/space_setting?access_token=${accessToken}",
            headers = {
                    "Content-Type: application/json"
            }
    )
    JSONObject spaceSetting(@DataVariable("accessToken") String accessToken, @JSONBody String param);
    /**
     * 新建文件夹/文档
     * @param accessToken
     * @param param
     * @return
     */
    @Post(
            url = "https://qyapi.weixin.qq.com/cgi-bin/wedrive/file_create?access_token=${accessToken}",
            headers = {
                    "Content-Type: application/json"
            }
    )
    JSONObject createFile(@DataVariable("accessToken") String accessToken, @JSONBody String param);
    /**
     * 文件重命名
     * @param accessToken
     * @param param
     * @return
     */
    @Post(
            url = "https://qyapi.weixin.qq.com/cgi-bin/wedrive/file_rename?access_token=${accessToken}",
            headers = {
                    "Content-Type: application/json"
            }
    )
    JSONObject renameFile(@DataVariable("accessToken") String accessToken, @JSONBody String param);
    /**
     * 移除文件
     * @param accessToken
     * @param param
     * @return
     */
    @Post(
            url = "https://qyapi.weixin.qq.com/cgi-bin/wedrive/file_delete?access_token=${accessToken}",
            headers = {
                    "Content-Type: application/json"
            }
    )
    JSONObject deleteFile(@DataVariable("accessToken") String accessToken, @JSONBody String param);
    /**
     * 分块上传初始化
     * @param accessToken
     * @param param
     * @return
     */
    @Post(
            url = "https://qyapi.weixin.qq.com/cgi-bin/wedrive/file_upload_init?access_token=${accessToken}",
            headers = {
                    "Content-Type: application/json"
            }
    )
    JSONObject fileUploadInit(@DataVariable("accessToken") String accessToken, @JSONBody String param);
    /**
     * 分块上传文件
     * @param accessToken
     * @param param
     * @return
     */
    @Post(
            url = "https://qyapi.weixin.qq.com/cgi-bin/wedrive/file_upload_part?access_token=${accessToken}",
            headers = {
                    "Content-Type: application/json"
            }
    )
    JSONObject fileUploadPart(@DataVariable("accessToken") String accessToken, @JSONBody String param);
    /**
     * 分块上传完成
     * @param accessToken
     * @param param
     * @return
     */
    @Post(
            url = "https://qyapi.weixin.qq.com/cgi-bin/wedrive/file_upload_finish?access_token=${accessToken}",
            headers = {
                    "Content-Type: application/json"
            }
    )
    JSONObject fileUploadFinish(@DataVariable("accessToken") String accessToken, @JSONBody String param);
    /**
     * 上传文件
     * @param accessToken
     * @param param
     * @return
     */
    @Post(
            url = "https://qyapi.weixin.qq.com/cgi-bin/wedrive/file_upload?access_token=${accessToken}",
            headers = {
                    "Content-Type: application/json"
            }
    )
    JSONObject fileUpload(@DataVariable("accessToken") String accessToken, @JSONBody String param);
    /**
     * 删除文件
     * @param accessToken
     * @param param
     * @return
     */
    @Post(
            url = "https://qyapi.weixin.qq.com/cgi-bin/wedrive/file_delete?access_token=${accessToken}",
            headers = {
                    "Content-Type: application/json"
            }
    )
    JSONObject fileDelete(@DataVariable("accessToken") String accessToken, @JSONBody String param);
}
src/main/java/com/hx/mp/util/CorpMpSpaceUtil.java
New file
@@ -0,0 +1,223 @@
package com.hx.mp.util;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hx.api.CorpMpSpaceApi;
import java.util.List;
/**
 * 企业微信微盘接口API
 * @Author: cmg
 * @Date: 2023-7-13 11:08
 */
public class CorpMpSpaceUtil {
    /**
     * 增加空间
     * @param corpMpSpaceApi
     * @param accessToken
     * @param name 空间名称
     * @param depId 部门id
     * @param adminId 管理员的企业微信id
     * @return
     */
    public static JSONObject addSpace(CorpMpSpaceApi corpMpSpaceApi, String accessToken, String name, Integer depId, String adminId)
    {
        JSONObject obj = new JSONObject();
        obj.put("space_name", name);
        JSONArray arr = new JSONArray();
        //设置部门的可下载权限
        JSONObject temp = new JSONObject();
        temp.put("type", 2);
        temp.put("departmentid", depId);
        temp.put("auth", 1);
        arr.add(temp);
        //设置管理员
        temp = new JSONObject();
        temp.put("type", 1);
        temp.put("userid", adminId);
        temp.put("auth", 7);
        arr.add(temp);
        obj.put("auth_info", arr);
        return corpMpSpaceApi.addSpace(accessToken, obj.toJSONString());
    }
    /**
     * 空间重命名
     * @param corpMpSpaceApi
     * @param accessToken
     * @param spaceId
     * @param name
     * @return
     */
    public static JSONObject editSpaceName(CorpMpSpaceApi corpMpSpaceApi, String accessToken, String spaceId, String name)
    {
        JSONObject obj = new JSONObject();
        obj.put("space_name", name);
        obj.put("spaceid", spaceId);
        return corpMpSpaceApi.renameSpace(accessToken, obj.toJSONString());
    }
    /**
     * 空间设置
     * @param corpMpSpaceApi
     * @param accessToken
     * @param spaceId
     * @param enableConfidentialMode
     * @return
     */
    public static JSONObject spaceSetting(CorpMpSpaceApi corpMpSpaceApi, String accessToken, String spaceId, boolean enableConfidentialMode)
    {
        JSONObject obj = new JSONObject();
        obj.put("spaceid", spaceId);
        obj.put("enable_confidential_mode", enableConfidentialMode);
        return corpMpSpaceApi.spaceSetting(accessToken, obj.toJSONString());
    }
    /**
     * 创建文件夹
     * @param corpMpSpaceApi
     * @param accessToken
     * @param spaceId 空间id
     * @param fatherId 上级目录id,如果是空间下第一级,则是空间id
     * @param fileName 文件夹名称
     * @return
     */
    public static JSONObject createDir(CorpMpSpaceApi corpMpSpaceApi, String accessToken, String spaceId, String fatherId, String fileName)
    {
        JSONObject param = new JSONObject();
        param.put("spaceid", spaceId);
        param.put("fatherid", fatherId);
        param.put("file_type", 1);
        param.put("file_name", fileName);
        return corpMpSpaceApi.createFile(accessToken, param.toJSONString());
    }
    /**
     * 文件/文件夹重命名
     * @param corpMpSpaceApi
     * @param accessToken
     * @param fileId
     * @param fileName
     * @return
     */
    public static JSONObject editDirName(CorpMpSpaceApi corpMpSpaceApi, String accessToken, String fileId, String fileName)
    {
        JSONObject param = new JSONObject();
        param.put("fileid", fileId);
        param.put("new_name", fileName);
        return corpMpSpaceApi.renameFile(accessToken, param.toJSONString());
    }
    /**
     * 移除文件夹
     * @param corpMpSpaceApi
     * @param accessToken
     * @param fileId
     * @return
     */
    public static JSONObject deleteDir(CorpMpSpaceApi corpMpSpaceApi, String accessToken, String fileId)
    {
        JSONObject param = new JSONObject();
        JSONArray arr = new JSONArray();
        arr.add(fileId);
        param.put("fileid", arr);
        return corpMpSpaceApi.deleteFile(accessToken, param.toJSONString());
    }
    /**
     * 传10M以内文件
     * @param corpMpSpaceApi
     * @param accessToken
     * @param name
     * @param fileBase64Content
     * @param spaceId
     * @param fatherId
     * @return
     */
    public static JSONObject uploadFile(CorpMpSpaceApi corpMpSpaceApi, String accessToken, String name, String fileBase64Content, String spaceId, String fatherId) {
        JSONObject param = new JSONObject();
        param.put("spaceid", spaceId);
        param.put("fatherid", fatherId);
        param.put("file_name", name);
        param.put("file_base64_content", fileBase64Content);
        return corpMpSpaceApi.fileUpload(accessToken, param.toJSONString());
    }
    /**
     * 分块上传文件-初始化
     * @param corpMpSpaceApi
     * @param accessToken
     * @param spaceId
     * @param fatherId
     * @param selectTicket
     * @param fileName
     * @param size
     * @param skipPushCard
     * @param blockSha
     * @return
     */
    public static JSONObject uploadBigFileInit(CorpMpSpaceApi corpMpSpaceApi, String accessToken, String spaceId, String fatherId,
                                               String selectTicket, String fileName, Long size, boolean skipPushCard, List<String> blockSha) {
        JSONObject param = new JSONObject();
        param.put("spaceid", spaceId);
        param.put("fatherid", fatherId);
        param.put("selected_ticket", selectTicket);
        param.put("file_name", fileName);
        param.put("size", size);
        param.put("skip_push_card", skipPushCard);
        JSONArray arr = new JSONArray();
        for(String sha : blockSha)
        {
            arr.add(sha);
        }
        return corpMpSpaceApi.fileUploadInit(accessToken, param.toJSONString());
    }
    /**
     * 分块上传文件-部分上传
     * @param corpMpSpaceApi
     * @param accessToken
     * @param uploadKey
     * @param index
     * @param fileBase64Content
     * @return
     */
    public static JSONObject uploadBigFilePart(CorpMpSpaceApi corpMpSpaceApi, String accessToken, String uploadKey, int index, String fileBase64Content) {
        JSONObject param = new JSONObject();
        param.put("upload_key", uploadKey);
        param.put("index", index);
        param.put("file_base64_content", fileBase64Content);
        return corpMpSpaceApi.fileUploadPart(accessToken, param.toJSONString());
    }
    /**
     * 分块上传文件-上传完成
     * @param corpMpSpaceApi
     * @param accessToken
     * @param uploadKey
     * @return
     */
    public static JSONObject uploadBigFileFin(CorpMpSpaceApi corpMpSpaceApi, String accessToken, String uploadKey) {
        JSONObject param = new JSONObject();
        param.put("upload_key", uploadKey);
        return corpMpSpaceApi.fileUploadFinish(accessToken, param.toJSONString());
    }
}