cwg-link.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <!-- #ifdef H5 -->
  3. <!-- H5 端:支持普通跳转 / PDF预览 / 文件下载 -->
  4. <a v-if="isDownload" class="pdf-link" :href="url" :download="downloadName" target="_blank" :title="resolvedTitle">
  5. <slot v-if="slots['default']"></slot>
  6. <view v-else>{{ t(props.title) || props.title }}</view>
  7. </a>
  8. <a v-else-if="isHtml" class="pdf-link" :href="url" :target="target || '_blank'" :title="resolvedTitle"
  9. rel="noopener noreferrer">
  10. <slot v-if="slots['default']"></slot>
  11. <view v-else>{{ t(props.title) || props.title }}</view>
  12. </a>
  13. <view v-else class="pdf-link" @click="handleClick">
  14. <slot v-if="slots['default']"></slot>
  15. <view v-else>{{ t(props.title) || props.title }}</view>
  16. </view>
  17. <!-- #endif -->
  18. <!-- #ifdef APP-PLUS -->
  19. <!-- APP 端:统一点击处理 -->
  20. <view class="pdf-link" @click="handleClick">
  21. <slot v-if="slots['default']"></slot>
  22. <view v-else>{{ t(props.title) || props.title }}</view>
  23. </view>
  24. <!-- #endif -->
  25. </template>
  26. <script setup>
  27. import { computed, useSlots } from 'vue'
  28. import { openLocalPdf } from '@/utils/pdf'
  29. import { openExternalUrl } from '@/utils/openExternalUrl'
  30. import { useI18n } from 'vue-i18n'
  31. const { t } = useI18n()
  32. const slots = useSlots()
  33. const props = defineProps({
  34. url: String,
  35. type: {
  36. type: String,
  37. default: 'pdf' // pdf / pdf1 / html / download
  38. },
  39. title: String,
  40. target: String,
  41. // 下载文件名(下载时必填)
  42. downloadName: String
  43. })
  44. const isDownload = props.type === 'download'
  45. const isHtml = props.type === 'html'
  46. const resolvedTitle = computed(() => {
  47. if (!props.title) return undefined
  48. return t(props.title)
  49. })
  50. const handleClick = () => {
  51. if (!props.url) return
  52. // 下载逻辑(APP端)
  53. if (props.type === 'download') {
  54. startDownload()
  55. return
  56. }
  57. // PDF 预览
  58. if (props.type == 'pdf' || props.type == 'pdf1') {
  59. openLocalPdf(props.url, props.title, props.type)
  60. }
  61. // 外链打开
  62. else if (props.type === 'html') {
  63. openExternalUrl(props.url)
  64. }
  65. }
  66. // APP 端下载方法
  67. const startDownload = () => {
  68. uni.showLoading({ title: '下载中...', mask: true })
  69. uni.downloadFile({
  70. url: props.url,
  71. success: (res) => {
  72. uni.hideLoading()
  73. if (res.statusCode === 200) {
  74. uni.openDocument({
  75. filePath: res.tempFilePath,
  76. fileName: props.downloadName,
  77. success: () => {
  78. uni.showToast({ icon: 'none', title: t('mine.p27') })
  79. }
  80. })
  81. } else {
  82. uni.showToast({ icon: 'none', title: t('mine.p28') })
  83. }
  84. },
  85. fail: () => {
  86. uni.hideLoading()
  87. uni.showToast({ icon: 'none', title: t('mine.p28') })
  88. }
  89. })
  90. }
  91. </script>
  92. <style scoped lang="scss">
  93. .pdf-link {
  94. //width: 100%;
  95. display: inline-block;
  96. cursor: pointer;
  97. }
  98. </style>