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

【企业微信】增加微盘文件管理部分接口 #2648

Merged
merged 1 commit into from
May 17, 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 @@ -147,4 +147,51 @@ public interface WxCpOaWeDriveService {
*/
WxCpFileUpload fileUpload(@NonNull WxCpFileUploadRequest request) throws WxErrorException;

/**
* 下载文件
* 该接口用于下载文件,请求的userid需有下载权限。
* <p>
* 请求方式: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;

/**
* 重命名文件
* 该接口用于对指定文件进行重命名。
* <p>
* 请求方式: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;

/**
* 新建文件/微文档
* 该接口用于在微盘指定位置新建文件、微文档。
* <p>
* 请求方式: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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
* 审批流程引擎
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

/**
* 上传文件
Expand All @@ -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);
Expand All @@ -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());

/**
* 获取文件列表
*/
Expand Down