cwg-link.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <template>
  2. <!-- #ifdef H5 -->
  3. <!-- H5 端:支持普通跳转 / PDF预览 / 文件下载 -->
  4. <a v-if="isDownload" class="pdf-link cursor-pointer" :href="url" :download="downloadName" target="_blank" :data-tooltip="resolvedTitle" data-placement="top">
  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 cursor-pointer" :href="url" :target="target || '_blank'"
  9. rel="noopener noreferrer" :data-tooltip="resolvedTitle" data-placement="top">
  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 cursor-pointer" @click="handleClick" :data-tooltip="t(props.title) || props.title" data-placement="top">
  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. // title是否直接展示不用多语言方法转义
  42. isText: Boolean,
  43. // 下载文件名(下载时必填)
  44. downloadName: String
  45. })
  46. const isDownload = props.type === 'download'
  47. const isHtml = props.type === 'html'
  48. const resolvedTitle = computed(() => {
  49. if (!props.title) return undefined
  50. return props.isText?props.title : t(props.title)
  51. })
  52. const handleClick = () => {
  53. if (!props.url) return
  54. // 下载逻辑(APP端)
  55. if (props.type === 'download') {
  56. startDownload()
  57. return
  58. }
  59. // PDF 预览
  60. if (props.type == 'pdf' || props.type == 'pdf1') {
  61. openLocalPdf(props.url, props.title, props.type)
  62. }
  63. // 外链打开
  64. else if (props.type === 'html') {
  65. openExternalUrl(props.url)
  66. }
  67. }
  68. // APP 端下载方法
  69. const startDownload = () => {
  70. uni.showLoading({ title: '下载中...', mask: true })
  71. uni.downloadFile({
  72. url: props.url,
  73. success: (res) => {
  74. uni.hideLoading()
  75. if (res.statusCode === 200) {
  76. uni.openDocument({
  77. filePath: res.tempFilePath,
  78. fileName: props.downloadName,
  79. success: () => {
  80. uni.showToast({ icon: 'none', title: t('mine.p27') })
  81. }
  82. })
  83. } else {
  84. uni.showToast({ icon: 'none', title: t('mine.p28') })
  85. }
  86. },
  87. fail: () => {
  88. uni.hideLoading()
  89. uni.showToast({ icon: 'none', title: t('mine.p28') })
  90. }
  91. })
  92. }
  93. </script>
  94. <style scoped lang="scss">
  95. .pdf-link {
  96. //width: 100%;
  97. display: inline-block;
  98. cursor: pointer;
  99. }
  100. </style>