index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // pages/download/index.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. text: '下载文件'
  8. },
  9. backUrl() {
  10. console.log(123456);
  11. wx.navigateBack({
  12. delta: 1
  13. })
  14. },
  15. /**
  16. * 生命周期函数--监听页面加载
  17. */
  18. onLoad(o) {
  19. if (o.url) {
  20. wx.showLoading({
  21. title: '下载中', //提示文字
  22. mask: true, //是否显示透明蒙层,防止触摸穿透,默认:false
  23. })
  24. let that = this;
  25. console.log('--------url路径', decodeURI(o.url));
  26. const url = decodeURI(o.url)
  27. wx.downloadFile({
  28. url,
  29. success(res) {
  30. wx.getFileSystemManager().saveFile({ //微信保存文件,这个存储有点复杂
  31. // 临时存储路径,先有临时存储路径方可使用wx官方的存储本地路径( wx.env.USER_DATA_PATH )
  32. tempFilePath: res.tempFilePath,
  33. //定义本地的存储路径及名称
  34. filePath: wx.env.USER_DATA_PATH + '/' + Date.now() + '.pdf',
  35. success(res) {
  36. console.log('pppppp', res);
  37. const savedFilePath = res.savedFilePath;
  38. console.log('xxxx', savedFilePath);
  39. that.setData({
  40. text: '正在加载,预览成功后点击右上角即可保存(ios手机需要发送给文件助手后保存)'
  41. });
  42. setTimeout(() => {
  43. wx.hideLoading()
  44. wx.openDocument({ //微信打开文件
  45. filePath: savedFilePath,
  46. showMenu: true,
  47. success: function (res) {
  48. that.setData({
  49. text: '预览成功,点击返回继续',
  50. });
  51. },
  52. fail: function (err) {
  53. wx.showToast({
  54. title: '预览失败',
  55. icon: 'error',
  56. duration: 1500
  57. })
  58. that.setData({
  59. text: '预览失败'
  60. });
  61. }
  62. });
  63. }, 2000);
  64. },
  65. fail(err) {
  66. wx.showToast({
  67. title: '预览失败',
  68. icon: 'error',
  69. duration: 1500
  70. })
  71. that.setData({
  72. text: '预览失败'
  73. });
  74. }
  75. })
  76. },
  77. fail(err) {
  78. wx.hideLoading()
  79. wx.showToast({
  80. title: '下载失败',
  81. icon: 'error',
  82. duration: 1500
  83. })
  84. console.log(err)
  85. that.setData({
  86. text: '下载失败'
  87. });
  88. }
  89. })
  90. }
  91. },
  92. /**
  93. * 生命周期函数--监听页面初次渲染完成
  94. */
  95. onReady() {
  96. },
  97. /**
  98. * 生命周期函数--监听页面显示
  99. */
  100. onShow() {
  101. },
  102. /**
  103. * 生命周期函数--监听页面隐藏
  104. */
  105. onHide() {
  106. },
  107. /**
  108. * 生命周期函数--监听页面卸载
  109. */
  110. onUnload() {
  111. },
  112. /**
  113. * 页面相关事件处理函数--监听用户下拉动作
  114. */
  115. onPullDownRefresh() {
  116. },
  117. /**
  118. * 页面上拉触底事件的处理函数
  119. */
  120. onReachBottom() {
  121. },
  122. /**
  123. * 用户点击右上角分享
  124. */
  125. onShareAppMessage() {
  126. }
  127. })