Bläddra i källkod

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

0katekate0 3 år sedan
förälder
incheckning
66383836b8

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

@@ -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;
+
 }

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

@@ -88,4 +88,18 @@ public class WxCpOaWeDriveServiceImpl implements WxCpOaWeDriveService {
     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);
+  }
+
 }

+ 114 - 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/wedrive/WxCpFileList.java

@@ -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);
+  }
+
+}

+ 50 - 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/wedrive/WxCpFileListRequest.java

@@ -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);
+  }
+
+}

+ 30 - 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/wedrive/WxCpFileUpload.java

@@ -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);
+  }
+
+}

+ 47 - 0
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/oa/wedrive/WxCpFileUploadRequest.java

@@ -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);
+  }
+
+}

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

@@ -153,6 +153,8 @@ public interface WxCpApiPathConsts {
     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";
 
     /**
      * 审批流程引擎

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

@@ -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;
@@ -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);
@@ -40,6 +43,43 @@ public class WxCpOaWeDriveServiceTest {
 
     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());
 
     /**
      * 权限管理