web-view-select.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const config = require('@/config/config.js');
  2. const util = require('@/utils/util.js')
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. url: null,
  10. filePath: null
  11. },
  12. /**
  13. * 生命周期函数--监听页面加载
  14. */
  15. onLoad(options) {
  16. // 获取文件 的uuid
  17. let uuid = util.getGuid()
  18. this.setData({
  19. uuid: uuid
  20. })
  21. // 接收父页面传递的参数
  22. const eventChannel = this.getOpenerEventChannel();
  23. if (eventChannel && JSON.stringify(eventChannel) !== '{}') {
  24. let that = this;
  25. // 指定类型
  26. eventChannel.once('url', function (data) {
  27. that.setData({
  28. url: data + '&uuid=' + uuid
  29. })
  30. var timesRun = 0;
  31. var interval = setInterval(function () {
  32. timesRun += 1;
  33. if (timesRun > 10 || that.data.filePath) {
  34. clearInterval(interval);
  35. } else {
  36. let path = 'pdf/' + that.data.uuid + '.pdf'
  37. that.downloadFile(`${config.image_url}/${path}`)
  38. }
  39. }, 2000);
  40. })
  41. }
  42. },
  43. /**
  44. * 下载文件资源到本地 打开文档
  45. * @param {*} url
  46. */
  47. downloadFile(url) {
  48. let that = this
  49. wx.downloadFile({
  50. url: url,
  51. fileType: 'pdf',
  52. success: function (resd) {
  53. if(resd.statusCode == 200){
  54. const filePath = resd.tempFilePath
  55. // 说明有值
  56. if (filePath) {
  57. that.setData({
  58. filePath: filePath
  59. })
  60. wx.navigateBack({
  61. delta: 1,
  62. })
  63. // 如果有值就打开文档
  64. wx.openDocument({
  65. filePath: filePath,
  66. showMenu: true,
  67. fileType: 'pdf',
  68. success: function (reso) {
  69. }
  70. })
  71. }
  72. }
  73. }, fail(err) {
  74. }
  75. })
  76. },
  77. })