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.
 
 
 
 
 
 

157 line
4.5 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.type)"
  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.type)" @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(type) {
  49. const typeString = (type || '').toLowerCase()
  50. //return ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'image'].includes(typeString)
  51. return true;
  52. },
  53. isDocFile(type) {
  54. const typeString = (type || '').toLowerCase()
  55. return ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf'].includes(typeString)
  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.chooseFile({
  82. count: Number(this.number),
  83. sizeType: ['original', 'compressed'],
  84. sourceType: ['album', 'camera'],
  85. success: ({ tempFilePaths }) => {
  86. const newList = JSON.parse(JSON.stringify(this.value || [])).concat(
  87. tempFilePaths//.map(t => ({ path: t, type: this.getFileExt(t) }))
  88. )
  89. this.$emit('input', newList)
  90. this.$emit('change', newList)
  91. this.$emit('add')
  92. }
  93. })
  94. // #endif
  95. },
  96. async fileClick(index) {
  97. const { path, type, uid, size = 0 } = this.value[index]
  98. if (this.isImgFile(type)) {
  99. uni.previewImage({ urls: [path], current: path })
  100. } else if (this.isDocFile(type)) {
  101. // #ifndef H5 || MP-DINGTALK
  102. if (size >= 50 * 1024 * 1024) {
  103. this.TOAST('小程序端无法下载超过50MB的文件,此文件大小为${size}KB,超过限制')
  104. return
  105. }
  106. // #endif
  107. // #ifndef MP-DINGTALK
  108. const tempFilePath = await this.HTTP_DOWNLOAD(uid)
  109. uni.openDocument({ filePath: tempFilePath, fileType: type })
  110. // #endif
  111. // #ifdef MP-DINGTALK
  112. this.TOAST('钉钉小程序只支持查看图片文件')
  113. // #endif
  114. } else {
  115. // #ifndef MP-DINGTALK
  116. this.TOAST('小程序端只支持打开图片和文档(word、pdf等)文件')
  117. // #endif
  118. // #ifdef MP-DINGTALK
  119. this.TOAST('钉钉小程序只支持查看图片文件')
  120. // #endif
  121. // #ifdef APP-VUE
  122. const tempFilePath = await this.HTTP_DOWNLOAD(uid)
  123. uni.openDocument({ filePath: tempFilePath, fileType: type })
  124. // #endif
  125. // #ifdef H5
  126. await this.HTTP_DOWNLOAD(uid)
  127. // #endif
  128. }
  129. }
  130. }
  131. }
  132. </script>
  133. <style lang="less" scoped>
  134. .file-icon {
  135. line-height: 100%;
  136. position: static;
  137. }
  138. </style>