index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // 获取参数
  42. const query = JSON.parse(decrypt(this.$route.query.state))
  43. this.query = query
  44. this.timer = setInterval(() => {
  45. this.time++
  46. }, 1000)
  47. this.isRequest = true
  48. const data = await fetch(query.downUrl, {
  49. headers: { 'X-Token': query.token },
  50. })
  51. // 链接是否重定向
  52. if (data.redirected) {
  53. const resultData = await data.json()
  54. // 登录过期
  55. if (resultData.code === -99) {
  56. return this.$toast('链接已失效~~')
  57. }
  58. }
  59. const res = await data.blob()
  60. const link = document.createElement('a')
  61. const url = URL.createObjectURL(res)
  62. link.href = url
  63. link.download = query.fileName
  64. link.click()
  65. URL.revokeObjectURL(url)
  66. clearInterval(this.timer)
  67. this.success = true
  68. } catch (error) {
  69. console.log(error)
  70. this.$toast('链接已失效~~')
  71. clearInterval(this.timer)
  72. } finally {
  73. this.isRequest = false
  74. }
  75. },
  76. },
  77. }
  78. </script>
  79. <style scoped lang="scss">
  80. .download-page {
  81. width: 100vw;
  82. height: 100vh;
  83. overflow: hidden;
  84. // background: #f1f8ee;
  85. box-sizing: border-box;
  86. padding-top: 16%;
  87. .retry {
  88. text-align: center;
  89. span {
  90. text-decoration: underline;
  91. transition: all 0.4s;
  92. &:hover {
  93. color: red;
  94. }
  95. }
  96. }
  97. }
  98. </style>