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.
 
 
 
 
 
 

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