web-view-select.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. console.log('data', that.data.url);
  31. var timesRun = 0;
  32. var interval = setInterval(function () {
  33. timesRun += 1;
  34. if (timesRun > 10 || that.data.filePath) {
  35. clearInterval(interval);
  36. } else {
  37. let path = 'pdf/' + that.data.uuid + '.pdf'
  38. that.downloadFile(`${config.image_url}/${path}`)
  39. }
  40. }, 2000);
  41. })
  42. }
  43. },
  44. /**
  45. * 下载文件资源到本地 打开文档
  46. * @param {*} url
  47. */
  48. downloadFile(url) {
  49. console.log('url', url);
  50. let that = this
  51. wx.downloadFile({
  52. url: url,
  53. fileType: 'pdf',
  54. success: function (resd) {
  55. console.log('resd', resd);
  56. if(resd.statusCode == 200){
  57. const filePath = resd.tempFilePath
  58. // 说明有值
  59. if (filePath) {
  60. that.setData({
  61. filePath: filePath
  62. })
  63. wx.navigateBack({
  64. delta: 1,
  65. })
  66. // 如果有值就打开文档
  67. wx.openDocument({
  68. filePath: filePath,
  69. showMenu: true,
  70. fileType: 'pdf',
  71. success: function (reso) {
  72. }
  73. })
  74. }
  75. }
  76. }, fail(err) {
  77. }
  78. })
  79. },
  80. })