cwg-file.vue 3.9 KB

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