| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- 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,
- })
- // 如果有值就打开文档
- wx.openDocument({
- filePath: filePath,
- showMenu: true,
- fileType: 'pdf',
- success: function (reso) {
- }
- })
- }
- }
- }, fail(err) {
- }
- })
- },
- })
|