需求:注册账号时,需要用户可以在线浏览注册协议。
1.注册页面关键代码
// html代码 <view class="word"> <text>我已阅读并同意</text ><text class="text" @click="previewFile" >《xxx服务平台协议》</text ><text>条款</text> </view> // js代码 // 预览协议的方法 previewFile() { uni.downloadFile({ url: "https://xxx.pdf", // 后端提供的pdf地址 success(res) { let path = res.tempFilePath; uni.openDocument({ filePath: res.tempFilePath, fileType: "pdf", showMenu: true, success: function (res) { console.log("打开文档成功"); }, fail: function (res) { uni.showToast({ title: "打开文档失败", }); }, }); }, }); },
2. web-view页面代码
<template> <view class="main"> <view class="content"> <web-view :src="allUrl"></web-view> </view> </view> </template> <script> export default { data() { return { allUrl: "", }; }, onLoad(options) { let fileUrl = decodeURIComponent(options.eSrc); // 接收后端给的pdf地址并解码 this.allUrl = fileUrl; console.log(this.allUrl); }, // 解决预览后返回出现自动跳转的问题 onBackPress(options) { console.log(options); if (options.from === "backbutton") { this.back(); return true; } }, methods: { // 返回原页面的方法 back(){ uni.navigateTo({ url:'/pagesA/login/personRegister' }) } }, }; </script> <style> </style>