微信小程序在线预览PDF文件

需求:微信小程序在线预览PDF合同文件,加载完成后强制阅读10秒才可点击同意按钮

H5代码:

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>微信小程序在线预览PDF</title>
  <link rel="stylesheet" href="./css/pdfh5.css">
  <link rel="stylesheet" href="./css/style.css">
  <style>
    #pdf {
      width: 100%;
      height: 100vh;
    }

    .btn {
      display: flex;
      justify-content: center;
      align-items: center;
      position: absolute;
      left: 32px;
      right: 32px;
      bottom: 10px;
      height: 50px;
      background-image: linear-gradient(270deg, #FF5D31 0%, #FFA853 100%);
      border-radius: 44px;
      font-weight: 500;
      font-size: 16px;
      color: #fff;
    }
  </style>
</head>

<body>
  <div id="app">
    <div id="pdf"></div>
    <div class="btn" style="opacity: .8;" v-if="show">{{message}}</div>
    <div class="btn" v-else @click="submit">同意签署</div>
  </div>
  <script src="./js/pdf.js"></script>
  <script src="./js/pdf.worker.js"></script>
  <script src="./js/jquery-3.6.0.min.js"></script>
  <script src="./js/pdfh5.js"></script>
  <script src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
  <script>
    var app = new Vue({
      el: '#app',
      data: {
        show: true,
        num: 10,
        timer: null
      },
      computed: {
        message() {
          if (this.num === 10) {
            return '合同加载中...'
          } else {
            return '阅读倒计时' + this.num + '秒'
          }
        }
      },
      methods: {
        submit() {
          wx.miniProgram.postMessage({ data: 'done' })
        },
      },

      mounted() {
        let searchURL = window.location.search;
        searchURL = searchURL.substring(1, searchURL.length);
        const pdfurl = searchURL.split("&")[0].split("=")[1];
        const pdfh5 = new Pdfh5('#pdf', {
          pdfurl,
        }, {
          lazy: true
        });

        pdfh5.on("complete", (status) => {
          if (status === 'error') return

          this.timer = setInterval(() => {
            if (this.num === 0) {
              this.show = false
              clearInterval(this.timer)
            } else {
              this.num -= 1
            }
          }, 1000)
        })
      },
      
	  unmounted() {
        this.timer && clearInterval(this.timer)
      },
    })
  </script>
</body>

</html>

微信小程序:

javascript 复制代码
wxml
<web-view src="{{src}}" bindmessage="bindmessage" />

js
Page({
  data: {
    src: '',
  },

  onLoad() {
    this.setData({
      src: `${将H5页面部署到服务器的地址}?file=${pdf文件路径}`
    })
  },

  bindmessage(e) {
    if (e.detail.data[0] === 'done') {
    // todo 处理同意后的逻辑
    }
  },
})
相关推荐
我命由我123454 分钟前
访客系统形式(有用户概念、无用户概念)
java·开发语言·前端·javascript·python·产品运营·产品经理
key_3_feng19 分钟前
评估与对齐:Agent 自我进化的护栏
前端·数据库·agent
程序员大阳34 分钟前
副队长JavaScript教程(65)-详解JSON:轻量级数据交换格式
javascript·json·序列化·反序列化·stringify·parse
LaughingZhu1 小时前
Product Hunt 热榜 2026-10-30:Empacer、Scape、Floot in ChatGPT
java·前端·数据库·人工智能·chatgpt·notion
Lank_M1 小时前
抠图结果缩放后边缘发脏,该怪的不是插值
前端
jerrywus1 小时前
Skill、MCP、Workflow、Rules、Memory 有什么区别?拿几次真实开发任务讲清楚
前端·agent·claude
补码缩写1 小时前
canvas 与 sharp 缩放透明图:默认预乘,非预乘得拆通道
前端
静默回滚1 小时前
抠图 PNG 缩小再放大,边缘又糊又白是两件事
前端
用户11977079480731 小时前
GitHub 电商开源项目分享:用 sharp 批量出饰品主图,试了裁剪、细节图和色彩配置文件三个坑
前端·node.js
deli0072 小时前
地图定位为什么偏了 500 米?GPS 与火星坐标实测
前端