diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOaWeDriveService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOaWeDriveService.java index dd27ddbf87..32182f9f3c 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOaWeDriveService.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOaWeDriveService.java @@ -147,4 +147,51 @@ public interface WxCpOaWeDriveService { */ WxCpFileUpload fileUpload(@NonNull WxCpFileUploadRequest request) throws WxErrorException; + /** + * 下载文件 + * 该接口用于下载文件,请求的userid需有下载权限。 + *
+ * 请求方式:POST(HTTPS) + * 请求地址: https://qyapi.weixin.qq.com/cgi-bin/wedrive/file_download?access_token=ACCESS_TOKEN + * + * @param userId + * @param fileId + * @return + * @throws WxErrorException + */ + WxCpFileDownload fileDownload(@NonNull String userId, @NonNull String fileId) throws WxErrorException; + + /** + * 重命名文件 + * 该接口用于对指定文件进行重命名。 + *
+ * 请求方式:POST(HTTPS) + * 请求地址: https://qyapi.weixin.qq.com/cgi-bin/wedrive/file_rename?access_token=ACCESS_TOKEN + * + * @param userId + * @param fileId + * @param newName + * @return + * @throws WxErrorException + */ + WxCpFileRename fileRename(@NonNull String userId, @NonNull String fileId, @NonNull String newName) throws WxErrorException; + + /** + * 新建文件/微文档 + * 该接口用于在微盘指定位置新建文件、微文档。 + *
+ * 请求方式:POST(HTTPS) + * 请求地址: https://qyapi.weixin.qq.com/cgi-bin/wedrive/file_create?access_token=ACCESS_TOKEN + * + * @param userId 操作者userid + * @param spaceId 空间spaceid + * @param fatherId 父目录fileid, 在根目录时为空间spaceid + * @param fileType 文件类型, 1:文件夹 3:微文档(文档) 4:微文档(表格) + * @param fileName 文件名字 + * @return + * @throws WxErrorException + */ + WxCpFileCreate fileCreate(@NonNull String userId, @NonNull String spaceId, + @NonNull String fatherId, @NonNull Integer fileType, @NonNull String fileName) throws WxErrorException; + } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpOaWeDriveServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpOaWeDriveServiceImpl.java index 188ba2bb6e..771e5c00c7 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpOaWeDriveServiceImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpOaWeDriveServiceImpl.java @@ -102,4 +102,38 @@ public WxCpFileUpload fileUpload(@NonNull WxCpFileUploadRequest request) throws return WxCpFileUpload.fromJson(responseContent); } + @Override + public WxCpFileDownload fileDownload(@NonNull String userId, @NonNull String fileId) throws WxErrorException { + String apiUrl = this.cpService.getWxCpConfigStorage().getApiUrl(FILE_DOWNLOAD); + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("userid", userId); + jsonObject.addProperty("fileid", fileId); + String responseContent = this.cpService.post(apiUrl, jsonObject.toString()); + return WxCpFileDownload.fromJson(responseContent); + } + + @Override + public WxCpFileRename fileRename(@NonNull String userId, @NonNull String fileId, @NonNull String newName) throws WxErrorException { + String apiUrl = this.cpService.getWxCpConfigStorage().getApiUrl(FILE_RENAME); + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("userid", userId); + jsonObject.addProperty("fileiid", fileId); + jsonObject.addProperty("new_name", newName); + String responseContent = this.cpService.post(apiUrl, jsonObject.toString()); + return WxCpFileRename.fromJson(responseContent); + } + + @Override + public WxCpFileCreate fileCreate(@NonNull String userId, @NonNull String spaceId, @NonNull String fatherId, @NonNull Integer fileType, @NonNull String fileName) throws WxErrorException { + String apiUrl = this.cpService.getWxCpConfigStorage().getApiUrl(FILE_CREATE); + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("userid", userId); + jsonObject.addProperty("spaceid", spaceId); + jsonObject.addProperty("fatherid", fatherId); + jsonObject.addProperty("file_type", fileType); + jsonObject.addProperty("file_name", fileName); + String responseContent = this.cpService.post(apiUrl, jsonObject.toString()); + return WxCpFileCreate.fromJson(responseContent); + } + } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/wedrive/WxCpFileCreate.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/wedrive/WxCpFileCreate.java new file mode 100644 index 0000000000..6d7ba3e6fe --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/wedrive/WxCpFileCreate.java @@ -0,0 +1,33 @@ +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 WxCpFileCreate extends WxCpBaseResp implements Serializable { + private static final long serialVersionUID = -5028321625142879581L; + + @SerializedName("fileid") + private String fileId; + + @SerializedName("url") + private String url; + + public static WxCpFileCreate fromJson(String json) { + return WxCpGsonBuilder.create().fromJson(json, WxCpFileCreate.class); + } + + public String toJson() { + return WxCpGsonBuilder.create().toJson(this); + } + +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/wedrive/WxCpFileDownload.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/wedrive/WxCpFileDownload.java new file mode 100644 index 0000000000..638d26e488 --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/wedrive/WxCpFileDownload.java @@ -0,0 +1,36 @@ +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 WxCpFileDownload extends WxCpBaseResp implements Serializable { + private static final long serialVersionUID = -5028321625142879581L; + + @SerializedName("download_url") + private String downloadUrl; + + @SerializedName("cookie_name") + private String cookieName; + + @SerializedName("cookie_value") + private String cookieValue; + + public static WxCpFileDownload fromJson(String json) { + return WxCpGsonBuilder.create().fromJson(json, WxCpFileDownload.class); + } + + public String toJson() { + return WxCpGsonBuilder.create().toJson(this); + } + +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/wedrive/WxCpFileRename.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/wedrive/WxCpFileRename.java new file mode 100644 index 0000000000..26fe21b198 --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/wedrive/WxCpFileRename.java @@ -0,0 +1,89 @@ +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; + +/** + * 下载文件返回信息. + * + * @author Wang_Wong + */ +@Data +public class WxCpFileRename extends WxCpBaseResp implements Serializable { + private static final long serialVersionUID = -5028321625142879581L; + + @SerializedName("file") + private File file; + + @Getter + @Setter + public static class File 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 File fromJson(String json) { + return WxCpGsonBuilder.create().fromJson(json, File.class); + } + + public String toJson() { + return WxCpGsonBuilder.create().toJson(this); + } + + } + + public static WxCpFileRename fromJson(String json) { + return WxCpGsonBuilder.create().fromJson(json, WxCpFileRename.class); + } + + public String toJson() { + return WxCpGsonBuilder.create().toJson(this); + } + +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java index 8eae00d57d..05af6660c5 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java @@ -155,6 +155,9 @@ interface Oa { String SPACE_SHARE = "/cgi-bin/wedrive/space_share"; String FILE_LIST = "/cgi-bin/wedrive/file_list"; String FILE_UPLOAD = "/cgi-bin/wedrive/file_upload"; + String FILE_DOWNLOAD = "/cgi-bin/wedrive/file_download"; + String FILE_RENAME = "/cgi-bin/wedrive/file_rename"; + String FILE_CREATE = "/cgi-bin/wedrive/file_create"; /** * 审批流程引擎 diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpOaWeDriveServiceTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpOaWeDriveServiceTest.java index e51ef106c9..5d0016ae83 100644 --- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpOaWeDriveServiceTest.java +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpOaWeDriveServiceTest.java @@ -45,6 +45,19 @@ public void test() throws Exception { String spId = "s.ww45d3e188865aca30.652091685u4h"; // 空间的文件id String fileId = "s.ww45d3e188865aca30.652091685u4h_f.652344507ysDL"; + String fileId2 = "s.ww45d3e188865aca30.652091685u4h_f.652696024TU4P"; + + /** + * 新建文件/微文档 + */ + WxCpFileCreate fileCreate = cpService.getOaWeDriveService().fileCreate(uId, spId, spId, 3, "新建微文档1"); + log.info("新建文件/微文档:{}", fileCreate.toJson()); + + /** + * 下载文件 + */ + WxCpFileDownload fileDownload = cpService.getOaWeDriveService().fileDownload(uId, fileId); + log.info("下载文件为:{}", fileDownload.toJson()); /** * 上传文件 @@ -57,6 +70,7 @@ public void test() throws Exception { // 将文件转成base64字符串 File file = new File("D:/info.log.2022-05-07.0.log"); +// File file = new File("D:/16.png"); FileInputStream inputFile = new FileInputStream(file); byte[] buffer = new byte[(int)file.length()]; inputFile.read(buffer); @@ -67,6 +81,12 @@ public void test() throws Exception { WxCpFileUpload fileUpload = cpService.getOaWeDriveService().fileUpload(fileUploadRequest); log.info("上传文件为:{}", fileUpload.toJson()); + /** + * 重命名文件 + */ + WxCpFileRename fileRename = cpService.getOaWeDriveService().fileRename(uId, fileUpload.getFileId(), "新的名字呢"); + log.info("重命名文件:{}", fileRename.toJson()); + /** * 获取文件列表 */