cwg-file.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <view class="file-preview">
  3. <!-- PDF 预览链接 -->
  4. <!-- <view v-if="isPdfFile" class="pdf-link" @click="handlePreviewPdf">-->
  5. <!-- <uni-icons type="document" size="20" color="#ff0000"></uni-icons>-->
  6. <!-- <text class="pdf-text">{{ displayFileName }}</text>-->
  7. <!-- </view>-->
  8. <!-- 图片预览 -->
  9. <view v-if="isImageFile" class="image-preview cursor-pointer" :data-tooltip="t('vu.tooltip.t16')">
  10. <image :src="fullPath" mode="aspectFill" class="preview-image" @click.stop="handlePreviewImage" />
  11. </view>
  12. <!-- 其他文件类型 -->
  13. <view v-else class="file-info">
  14. <cwg-icon icon="crm-document" :size="24" color="#000" />
  15. <!-- <uni-icons type="folder" size="20" color="#007aff"></uni-icons>-->
  16. <!-- <text class="file-name">{{ displayFileName }}</text>-->
  17. </view>
  18. </view>
  19. </template>
  20. <script setup>
  21. import { computed } from 'vue'
  22. import { useI18n } from "vue-i18n";
  23. import config from '@/config';
  24. const { t,locale } = useI18n();
  25. const props = defineProps({
  26. // 文件路径
  27. path: {
  28. type: String,
  29. default: ''
  30. },
  31. // 更新URL前缀
  32. updateUrl: {
  33. type: String,
  34. default: config.Host05
  35. },
  36. // 文件名称(可选)
  37. fileName: {
  38. type: String,
  39. default: ''
  40. }
  41. })
  42. // 完整的文件路径
  43. const fullPath = computed(() => {
  44. console.log(props.updateUrl, props.path);
  45. if (!props.path) return ''
  46. return props.updateUrl + props.path
  47. })
  48. // 判断是否为PDF文件
  49. const isPdfFile = computed(() => {
  50. if (!props.path) return false
  51. const ext = props.path.substr(-3).toLowerCase()
  52. return ext === 'pdf'
  53. })
  54. // 判断是否为图片文件
  55. const isImageFile = computed(() => {
  56. if (!props.path) return false
  57. const ext = props.path.substr(-3).toLowerCase()
  58. return ['jpg', 'png', 'gif', 'jpeg', 'bmp', 'webp'].includes(ext) ||
  59. props.path.substr(-4).toLowerCase() === 'jpeg'
  60. })
  61. // 获取文件名
  62. const displayFileName = computed(() => {
  63. if (props.fileName) return props.fileName
  64. if (!props.path) return '--'
  65. // 提取文件名
  66. const parts = props.path.split('/')
  67. return parts[parts.length - 1] || props.path
  68. })
  69. // 预览PDF
  70. const handlePreviewPdf = () => {
  71. if (!fullPath.value) return
  72. // #ifdef H5
  73. window.open(fullPath.value, '_blank')
  74. // #endif
  75. // #ifdef APP-PLUS
  76. plus.runtime.openURL(fullPath.value)
  77. // #endif
  78. // #ifdef MP-WEIXIN
  79. uni.downloadFile({
  80. url: fullPath.value,
  81. success: (res) => {
  82. if (res.statusCode === 200) {
  83. uni.openDocument({
  84. filePath: res.tempFilePath,
  85. success: () => {
  86. console.log('打开文档成功')
  87. }
  88. })
  89. }
  90. }
  91. })
  92. // #endif
  93. }
  94. // 预览图片
  95. const handlePreviewImage = () => {
  96. if (!fullPath.value) return
  97. uni.previewImage({
  98. urls: [fullPath.value],
  99. current: 0
  100. })
  101. }
  102. </script>
  103. <style scoped lang="scss">
  104. .pdf-link {
  105. display: flex;
  106. align-items: center;
  107. gap: px2rpx(8);
  108. padding: px2rpx(4) px2rpx(8);
  109. background-color: #fff1f0;
  110. border-radius: px2rpx(4);
  111. cursor: pointer;
  112. transition: all 0.3s;
  113. &:hover {
  114. background-color: #ffe7e6;
  115. }
  116. .pdf-text {
  117. color: #ff0000;
  118. font-size: px2rpx(14);
  119. max-width: px2rpx(200);
  120. overflow: hidden;
  121. text-overflow: ellipsis;
  122. white-space: nowrap;
  123. }
  124. }
  125. .image-preview {
  126. .preview-image {
  127. width: px2rpx(60);
  128. height: px2rpx(60);
  129. border-radius: px2rpx(4);
  130. object-fit: cover;
  131. cursor: pointer;
  132. transition: transform 0.3s;
  133. &:hover {
  134. transform: scale(1.1);
  135. }
  136. }
  137. }
  138. .file-info {
  139. display: flex;
  140. align-items: center;
  141. .file-name {
  142. color: var(--bs-heading-color);
  143. font-size: px2rpx(14);
  144. max-width: px2rpx(200);
  145. overflow: hidden;
  146. text-overflow: ellipsis;
  147. white-space: nowrap;
  148. }
  149. }
  150. </style>