Skip to content

openDocument

openDocumentByUrl 下载并打开在线文档,全程带 loading 与失败提示。

功能描述

把「下载 → 打开」两步 uni API 串成一个 Promise,并统一处理三类失败:地址为空、下载失败、打开失败。 提示文案可覆盖,传空字符串即关闭对应提示。

函数签名

typescript
function openDocumentByUrl(
  url: string,
  options?: OpenDocumentOptions,
): Promise<string>;

参数

参数类型必填说明
urlstring文档地址
optionsOpenDocumentOptions配置项

OpenDocumentOptions

参数类型必填默认值说明
messagesOpenDocumentMessages见下覆盖提示文案
showMenubooleantrue打开后是否显示右上角菜单

OpenDocumentMessages

字段默认值说明
emptyUrl文档地址不存在地址为空时的提示
loading文档加载中...下载中的 loading 文案
downloadFailed文档下载失败下载失败的提示
openFailed文档打开失败打开失败的提示

返回值

Promise<string>,成功时 resolve 下载到本地的临时文件路径;上述三类失败均 reject。

使用方法

typescript
import { openDocumentByUrl } from "uni-toolkit/tools";

try {
  const filePath = await openDocumentByUrl("https://example.com/contract.pdf");
  console.log("已打开", filePath);
} catch (error) {
  console.warn("打开文档失败", error);
}

自定义文案(例如做多语言):

typescript
await openDocumentByUrl(url, {
  messages: {
    loading: "Loading...",
    downloadFailed: "Download failed",
    openFailed: "Cannot open this file",
  },
});

关掉 loading 只保留失败提示:

typescript
await openDocumentByUrl(url, { messages: { loading: "" } });

注意事项

  • 各端对可打开的文档格式支持不同,PDF 兼容性最好;doc / xls 等在部分端会打开失败
  • 下载走的是 uni.downloadFile,小程序端需要把域名加入下载白名单
  • 失败时内部已 hideLoading 并提示,调用方只需处理业务分支,但仍建议 catch 以免未处理拒绝

基于 MIT 许可发布