微信小程序 PDF 预览实践(uni.downloadFile → uni.openDocument)

微信小程序 PDF 预览实践

小程序无法直接打开远程 PDF,需先下载至本地临时文件,再调用系统文档能力打开。核心 API:uni.downloadFile → uni.openDocument。


接口数据约定

GET /wx/recycle/resourceOrder/contractTemplate 返回示例:

json 复制代码
{
  "code": 200,
  "msg": "操作成功",
  "data": {
    "templateName": "资源交易合同模板",
    "fileName": "交易合同.pdf",
    "fileUrl": "https://zszy.weerspace.com:9220/recyclebucket/xxx.pdf",
    "fileType": "pdf",
    "suffix": "pdf"
  }
}

fileUrl 须为可访问的完整 HTTPS 地址。


调用时机(什么时候执行)

时机 做什么
页面 onLoad 请求合同模板接口,用 applyTemplate 把 res.data 写入 contractTemplate
用户点击「预览」 调用 previewTemplateFile(contractTemplate),用其中的 fileUrl 下载并打开

页面结构示例:

html 复制代码
<!-- 有 fileUrl 时展示文件行;点击整行触发预览 -->
<view
  v-if="contractTemplate.fileUrl"
  class="file-item"
  @tap="previewTemplateFile(contractTemplate)"
>
  <text>{{ contractTemplate.fileName || '合同模板' }}</text>
  <text>预览</text>
</view>

数据准备:

js 复制代码
const contractTemplate = reactive({
  templateName: '',
  fileName: '',
  fileUrl: '',
  fileType: '',
  suffix: '',
})

const applyTemplate = (target, data = {}) => {
  Object.assign(target, {
    templateName: data.templateName || '',
    fileName: data.fileName || '',
    fileUrl: data.fileUrl || '',
    fileType: data.fileType || '',
    suffix: data.suffix || '',
  })
}

// onLoad 时调用
const fetchContractTemplate = async () => {
  const res = await resourceOrderApi.contractTemplate()
  if (res?.code === 200 || res?.code === 0) {
    applyTemplate(contractTemplate, res.data || {})
  }
}

流程:

复制代码
onLoad → 接口灌入 contractTemplate
用户点击「预览」→ previewTemplateFile(contractTemplate)
→ downloadFile → openDocument

核心实现

js 复制代码
const fileBusy = ref(false) // 防连点:下载中再次点击直接 return

/**
 * 预览远程文档(PDF 等)
 * @param {{ fileUrl: string }} template 一般为页面上的 contractTemplate
 */
const previewTemplateFile = (template) => {
  if (fileBusy.value) return

  const filePath = template?.fileUrl
  if (!filePath) {
    uni.showToast({ title: '暂无可预览文件', icon: 'none' })
    return
  }

  fileBusy.value = true
  uni.showLoading({ title: '打开中', mask: true })

  uni.downloadFile({
    url: filePath,
    success: (res) => {
      uni.hideLoading()
      fileBusy.value = false

      if (res.statusCode !== 200 || !res.tempFilePath) {
        uni.showToast({ title: '预览失败', icon: 'none' })
        return
      }

      uni.openDocument({
        filePath: res.tempFilePath,
        showMenu: true,
        fail: () => {
          uni.showToast({ title: '打开文件失败', icon: 'none' })
        },
      })
    },
    complete: () => {
      uni.hideLoading()
      fileBusy.value = false
    },
    fail: () => {
      uni.hideLoading()
      fileBusy.value = false
      uni.showToast({ title: '预览失败', icon: 'none' })
    },
  })
}

上线注意

预览码 / 体验版 / 正式版会校验 downloadFile 合法域名 ,端口须与 fileUrl 一致。真机调试通常不校验,不能作为最终验收依据。

相关推荐
考拉讲支付10 小时前
延迟分账 / 核销分账的技术实现方案:服务完成后再分钱,到底怎么设计?
大数据·运维·人工智能·微信小程序·小程序·系统架构
Newway32114 小时前
便利店小程序商城如何打通总部、门店与会员
小程序·小程序商城·会员运营·连锁便利店·门店订货·o2o系统
Strawberry9614 小时前
小程序视频播放官方方案
小程序
weixin_1772972206917 小时前
二手旧物回收小程序先要解决的不是下单,是“上门之后价格变了怎么办“
小程序·apache
青企通17 小时前
电动车门店小程序:以旧换新估价、维修工单与电池档案的设计实现
小程序
努力变的更好18 小时前
H5与小程序之间的回调
前端·小程序·apache
镭封1 天前
自媒体内容生产怎么提速?文案整理、配音与剪辑流程
人工智能·小程序·音视频·语音识别·媒体
anyup1 天前
uView Pro X 1.0.0 发布:全面支持 uni-app x 蒸汽模式,80+ 组件
前端·uni-app·ai编程
科威网络1 天前
小程序开发性能优化:分包、预加载与启动时间
性能优化·微信小程序·小程序
zhongben123_2 天前
从零开发一个打车APP:技术选型、系统架构与成本评估指南
大数据·运维·ios·系统架构·uni-app·web app