web-view-select.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 pdf = 'pdf/' + that.data.uuid + '.pdf'
  38. that.downloadFile(`${config.image_url}/${pdf}`)
  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,并非真实存在
  53. url: url,
  54. fileType: 'pdf',
  55. success: function (resd) {
  56. console.log('resd', resd);
  57. if(resd.statusCode == 200){
  58. const filePath = resd.tempFilePath
  59. // 说明有值
  60. if (filePath) {
  61. that.setData({
  62. filePath: filePath
  63. })
  64. wx.navigateBack({
  65. delta: 1,
  66. })
  67. // 如果有值就打开文档
  68. wx.openDocument({
  69. filePath: filePath,
  70. showMenu: true,
  71. fileType: 'pdf',
  72. success: function (reso) {
  73. }
  74. })
  75. }
  76. }
  77. }, fail(err) {
  78. }
  79. })
  80. },
  81. })