download.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // pages/file/download.js
  2. const util = require('../../utils/util.js');
  3. const api = require('../../utils/o2Api.js');
  4. Page({
  5. /**
  6. * Page initial data
  7. */
  8. data: {
  9. },
  10. /**
  11. * Lifecycle function--Called when page load
  12. */
  13. onLoad: function (options) {
  14. util.showLoading();
  15. var attId = options.attId; //附件id
  16. var type = options.type; // work | cms | bbs
  17. console.log('attId:', attId, ' type: ', type);
  18. if (!attId || !type) {
  19. util.hideLoading();
  20. console.log('没有传入参数!');
  21. wx.navigateBack({
  22. delta: 1,
  23. });
  24. }else {
  25. if (type == 'work') { // type = work
  26. if (options.workCompleted) {
  27. var url = api.workCompletedAttachementUrl(attId, options.workCompleted);
  28. this.downloadFileAndOpen(url);
  29. } else if (options.work) {
  30. var url = api.workAttachmentUrl(attId, options.work);
  31. this.downloadFileAndOpen(url);
  32. }else {
  33. util.hideLoading();
  34. console.log('没有传入工作id!');
  35. wx.navigateBack({
  36. delta: 1,
  37. });
  38. }
  39. } else if (type == 'cms') {//type = cms
  40. // var documentId = options.documentId;
  41. var url = api.cmsAttachementUrl(attId);
  42. this.downloadFileAndOpen(url);
  43. } else if (type == 'bbs') {//type = bbs
  44. // var subjectId = options.subjectId;
  45. var url = api.bbsAttachementUrl(attId);
  46. this.downloadFileAndOpen(url);
  47. }else {
  48. util.hideLoading();
  49. console.log('参数type不正确!');
  50. wx.navigateBack({
  51. delta: 1,
  52. });
  53. }
  54. }
  55. },
  56. //下载文件
  57. downloadFileAndOpen: function (url) {
  58. var who = wx.getStorageSync('who');
  59. var tokenName = wx.getStorageSync('tokenName');
  60. var token = '';
  61. if (who && who.token) {
  62. token = who.token;
  63. }
  64. let header = {}
  65. header[tokenName] = token
  66. console.log("url ", url)
  67. console.log(header)
  68. wx.downloadFile({
  69. url: url,
  70. header: header,
  71. success(res) {
  72. console.log(res)
  73. if (res.statusCode === 200) {
  74. console.log(res)
  75. var filePath = res.tempFilePath
  76. wx.navigateBack({
  77. delta: 1,
  78. success: function() {
  79. // 打开这个文件
  80. wx.openDocument({
  81. filePath: filePath,
  82. success: function (res) {
  83. console.log('打开文档成功')
  84. }
  85. });
  86. }
  87. });
  88. } else {
  89. console.log("下载失败。。。。。");
  90. util.hideLoading();
  91. util.toast('下载失败!');
  92. wx.navigateBack({
  93. delta: 1,
  94. });
  95. }
  96. },
  97. fail(res) {
  98. console.log(res);
  99. util.hideLoading();
  100. util.toast('下载失败!');
  101. wx.navigateBack({
  102. delta: 1,
  103. });
  104. }
  105. })
  106. },
  107. /**
  108. * Lifecycle function--Called when page is initially rendered
  109. */
  110. onReady: function () {
  111. },
  112. /**
  113. * Lifecycle function--Called when page show
  114. */
  115. onShow: function () {
  116. },
  117. /**
  118. * Lifecycle function--Called when page hide
  119. */
  120. onHide: function () {
  121. },
  122. /**
  123. * Lifecycle function--Called when page unload
  124. */
  125. onUnload: function () {
  126. },
  127. /**
  128. * Page event handler function--Called when user drop down
  129. */
  130. onPullDownRefresh: function () {
  131. },
  132. /**
  133. * Called when page reach bottom
  134. */
  135. onReachBottom: function () {
  136. },
  137. /**
  138. * Called when user click on the top right corner to share
  139. */
  140. onShareAppMessage: function () {
  141. }
  142. })