index.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <template>
  2. <div class="download-page flex items-center flex-col">
  3. <div class="retry text-sm text-center text-gray-700" v-if="!isRequest">
  4. 下载出现问题了?<span class="cursor-pointer" @click="init"
  5. >点我重试!</span
  6. >
  7. </div>
  8. <template v-else>
  9. <div class="dot-elastic"></div>
  10. <div class="text-sm mt-6 text-center text-gray-700">
  11. {{ downloadTip }}
  12. </div>
  13. </template>
  14. </div>
  15. </template>
  16. <script>
  17. import { decrypt } from '~/utils/crypto'
  18. export default {
  19. data() {
  20. return {
  21. query: {},
  22. timer: null,
  23. time: 0,
  24. isRequest: false,
  25. success: false,
  26. }
  27. },
  28. computed: {
  29. downloadTip() {
  30. return this.time > 10
  31. ? '当前文件过大,请勿退出,请耐心等待~~'
  32. : '正在下载,请稍等~~'
  33. },
  34. },
  35. created() {
  36. this.init()
  37. },
  38. methods: {
  39. async init() {
  40. try {
  41. const state = this.$route.query.state
  42. if (!state) return
  43. // 获取参数
  44. const query = JSON.parse(decrypt(this.$route.query.state))
  45. this.query = query
  46. this.timer = setInterval(() => {
  47. this.time++
  48. }, 1000)
  49. this.isRequest = true
  50. const data = await fetch(query.downUrl, {
  51. headers: { 'X-Token': query.token },
  52. })
  53. // 链接是否重定向
  54. if (data.redirected) {
  55. const resultData = await data.json()
  56. // 登录过期
  57. if (resultData.code === -99) {
  58. return this.$toast('链接已失效~~')
  59. }
  60. }
  61. const res = await data.blob()
  62. const link = document.createElement('a')
  63. const url = URL.createObjectURL(res)
  64. link.href = url
  65. link.download = query.fileName
  66. link.click()
  67. URL.revokeObjectURL(url)
  68. clearInterval(this.timer)
  69. this.success = true
  70. } catch (error) {
  71. console.log(error)
  72. this.$toast('链接已失效~~')
  73. clearInterval(this.timer)
  74. } finally {
  75. this.isRequest = false
  76. }
  77. },
  78. },
  79. }
  80. </script>
  81. <style scoped lang="scss">
  82. .download-page {
  83. width: 100vw;
  84. height: 100vh;
  85. overflow: hidden;
  86. // background: #f1f8ee;
  87. box-sizing: border-box;
  88. padding-top: 16%;
  89. .retry {
  90. text-align: center;
  91. span {
  92. text-decoration: underline;
  93. transition: all 0.4s;
  94. &:hover {
  95. color: red;
  96. }
  97. }
  98. }
  99. }
  100. </style>