web-view-select.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. complete(cplt) {
  63. // 如果有值就打开文档
  64. wx.openDocument({
  65. filePath: filePath,
  66. showMenu: true,
  67. fileType: 'pdf',
  68. success: function (reso) {
  69. console.log('openDocument-success',reso);
  70. },
  71. fail(err) {
  72. console.log('openDocument-fail',err);
  73. },
  74. complete(cplt) {
  75. console.log('openDocument-complete',cplt);
  76. },
  77. })
  78. }
  79. })
  80. }
  81. }
  82. }, fail(err) {
  83. }
  84. })
  85. },
  86. })