init error: AppInfoNotExists won't start session
GET https://o.wpsgo.com/api/v3/office/file/28/multiwatermark 403 (Forbidden)
TypeError: Cannot read properties of undefined (reading 'officeType')
WPS SDK 必须依赖后端接口才能工作!
WPS SDK 的工作流程:
1. 前端调用 WebOfficeSDK.init()
↓
2. WPS SDK 向 WPS 服务器发送请求
↓
3. WPS 服务器回调你的后端接口获取文件信息
↓
4. 如果后端接口不存在 → 返回 AppInfoNotExists 错误
↓
5. 如果后端接口存在 → 返回文件信息 → 显示编辑器
结论:没有后端接口,WPS SDK 无法工作!这不是前端的问题。
实现文件信息接口(必需)
GET /v1/3rd/file/info
在 WPS 控制台配置回调地址
https://你的域名/v1/3rd/file/info@RestController
@RequestMapping("/v1/3rd/file")
public class WpsFileController {
@Autowired
private DocumentService documentService;
@GetMapping("/info")
public Map<String, Object> getFileInfo(
@RequestParam("_w_appid") String appId,
@RequestParam("_w_fileid") String fileId
) {
// 1. 验证 appId
if (!"SX20251229FLIAPDAPP".equals(appId)) {
throw new RuntimeException("Invalid appId");
}
// 2. 查询文档
Document doc = documentService.getById(Long.parseLong(fileId));
if (doc == null) {
throw new RuntimeException("File not found");
}
// 3. 构建响应
Map<String, Object> file = new HashMap<>();
file.put("id", doc.getId().toString());
file.put("name", doc.getFileName());
file.put("version", 1);
file.put("size", doc.getFileSize() != null ? doc.getFileSize() : 0);
file.put("download_url", doc.getUrl());
Map<String, Object> creator = new HashMap<>();
creator.put("id", "user_1");
creator.put("name", "系统用户");
file.put("creator", creator);
file.put("create_time", System.currentTimeMillis() / 1000);
file.put("modify_time", System.currentTimeMillis() / 1000);
Map<String, Integer> acl = new HashMap<>();
acl.put("rename", 1);
acl.put("history", 1);
acl.put("copy", 1);
acl.put("export", 1);
acl.put("print", 1);
acl.put("read", 1);
acl.put("update", 1); // 1=可编辑,0=只读
acl.put("comment", 1);
file.put("user_acl", acl);
Map<String, Object> data = new HashMap<>();
data.put("file", file);
Map<String, Object> response = new HashMap<>();
response.put("code", 0);
response.put("msg", "success");
response.put("data", data);
return response;
}
}
什么都不需要做! 前端已经实现了降级方案。
当 WPS SDK 初始化失败时,会自动切换到 iframe 预览模式。
可以简化代码,只使用 iframe:
<template>
<div class="preview-container">
<iframe
v-if="document?.url"
:src="document.url"
frameborder="0"
style="width: 100%; height: 100%;"
></iframe>
</div>
</template>
推荐:使用 iframe 预览
推荐:实现后端接口
推荐:下载编辑方案
查看:.kiro/specs/backend-api-requirements.md
包含:
查看:.kiro/specs/simple-document-viewer-plan.md
包含:
是 → 实现后端接口(选择 1)
否 → 使用 iframe 预览(选择 2)
1-2 天内 → 等待后端实现,使用 WPS SDK
超过 2 天 → 先用 iframe 预览,后续再升级
是 → 必须使用 WPS SDK + 后端接口
否 → iframe 预览就够了
curl "http://localhost:8080/v1/3rd/file/info?_w_appid=SX20251229FLIAPDAPP&_w_fileid=28"当前错误是正常的! 因为后端接口还没实现。
你需要决定:
告诉我你的选择,我会帮你完成相应的实现!