const config = require('@/config/config.js'); const util = require('@/utils/util.js') const app = getApp() Page({ /** * 页面的初始数据 */ data: { url: null, filePath: null }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { // 获取文件 的uuid let uuid = util.getGuid() this.setData({ uuid: uuid }) // 接收父页面传递的参数 const eventChannel = this.getOpenerEventChannel(); if (eventChannel && JSON.stringify(eventChannel) !== '{}') { let that = this; // 指定类型 eventChannel.once('url', function (data) { that.setData({ url: data + '&uuid=' + uuid }) var timesRun = 0; var interval = setInterval(function () { timesRun += 1; if (timesRun > 10 || that.data.filePath) { clearInterval(interval); } else { let path = 'pdf/' + that.data.uuid + '.pdf' that.downloadFile(`${config.image_url}/${path}`) } }, 2000); }) } }, /** * 下载文件资源到本地 打开文档 * @param {*} url */ downloadFile(url) { let that = this wx.downloadFile({ url: url, fileType: 'pdf', success: function (resd) { if(resd.statusCode == 200){ const filePath = resd.tempFilePath // 说明有值 if (filePath) { that.setData({ filePath: filePath }) wx.navigateBack({ delta: 1, complete(cplt) { // 如果有值就打开文档 wx.openDocument({ filePath: filePath, showMenu: true, fileType: 'pdf', success: function (reso) { console.log('openDocument-success',reso); }, fail(err) { console.log('openDocument-fail',err); }, complete(cplt) { console.log('openDocument-complete',cplt); }, }) } }) } } }, fail(err) { } }) }, })