You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

159 lines
4.8 KiB

  1. <template>
  2. <view>
  3. <view class="cu-form-group" style="border-bottom: none; padding-bottom: 0;">
  4. <view class="title">
  5. <text v-if="required" class="lr-required">*</text>
  6. {{ title || '' }}
  7. </view>
  8. </view>
  9. <view class="cu-form-group" style="border-top: none;">
  10. <view class="grid col-4 grid-square flex-sub">
  11. <view v-for="(file, index) in value" :key="index" class="bg-img">
  12. <image
  13. v-if="isImgFile(file)"
  14. @click="fileClick(index)"
  15. :src="file.path?file.path:file"
  16. :webp="file.type === 'webp'"
  17. mode="aspectFill"
  18. ></image>
  19. <view v-else-if="isDocFile(file)" @click="fileClick(index)" class="file-icon solids">
  20. <l-icon type="text" />
  21. </view>
  22. <view v-else class="file-icon solids"><l-icon type="text" /></view>
  23. <view v-if="!readonly" @click.stop="delFile(index)" class="cu-tag bg-red" style="height: 24px; width: 24px;">
  24. <l-icon type="close" color="white" style="width: 18px; height: 24px; font-size: 24px;" />
  25. </view>
  26. </view>
  27. <view v-if="!readonly && value.length < Number(number)" @click="chooseFile" class="solids">
  28. <l-icon type="file" />
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. export default {
  36. name: 'l-upload-file',
  37. props: {
  38. number: { default: 1 },
  39. readonly: {},
  40. value: { default: () => [] },
  41. title: {},
  42. required: {}
  43. },
  44. methods: {
  45. getFileExt(path) {
  46. return /\.(\w{2,5})$/.exec(path)[1] || null
  47. },
  48. isImgFile(file) {
  49. const typeString = (file.type || '').toLowerCase()
  50. return ['jpg','image/jpg','jpeg','image/jpeg', 'png', 'image/png','gif', 'image/gif','bmp', 'image/bmp','webp', 'image/webp','image'].includes(typeString)
  51. },
  52. isDocFile(file) {
  53. const typeString = (file.type || '').toLowerCase()
  54. return ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf'].includes(typeString)
  55. return true
  56. },
  57. delFile(index) {
  58. const newList = JSON.parse(JSON.stringify(this.value))
  59. newList.splice(index, 1)
  60. this.$emit('input', newList)
  61. this.$emit('change')
  62. this.$emit('del')
  63. },
  64. chooseFile() {
  65. // #ifdef MP-DINGTALK
  66. // dd.chooseImage({
  67. // count: Number(this.number),
  68. // success: ({ filePaths }) => {
  69. // if (filePaths) {
  70. // const newList = JSON.parse(JSON.stringify(this.value || [])).concat(
  71. // filePaths.map(t => ({ path: t, type: this.getFileExt(t) }))
  72. // )
  73. // this.$emit('input', newList)
  74. // this.$emit('change', newList)
  75. // this.$emit('add')
  76. // }
  77. // }
  78. // })
  79. // #endif
  80. // #ifndef MP-DINGTALK
  81. uni.chooseImage({
  82. count: Number(this.number),
  83. sizeType: ['original', 'compressed'],
  84. sourceType: ['album', 'camera'],
  85. success: ({ tempFilePaths,tempFiles }) => {
  86. console.log(tempFiles)
  87. const newList = JSON.parse(JSON.stringify(this.value || [])).concat(
  88. // tempFilePaths//.map(t => ({ path: t, type: this.getFileExt(t) }))
  89. tempFilePaths.map((t,i) => ({ path: t, type: tempFiles[i].type,size:tempFiles[i].size }))
  90. )
  91. this.$emit('input', newList)
  92. this.$emit('change', newList)
  93. this.$emit('add')
  94. }
  95. })
  96. // #endif
  97. },
  98. async fileClick(index) {
  99. const { path, type, uid, size = 0 } = this.value[index]
  100. if (this.isImgFile(this.value[index])) {
  101. uni.previewImage({ urls: [path], current: path })
  102. } else if (this.isDocFile(this.value[index])) {
  103. // #ifndef H5 || MP-DINGTALK
  104. if (size >= 50 * 1024 * 1024) {
  105. this.TOAST('小程序端无法下载超过50MB的文件,此文件大小为${size}KB,超过限制')
  106. return
  107. }
  108. // #endif
  109. // #ifndef MP-DINGTALK
  110. const tempFilePath = await this.HTTP_DOWNLOAD(uid)
  111. uni.openDocument({ filePath: tempFilePath, fileType: type })
  112. // #endif
  113. // #ifdef MP-DINGTALK
  114. this.TOAST('钉钉小程序只支持查看图片文件')
  115. // #endif
  116. } else {
  117. // #ifndef MP-DINGTALK
  118. this.TOAST('小程序端只支持打开图片和文档(word、pdf等)文件')
  119. // #endif
  120. // #ifdef MP-DINGTALK
  121. this.TOAST('钉钉小程序只支持查看图片文件')
  122. // #endif
  123. // #ifdef APP-VUE
  124. const tempFilePath = await this.HTTP_DOWNLOAD(uid)
  125. uni.openDocument({ filePath: tempFilePath, fileType: type })
  126. // #endif
  127. // #ifdef H5
  128. await this.HTTP_DOWNLOAD(uid)
  129. // #endif
  130. }
  131. }
  132. }
  133. }
  134. </script>
  135. <style lang="less" scoped>
  136. .file-icon {
  137. line-height: 100%;
  138. position: static;
  139. }
  140. </style>