微信小程序 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 一致。真机调试通常不校验,不能作为最终验收依据。

相关推荐
djxzyr18 小时前
微信小程序传感器实战:罗盘、加速计、陀螺仪与加速度实时波形
微信小程序·小程序
码间留白2 天前
AI 给的两个方案都被我否了:把 startIcon 与启动遮罩做成同一张图,鸿蒙 APP 冷启动零白屏
性能优化·uni-app·ai编程·harmonyos
anyup2 天前
自带 uni-app x 页面级能力,uView Pro X 8 个开箱即用的工具函数
前端·vue.js·uni-app
李纲明3 天前
微信小程序开了 Skyline 后样式全乱、滚动对不上甚至部分组件不能用?Skyline 怎么接才稳
javascript·微信小程序·小程序·devdiy
wx1qq1com5 天前
一桩带几台车:共享租赁桩位配比、取电与结算闭环设计
物联网·微信小程序·小程序
2401_879692515 天前
微信小程序照相与摄像功能实现——chooseImage 与 chooseVideo 详解
微信小程序·小程序·notepad++
李纲明7 天前
微信小程序 getSystemInfo 提示已废弃、屏幕高度算错胶囊还对不齐?getWindowInfo 和 getDeviceInfo 怎么接才稳
javascript·微信小程序·小程序·小程序开发·devdiy
dreams_dream7 天前
微信小程序-使用npm包
微信小程序·小程序·npm
码间留白7 天前
六轮真机实验后,我把鸿蒙直播推流的 UI 全逼成了原生——uni-app 适配 HarmonyOS NEXT 完整复盘
uni-app·鸿蒙·harmonyos next·直播推流·arkts/arkui
caoliaoerweima7 天前
草料二维码小程序上线活码编辑器:手机上也能编辑活码了
智能手机·小程序·编辑器·草料二维码