1
This commit is contained in:
@@ -54,35 +54,85 @@ public class WPS365ApiServiceImpl implements IWPS365ApiService {
|
||||
|
||||
@Override
|
||||
public JSONObject getFileList(String accessToken, Map<String, Object> params) {
|
||||
// 官方文档:云文档文件在「驱动盘」下,路径为 GET https://openapi.wps.cn/v7/drives/{drive_id}/files
|
||||
// /api/v1/yundoc/files 会 404,需先取 drive_id 再请求 v7/drives/{drive_id}/files
|
||||
String baseV7 = "https://openapi.wps.cn/v7";
|
||||
int page = params != null && params.get("page") != null ? ((Number) params.get("page")).intValue() : 1;
|
||||
int pageSize = params != null && params.get("page_size") != null ? ((Number) params.get("page_size")).intValue() : 20;
|
||||
if (params != null && params.get("pageSize") != null) {
|
||||
pageSize = ((Number) params.get("pageSize")).intValue();
|
||||
}
|
||||
|
||||
String driveId = null;
|
||||
// 1) 尝试获取驱动盘列表:GET /v7/drives
|
||||
try {
|
||||
// WPS365文件列表API路径可能是 /yundoc/files 而不是 /files
|
||||
// 根据WPS365 API文档,文件相关API通常在 /yundoc 路径下
|
||||
StringBuilder url = new StringBuilder(wps365Config.getApiBaseUrl() + "/yundoc/files");
|
||||
|
||||
// 添加查询参数
|
||||
if (params != null && !params.isEmpty()) {
|
||||
url.append("?");
|
||||
boolean first = true;
|
||||
for (Map.Entry<String, Object> entry : params.entrySet()) {
|
||||
if (!first) {
|
||||
url.append("&");
|
||||
JSONObject drivesRes = WPS365ApiUtil.httpRequest("GET", baseV7 + "/drives", accessToken, null);
|
||||
if (drivesRes != null) {
|
||||
JSONArray items = drivesRes.getJSONArray("items");
|
||||
if (items == null) {
|
||||
items = drivesRes.getJSONArray("drives");
|
||||
}
|
||||
if (items == null && drivesRes.get("data") != null) {
|
||||
Object data = drivesRes.get("data");
|
||||
if (data instanceof JSONArray) {
|
||||
items = (JSONArray) data;
|
||||
} else if (data instanceof JSONObject) {
|
||||
JSONObject dataObj = (JSONObject) data;
|
||||
items = dataObj.getJSONArray("items");
|
||||
if (items == null) {
|
||||
items = dataObj.getJSONArray("drives");
|
||||
}
|
||||
}
|
||||
// URL编码参数值
|
||||
try {
|
||||
String key = entry.getKey();
|
||||
String value = entry.getValue() != null ? entry.getValue().toString() : "";
|
||||
url.append(key).append("=").append(java.net.URLEncoder.encode(value, "UTF-8"));
|
||||
} catch (java.io.UnsupportedEncodingException e) {
|
||||
url.append(entry.getKey()).append("=").append(entry.getValue());
|
||||
}
|
||||
if (items != null && !items.isEmpty()) {
|
||||
for (int i = 0; i < items.size(); i++) {
|
||||
JSONObject item = items.getJSONObject(i);
|
||||
String id = item.getString("id");
|
||||
if (id != null && !id.isEmpty()) {
|
||||
// 优先个人盘 allotee_type=user(我的云文档)
|
||||
if ("user".equalsIgnoreCase(item.getString("allotee_type"))) {
|
||||
driveId = id;
|
||||
break;
|
||||
}
|
||||
if (driveId == null) {
|
||||
driveId = id;
|
||||
}
|
||||
}
|
||||
}
|
||||
first = false;
|
||||
}
|
||||
}
|
||||
|
||||
log.debug("调用文件列表API: {}", url.toString());
|
||||
return WPS365ApiUtil.httpRequest("GET", url.toString(), accessToken, null);
|
||||
} catch (Exception e) {
|
||||
log.error("获取文件列表失败 - url: {}", wps365Config.getApiBaseUrl() + "/yundoc/files", e);
|
||||
log.debug("获取驱动盘列表失败,尝试其他方式: {}", e.getMessage());
|
||||
}
|
||||
|
||||
// 2) 若没有 drive_id,尝试「当前用户默认盘」常见路径
|
||||
if (driveId == null) {
|
||||
try {
|
||||
JSONObject meRes = WPS365ApiUtil.httpRequest("GET", baseV7 + "/drives/me", accessToken, null);
|
||||
if (meRes != null && meRes.getString("id") != null) {
|
||||
driveId = meRes.getString("id");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.debug("v7/drives/me 失败: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if (driveId == null) {
|
||||
throw new RuntimeException("获取文件列表失败:无法获取驱动盘ID(drive_id)。请确认已授权且 WPS 开放平台云文档接口可用,参见 open.wps.cn 云文档业务域文档。");
|
||||
}
|
||||
|
||||
// 3) 获取该盘下的文件列表
|
||||
String filesUrl = baseV7 + "/drives/" + driveId + "/files?page=" + page + "&page_size=" + pageSize;
|
||||
try {
|
||||
log.debug("调用文件列表API: {}", filesUrl);
|
||||
JSONObject result = WPS365ApiUtil.httpRequest("GET", filesUrl, accessToken, null);
|
||||
// 前端期望 data.files;v7 可能返回 items,统一成 files
|
||||
if (result != null && result.get("files") == null && result.getJSONArray("items") != null) {
|
||||
result.put("files", result.getJSONArray("items"));
|
||||
}
|
||||
return result != null ? result : new JSONObject();
|
||||
} catch (Exception e) {
|
||||
log.error("获取文件列表失败 - url: {}", filesUrl, e);
|
||||
throw new RuntimeException("获取文件列表失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user