index.js 3.3 KB

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