Skip to content

Commit 19c3113

Browse files
authored
🆕 #2648 【企业微信】增加微盘文件管理部分接口
1 parent b353067 commit 19c3113

7 files changed

Lines changed: 262 additions & 0 deletions

File tree

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOaWeDriveService.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,51 @@ public interface WxCpOaWeDriveService {
147147
*/
148148
WxCpFileUpload fileUpload(@NonNull WxCpFileUploadRequest request) throws WxErrorException;
149149

150+
/**
151+
* 下载文件
152+
* 该接口用于下载文件,请求的userid需有下载权限。
153+
* <p>
154+
* 请求方式:POST(HTTPS)
155+
* 请求地址: https://qyapi.weixin.qq.com/cgi-bin/wedrive/file_download?access_token=ACCESS_TOKEN
156+
*
157+
* @param userId
158+
* @param fileId
159+
* @return
160+
* @throws WxErrorException
161+
*/
162+
WxCpFileDownload fileDownload(@NonNull String userId, @NonNull String fileId) throws WxErrorException;
163+
164+
/**
165+
* 重命名文件
166+
* 该接口用于对指定文件进行重命名。
167+
* <p>
168+
* 请求方式:POST(HTTPS)
169+
* 请求地址: https://qyapi.weixin.qq.com/cgi-bin/wedrive/file_rename?access_token=ACCESS_TOKEN
170+
*
171+
* @param userId
172+
* @param fileId
173+
* @param newName
174+
* @return
175+
* @throws WxErrorException
176+
*/
177+
WxCpFileRename fileRename(@NonNull String userId, @NonNull String fileId, @NonNull String newName) throws WxErrorException;
178+
179+
/**
180+
* 新建文件/微文档
181+
* 该接口用于在微盘指定位置新建文件、微文档。
182+
* <p>
183+
* 请求方式:POST(HTTPS)
184+
* 请求地址: https://qyapi.weixin.qq.com/cgi-bin/wedrive/file_create?access_token=ACCESS_TOKEN
185+
*
186+
* @param userId 操作者userid
187+
* @param spaceId 空间spaceid
188+
* @param fatherId 父目录fileid, 在根目录时为空间spaceid
189+
* @param fileType 文件类型, 1:文件夹 3:微文档(文档) 4:微文档(表格)
190+
* @param fileName 文件名字
191+
* @return
192+
* @throws WxErrorException
193+
*/
194+
WxCpFileCreate fileCreate(@NonNull String userId, @NonNull String spaceId,
195+
@NonNull String fatherId, @NonNull Integer fileType, @NonNull String fileName) throws WxErrorException;
196+
150197
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpOaWeDriveServiceImpl.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,38 @@ public WxCpFileUpload fileUpload(@NonNull WxCpFileUploadRequest request) throws
102102
return WxCpFileUpload.fromJson(responseContent);
103103
}
104104

105+
@Override
106+
public WxCpFileDownload fileDownload(@NonNull String userId, @NonNull String fileId) throws WxErrorException {
107+
String apiUrl = this.cpService.getWxCpConfigStorage().getApiUrl(FILE_DOWNLOAD);
108+
JsonObject jsonObject = new JsonObject();
109+
jsonObject.addProperty("userid", userId);
110+
jsonObject.addProperty("fileid", fileId);
111+
String responseContent = this.cpService.post(apiUrl, jsonObject.toString());
112+
return WxCpFileDownload.fromJson(responseContent);
113+
}
114+
115+
@Override
116+
public WxCpFileRename fileRename(@NonNull String userId, @NonNull String fileId, @NonNull String newName) throws WxErrorException {
117+
String apiUrl = this.cpService.getWxCpConfigStorage().getApiUrl(FILE_RENAME);
118+
JsonObject jsonObject = new JsonObject();
119+
jsonObject.addProperty("userid", userId);
120+
jsonObject.addProperty("fileiid", fileId);
121+
jsonObject.addProperty("new_name", newName);
122+
String responseContent = this.cpService.post(apiUrl, jsonObject.toString());
123+
return WxCpFileRename.fromJson(responseContent);
124+
}
125+
126+
@Override
127+
public WxCpFileCreate fileCreate(@NonNull String userId, @NonNull String spaceId, @NonNull String fatherId, @NonNull Integer fileType, @NonNull String fileName) throws WxErrorException {
128+
String apiUrl = this.cpService.getWxCpConfigStorage().getApiUrl(FILE_CREATE);
129+
JsonObject jsonObject = new JsonObject();
130+
jsonObject.addProperty("userid", userId);
131+
jsonObject.addProperty("spaceid", spaceId);
132+
jsonObject.addProperty("fatherid", fatherId);
133+
jsonObject.addProperty("file_type", fileType);
134+
jsonObject.addProperty("file_name", fileName);
135+
String responseContent = this.cpService.post(apiUrl, jsonObject.toString());
136+
return WxCpFileCreate.fromJson(responseContent);
137+
}
138+
105139
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package me.chanjar.weixin.cp.bean.oa.wedrive;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
6+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
7+
8+
import java.io.Serializable;
9+
10+
/**
11+
* 新建文件/微文档 返回信息.
12+
*
13+
* @author Wang_Wong
14+
*/
15+
@Data
16+
public class WxCpFileCreate extends WxCpBaseResp implements Serializable {
17+
private static final long serialVersionUID = -5028321625142879581L;
18+
19+
@SerializedName("fileid")
20+
private String fileId;
21+
22+
@SerializedName("url")
23+
private String url;
24+
25+
public static WxCpFileCreate fromJson(String json) {
26+
return WxCpGsonBuilder.create().fromJson(json, WxCpFileCreate.class);
27+
}
28+
29+
public String toJson() {
30+
return WxCpGsonBuilder.create().toJson(this);
31+
}
32+
33+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package me.chanjar.weixin.cp.bean.oa.wedrive;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
6+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
7+
8+
import java.io.Serializable;
9+
10+
/**
11+
* 下载文件返回信息.
12+
*
13+
* @author Wang_Wong
14+
*/
15+
@Data
16+
public class WxCpFileDownload extends WxCpBaseResp implements Serializable {
17+
private static final long serialVersionUID = -5028321625142879581L;
18+
19+
@SerializedName("download_url")
20+
private String downloadUrl;
21+
22+
@SerializedName("cookie_name")
23+
private String cookieName;
24+
25+
@SerializedName("cookie_value")
26+
private String cookieValue;
27+
28+
public static WxCpFileDownload fromJson(String json) {
29+
return WxCpGsonBuilder.create().fromJson(json, WxCpFileDownload.class);
30+
}
31+
32+
public String toJson() {
33+
return WxCpGsonBuilder.create().toJson(this);
34+
}
35+
36+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package me.chanjar.weixin.cp.bean.oa.wedrive;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
import lombok.Getter;
6+
import lombok.Setter;
7+
import me.chanjar.weixin.cp.bean.WxCpBaseResp;
8+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
9+
10+
import java.io.Serializable;
11+
12+
/**
13+
* 下载文件返回信息.
14+
*
15+
* @author Wang_Wong
16+
*/
17+
@Data
18+
public class WxCpFileRename extends WxCpBaseResp implements Serializable {
19+
private static final long serialVersionUID = -5028321625142879581L;
20+
21+
@SerializedName("file")
22+
private File file;
23+
24+
@Getter
25+
@Setter
26+
public static class File implements Serializable {
27+
private static final long serialVersionUID = -4960239393895754598L;
28+
29+
@SerializedName("fileid")
30+
private String fileId;
31+
32+
@SerializedName("file_name")
33+
private String fileName;
34+
35+
@SerializedName("spaceid")
36+
private String spaceId;
37+
38+
@SerializedName("fatherid")
39+
private String fatherId;
40+
41+
@SerializedName("file_size")
42+
private Long fileSize;
43+
44+
@SerializedName("ctime")
45+
private Long cTime;
46+
47+
@SerializedName("mtime")
48+
private Long mTime;
49+
50+
@SerializedName("file_type")
51+
private Integer fileType;
52+
53+
@SerializedName("file_status")
54+
private Integer fileStatus;
55+
56+
@SerializedName("create_userid")
57+
private String createUserId;
58+
59+
@SerializedName("update_userid")
60+
private String updateUserId;
61+
62+
@SerializedName("sha")
63+
private String sha;
64+
65+
@SerializedName("url")
66+
private String url;
67+
68+
@SerializedName("md5")
69+
private String md5;
70+
71+
public static File fromJson(String json) {
72+
return WxCpGsonBuilder.create().fromJson(json, File.class);
73+
}
74+
75+
public String toJson() {
76+
return WxCpGsonBuilder.create().toJson(this);
77+
}
78+
79+
}
80+
81+
public static WxCpFileRename fromJson(String json) {
82+
return WxCpGsonBuilder.create().fromJson(json, WxCpFileRename.class);
83+
}
84+
85+
public String toJson() {
86+
return WxCpGsonBuilder.create().toJson(this);
87+
}
88+
89+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ interface Oa {
155155
String SPACE_SHARE = "/cgi-bin/wedrive/space_share";
156156
String FILE_LIST = "/cgi-bin/wedrive/file_list";
157157
String FILE_UPLOAD = "/cgi-bin/wedrive/file_upload";
158+
String FILE_DOWNLOAD = "/cgi-bin/wedrive/file_download";
159+
String FILE_RENAME = "/cgi-bin/wedrive/file_rename";
160+
String FILE_CREATE = "/cgi-bin/wedrive/file_create";
158161

159162
/**
160163
* 审批流程引擎

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/WxCpOaWeDriveServiceTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ public void test() throws Exception {
4545
String spId = "s.ww45d3e188865aca30.652091685u4h";
4646
// 空间的文件id
4747
String fileId = "s.ww45d3e188865aca30.652091685u4h_f.652344507ysDL";
48+
String fileId2 = "s.ww45d3e188865aca30.652091685u4h_f.652696024TU4P";
49+
50+
/**
51+
* 新建文件/微文档
52+
*/
53+
WxCpFileCreate fileCreate = cpService.getOaWeDriveService().fileCreate(uId, spId, spId, 3, "新建微文档1");
54+
log.info("新建文件/微文档:{}", fileCreate.toJson());
55+
56+
/**
57+
* 下载文件
58+
*/
59+
WxCpFileDownload fileDownload = cpService.getOaWeDriveService().fileDownload(uId, fileId);
60+
log.info("下载文件为:{}", fileDownload.toJson());
4861

4962
/**
5063
* 上传文件
@@ -57,6 +70,7 @@ public void test() throws Exception {
5770

5871
// 将文件转成base64字符串
5972
File file = new File("D:/info.log.2022-05-07.0.log");
73+
// File file = new File("D:/16.png");
6074
FileInputStream inputFile = new FileInputStream(file);
6175
byte[] buffer = new byte[(int)file.length()];
6276
inputFile.read(buffer);
@@ -67,6 +81,12 @@ public void test() throws Exception {
6781
WxCpFileUpload fileUpload = cpService.getOaWeDriveService().fileUpload(fileUploadRequest);
6882
log.info("上传文件为:{}", fileUpload.toJson());
6983

84+
/**
85+
* 重命名文件
86+
*/
87+
WxCpFileRename fileRename = cpService.getOaWeDriveService().fileRename(uId, fileUpload.getFileId(), "新的名字呢");
88+
log.info("重命名文件:{}", fileRename.toJson());
89+
7090
/**
7191
* 获取文件列表
7292
*/

0 commit comments

Comments
 (0)