uni-app打包微信小程序,使用web-view查看PDF

概述:

   1、iOS 系统可以直接使用 web-view 打开查看

   2、Android 系统 需要先下载到本地,然后打开查看

注:

微信小程序需配置web-view业务域名,就是pdf访问的域名

具体代码实现:

<template>
  <view>
    <web-view :src="src" v-if="show"></web-view>
  </view>
</template>

<script>
export default {
  data() {
      return {
        src: '',// PDF文件地址
        show: true// 用于判断操作系统
      };
  },
  onLoad(option) {
    const src = `https://www.pt-teach.xyz/${option.src}`;
    // 判断操作系统
    uni.getSystemInfo({
      success: (res) => {
        if (res.platform == "ios") {
          // iOS 可直接查看
          this.show = true;
          this.src = src;
        } else {
          // Android 需要下载后、打开
          this.show = false;
          uni.downloadFile({
            url: src,
            success: (res) => {
              const path = res.tempFilePath;
              uni.openDocument({
                filePath: path,
                fileType: 'pdf',
                success: (res) => {
                  console.log("打开成功!");
                },
                fail: (err) => {
                  uni.showToast({ title: '打开文件失败', icon: 'none', duration: 2000 });
                }
              });
            },
            fail: (err) => {
              console.log(err);
              uni.showToast({ title: '下载文件失败', icon: 'none', duration: 2000 });
            }
          });
        }
      }
    });
  }
}
</script>

 


版权声明:本文为qq_40476712原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。