useDownload.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import env from '@/config';
  2. import config from '@/common/mxConfig';
  3. import {useEnvStore} from "@/hooks/useEnvStore";
  4. import {useUserStore} from "@/hooks/useUserStore";
  5. export const useDownload = function () {
  6. const {token, period} = useUserStore()
  7. const {isH5, isWap2App} = useEnvStore()
  8. const postMessage = (msgType, data) => {
  9. uni.webView.postMessage({
  10. data: {
  11. action: msgType,
  12. data: data
  13. }
  14. });
  15. }
  16. const downloadBlobFile = (response, filename) => {
  17. // download blob file
  18. const url = window.URL.createObjectURL(new Blob([response.data]))
  19. // get file suffix from response header
  20. // build file name from data.name data.score data.batchName and suffix
  21. let disposition = response.header['content-disposition']
  22. disposition = disposition.replace('"', '').replace("'", '')
  23. const suffix = disposition.substring(disposition.lastIndexOf('.'))
  24. const fileName = `${filename}${suffix}`
  25. // Create a download link and trigger a click event to download the file
  26. const link = document.createElement('a')
  27. link.href = url
  28. link.setAttribute('download', fileName)
  29. document.body.appendChild(link)
  30. link.click()
  31. // Remove the link element and release the object URL
  32. document.body.removeChild(link)
  33. window.URL.revokeObjectURL(url)
  34. }
  35. const downloadBlobFileForWap2app = (reqOption, fileName) => {
  36. // reqOption: {url, params}
  37. if (isWap2App.value) {
  38. postMessage('downloadBlob', {
  39. ...reqOption,
  40. token: token.value
  41. });
  42. }
  43. }
  44. const downloadRealPaper = (paperId) => {
  45. let path = env.serverBaseUrl + '/front/v2/papers/downloadRealPaper'
  46. path += '?paperId=' + paperId + '&period=' + period.value
  47. const override = {
  48. isOffice: () => true,
  49. getFileType: () => 'docx'
  50. }
  51. downloadFile(path, false, override)
  52. }
  53. const downloadFile = (url, openOnly = false, overrideFileHelper = {}) => {
  54. const itemList = (openOnly || isWap2App.value) ? ["打开文档"] : ["打开文档", "下载文档"];
  55. uni.showActionSheet({
  56. itemList: itemList,
  57. success: res => {
  58. const fileHelper = {
  59. ...config.fileHelper,
  60. ...overrideFileHelper
  61. }
  62. switch (res.tapIndex) {
  63. case 0:
  64. if (fileHelper.isFile(url)) {
  65. if (isWap2App.value) {
  66. postMessage('openFile', {url});
  67. } else if (isH5.value) {
  68. window.open(url);
  69. }
  70. } else if (fileHelper.isImage(url)) {
  71. if (isWap2App.value) {
  72. postMessage('openImage', {url});
  73. } else if (isH5.value) {
  74. uni.previewImage({
  75. urls: [url]
  76. });
  77. }
  78. } else {
  79. if (isWap2App.value) {
  80. postMessage('openFile', {url});
  81. } else if (isH5.value) {
  82. uni.showToast({
  83. title: "暂不支持直接打开该文档",
  84. icon: "none"
  85. });
  86. }
  87. }
  88. break;
  89. case 1:
  90. const remoteFunc = fileHelper.isImage(url) ? 'openImage' : 'downloadFile';
  91. if (isWap2App.value) {
  92. postMessage(remoteFunc, {url});
  93. } else if (isH5.value) {
  94. // 大文件不适合此方案,最好后台请求头部加上Content-Disposition: attachment; filename=xxx.xxx来触发下载
  95. uni.showLoading();
  96. uni.downloadFile({
  97. url: url,
  98. success: (res) => {
  99. if (res.statusCode === 200) {
  100. const fileName = url.split('/').pop();
  101. const a = document.createElement("a");
  102. a.target = '_blank';
  103. a.download = fileName;
  104. a.href = res.tempFilePath;
  105. document.body.appendChild(a);
  106. a.click();
  107. a.remove();
  108. }
  109. },
  110. fail: (err) => {
  111. uni.showToast({
  112. icon: 'none',
  113. mask: true,
  114. title: '失败请重新下载',
  115. });
  116. },
  117. complete: () => {
  118. uni.hideLoading();
  119. }
  120. })
  121. }
  122. break;
  123. default:
  124. break;
  125. }
  126. }
  127. });
  128. }
  129. return {
  130. postMessage,
  131. downloadRealPaper,
  132. downloadFile,
  133. downloadBlobFile,
  134. downloadBlobFileForWap2app
  135. }
  136. }