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

相关推荐
weixin_4222013011 小时前
如何解决小程序图标点击热区小,落点不准问题?
前端·小程序·样式·点击热区·扩大
河北清兮网络科技13 小时前
直播APP商用开发深度解析:为什么模板系统无法支撑规模化直播平台
运维·网络·人工智能·小程序·短剧app
今年下半年15 小时前
【微信小程序】uni-app + Vue3 + Vite 的小程序项目实现「进入指定范围才能打卡」的考勤功能
微信小程序·uni-app·定位·考勤
Geek_Vison16 小时前
政务APP新增鸿蒙客户端,如何复用已有的业务单元?
小程序·harmonyos·mpaas·政务·小程序容器
沐子a~16 小时前
小程序实战:萌宠案例
小程序
火眼金睛记单词16 小时前
零基础成人重拾英语:第一个月的30天行动地图
前端·经验分享·学习·小程序
xinraynet17 小时前
沈阳企业小程序开发选型:五个可核验维度,附设备数据采集与私域商城的技术实现要点
大数据·科技·ai·微信小程序·小程序
小码哥0681 天前
Java医院陪诊系统陪护系统陪诊小程序,三端齐全可定制
大数据·小程序·陪诊陪护·陪诊小程序·陪护系统·陪诊系统·陪护小程序
连涨- AI脑波英语1 天前
机构小程序对接词汇课程,测试账号怎样与正式学生账号隔离?
小程序·接口安全·脑机单词速记
扶风ff2 天前
练题簿在线免费刷题:题目导入、章节管理、Excel 与 Word 导出,让题库更好用
学习·小程序·word·excel