Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【企业微信】增加微盘获取文件列表接口 #2643

Merged
merged 1 commit into from
May 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,30 @@ public interface WxCpOaWeDriveService {
*/
WxCpSpaceShare spaceShare(@NonNull String userId, @NonNull String spaceId) throws WxErrorException;

/**
* 获取文件列表
* 该接口用于获取指定地址下的文件列表。
* <p>
* 请求方式:POST(HTTPS)
* 请求地址: https://qyapi.weixin.qq.com/cgi-bin/wedrive/file_list?access_token=ACCESS_TOKEN
*
* @param request 获取文件列表请求参数
* @return
* @throws WxErrorException
*/
WxCpFileList fileList(@NonNull WxCpFileListRequest request) throws WxErrorException;

/**
* 上传文件
* 该接口用于向微盘中的指定位置上传文件。
* <p>
* 请求方式:POST(HTTPS)
* 请求地址: https://qyapi.weixin.qq.com/cgi-bin/wedrive/file_upload?access_token=ACCESS_TOKEN
*
* @param request 上传文件请求参数
* @return
* @throws WxErrorException
*/
WxCpFileUpload fileUpload(@NonNull WxCpFileUploadRequest request) throws WxErrorException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,18 @@ public WxCpSpaceShare spaceShare(@NonNull String userId, @NonNull String spaceId
return WxCpSpaceShare.fromJson(responseContent);
}

@Override
public WxCpFileList fileList(@NonNull WxCpFileListRequest request) throws WxErrorException {
String apiUrl = this.cpService.getWxCpConfigStorage().getApiUrl(FILE_LIST);
String responseContent = this.cpService.post(apiUrl, request.toJson());
return WxCpFileList.fromJson(responseContent);
}

@Override
public WxCpFileUpload fileUpload(@NonNull WxCpFileUploadRequest request) throws WxErrorException {
String apiUrl = this.cpService.getWxCpConfigStorage().getApiUrl(FILE_UPLOAD);
String responseContent = this.cpService.post(apiUrl, request.toJson());
return WxCpFileUpload.fromJson(responseContent);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package me.chanjar.weixin.cp.bean.oa.wedrive;

import com.google.gson.annotations.SerializedName;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;

import java.io.Serializable;
import java.util.List;

/**
* 获取邀请链接.
*
* @author Wang_Wong
*/
@Data
public class WxCpFileList extends WxCpBaseResp implements Serializable {
private static final long serialVersionUID = -5028321625142879581L;

@SerializedName("has_more")
private Boolean hasMore;

@SerializedName("next_start")
private Integer nextStart;

@SerializedName("file_list")
private FileList fileList;

@Getter
@Setter
public static class FileList implements Serializable {
private static final long serialVersionUID = -4960239393895754598L;

@SerializedName("item")
private List<Item> item;

public static FileList fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, FileList.class);
}

public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}

}

@Getter
@Setter
public static class Item implements Serializable {
private static final long serialVersionUID = -4960239393895754598L;

@SerializedName("fileid")
private String fileId;

@SerializedName("file_name")
private String fileName;

@SerializedName("spaceid")
private String spaceId;

@SerializedName("fatherid")
private String fatherId;

@SerializedName("file_size")
private Long fileSize;

@SerializedName("ctime")
private Long cTime;

@SerializedName("mtime")
private Long mTime;

@SerializedName("file_type")
private Integer fileType;

@SerializedName("file_status")
private Integer fileStatus;

@SerializedName("create_userid")
private String createUserId;

@SerializedName("update_userid")
private String updateUserId;

@SerializedName("sha")
private String sha;

@SerializedName("url")
private String url;

@SerializedName("md5")
private String md5;

public static Item fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, Item.class);
}

public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}

}

public static WxCpFileList fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpFileList.class);
}

public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package me.chanjar.weixin.cp.bean.oa.wedrive;

import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;

import java.io.Serializable;

/**
* 获取文件列表请求.
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class WxCpFileListRequest implements Serializable {
private static final long serialVersionUID = -4960239393895754138L;

@SerializedName("userid")
private String userId;

@SerializedName("spaceid")
private String spaceId;

@SerializedName("fatherid")
private String fatherId;

@SerializedName("sort_type")
private Integer sortType;

@SerializedName("start")
private Integer start;

@SerializedName("limit")
private Integer limit;

public static WxCpFileListRequest fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpFileListRequest.class);
}

public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package me.chanjar.weixin.cp.bean.oa.wedrive;

import com.google.gson.annotations.SerializedName;
import lombok.Data;
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;

import java.io.Serializable;

/**
* 上传文件返回信息.
*
* @author Wang_Wong
*/
@Data
public class WxCpFileUpload extends WxCpBaseResp implements Serializable {
private static final long serialVersionUID = -5028321625142879581L;

@SerializedName("fileid")
private String fileId;

public static WxCpFileUpload fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpFileUpload.class);
}

public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package me.chanjar.weixin.cp.bean.oa.wedrive;

import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;

import java.io.Serializable;

/**
* 上传文件请求.
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class WxCpFileUploadRequest implements Serializable {
private static final long serialVersionUID = -4960239393895754138L;

@SerializedName("userid")
private String userId;

@SerializedName("spaceid")
private String spaceId;

@SerializedName("fatherid")
private String fatherId;

@SerializedName("file_name")
private String fileName;

@SerializedName("file_base64_content")
private String fileBase64Content;

public static WxCpFileUploadRequest fromJson(String json) {
return WxCpGsonBuilder.create().fromJson(json, WxCpFileUploadRequest.class);
}

public String toJson() {
return WxCpGsonBuilder.create().toJson(this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ interface Oa {
String SPACE_ACL_DEL = "/cgi-bin/wedrive/space_acl_del";
String SPACE_SETTING = "/cgi-bin/wedrive/space_setting";
String SPACE_SHARE = "/cgi-bin/wedrive/space_share";
String FILE_LIST = "/cgi-bin/wedrive/file_list";
String FILE_UPLOAD = "/cgi-bin/wedrive/file_upload";

/**
* 审批流程引擎
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package me.chanjar.weixin.cp.api;

import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
import me.chanjar.weixin.cp.bean.oa.wedrive.*;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.demo.WxCpDemoInMemoryConfigStorage;
import org.testng.annotations.Test;
import sun.misc.BASE64Encoder;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -25,7 +28,7 @@ public class WxCpOaWeDriveServiceTest {
private static WxCpService cpService;

@Test
public void test() throws WxErrorException {
public void test() throws Exception {

InputStream inputStream = ClassLoader.getSystemResourceAsStream("test-config.xml");
WxCpDemoInMemoryConfigStorage config = WxCpDemoInMemoryConfigStorage.fromXml(inputStream);
Expand All @@ -40,6 +43,43 @@ public void test() throws WxErrorException {

String uId = "WangKai";
String spId = "s.ww45d3e188865aca30.652091685u4h";
// 空间的文件id
String fileId = "s.ww45d3e188865aca30.652091685u4h_f.652344507ysDL";

/**
* 上传文件
*/
WxCpFileUploadRequest fileUploadRequest = new WxCpFileUploadRequest();
fileUploadRequest.setUserId(uId);
fileUploadRequest.setSpaceId(spId);
fileUploadRequest.setFatherId(spId);
fileUploadRequest.setFileName("第一个文件");

// 将文件转成base64字符串
File file = new File("D:/info.log.2022-05-07.0.log");
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int)file.length()];
inputFile.read(buffer);
inputFile.close();
String encodeBase64Content = new BASE64Encoder().encode(buffer);
fileUploadRequest.setFileBase64Content(encodeBase64Content);

WxCpFileUpload fileUpload = cpService.getOaWeDriveService().fileUpload(fileUploadRequest);
log.info("上传文件为:{}", fileUpload.toJson());

/**
* 获取文件列表
*/
WxCpFileListRequest fileListRequest = new WxCpFileListRequest();
fileListRequest.setUserId(uId);
fileListRequest.setSpaceId(spId);
fileListRequest.setFatherId(spId);
fileListRequest.setSortType(1);
fileListRequest.setStart(0);
fileListRequest.setLimit(100);

WxCpFileList fileList = cpService.getOaWeDriveService().fileList(fileListRequest);
log.info("获取文件列表为:{}", fileList.toJson());

/**
* 权限管理
Expand Down