cwg-file.vue 4.0 KB

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