donwload-tools.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import SimpleProgress from '@/components/SimpleProgress'
  2. import handleClipboard from '@/utils/clipboard'
  3. import { isWeChat } from '@/utils/validator'
  4. import Vue from 'vue'
  5. import { Dialog } from 'vant'
  6. import { encrypt } from './crypto'
  7. let uuid = 0 // 进度条id
  8. export async function downloadWithUrl(downUrl, fileName, self) {
  9. const h = self.$createElement
  10. let progressRef, tiemer
  11. const notification = self.$notify({
  12. title: '提示',
  13. message: h('div', {}, [
  14. h('div', {}, `正在下载${fileName},请勿重复操作!`),
  15. h(SimpleProgress, { ref: `progress${uuid}` }),
  16. ]),
  17. duration: 0,
  18. })
  19. self.$nextTick(() => {
  20. progressRef = self.$refs[`progress${uuid}`]
  21. tiemer = setInterval(() => {
  22. if (progressRef.percentage < 90) {
  23. progressRef.percentage += 2
  24. } else {
  25. clearInterval(tiemer)
  26. }
  27. }, 500)
  28. })
  29. try {
  30. const data = await fetch(downUrl, {
  31. headers: { 'X-Token': self.$store.getters.accessToken },
  32. })
  33. if (data.redirected) {
  34. const resultData = await data.json()
  35. console.log(resultData.code)
  36. // 登录过期
  37. if (resultData.code === -99) {
  38. notification && notification.close()
  39. const result = await Dialog.alert({
  40. title: '提示',
  41. message: '登录已过期,请重新登录',
  42. theme: 'round-button',
  43. confirmButtonColor: 'linear-gradient(to left, #404040, #101010)',
  44. })
  45. Vue.prototype.$removeStorage(
  46. self.$store.getters.routePrefix,
  47. 'userInfo'
  48. )
  49. self.$store.dispatch('user/logout')
  50. if (result === 'confirm') {
  51. // redirect(self.$store.getters.routePrefix)
  52. self.$router.replace(self.$store.getters.routePrefix)
  53. }
  54. return
  55. }
  56. }
  57. const res = await data.blob()
  58. const link = document.createElement('a')
  59. const url = URL.createObjectURL(res)
  60. link.href = url
  61. link.download = fileName
  62. link.click()
  63. console.log(url)
  64. URL.revokeObjectURL(url)
  65. } catch (err) {
  66. self.$message.error(`下载${fileName}失败`)
  67. console.log(err)
  68. } finally {
  69. clearInterval(tiemer)
  70. progressRef.percentage = 100
  71. notification && notification.close()
  72. }
  73. }
  74. // 通过链接下载
  75. export default function downloadFile(downUrl, fileName, self, $event) {
  76. if (isWeChat()) {
  77. // 加密下载链接
  78. const data = {
  79. downUrl: downUrl,
  80. token: self.$store.getters.accessToken,
  81. fileName: fileName,
  82. }
  83. const redirectUrl = `${
  84. process.env.LOCALHOSE
  85. }/public/download?state=${encodeURIComponent(encrypt(data))}`
  86. return handleClipboard(
  87. redirectUrl,
  88. $event,
  89. '下载链接已复制到剪切板,请粘贴到浏览器中下载'
  90. )
  91. } else {
  92. downloadWithUrl(downUrl, fileName, self)
  93. }
  94. }