UploadFile.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <template>
  2. <div class="upload-file">
  3. <el-upload
  4. ref="uploadRef"
  5. :multiple="props.limit > 1"
  6. name="file"
  7. v-model="valueRef"
  8. v-model:file-list="fileList"
  9. :show-file-list="true"
  10. :auto-upload="autoUpload"
  11. :action="updateUrl"
  12. :headers="uploadHeaders"
  13. :limit="props.limit"
  14. :drag="drag"
  15. :before-upload="beforeUpload"
  16. :on-exceed="handleExceed"
  17. :on-success="handleFileSuccess"
  18. :on-error="excelUploadError"
  19. :on-remove="handleRemove"
  20. :on-preview="handlePreview"
  21. class="upload-file-uploader"
  22. >
  23. <el-button type="primary"><Icon icon="ep:upload-filled" />选取文件</el-button>
  24. <template v-if="isShowTip" #tip>
  25. <div style="font-size: 8px">
  26. 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b>
  27. </div>
  28. <div style="font-size: 8px">
  29. 格式为 <b style="color: #f56c6c">{{ fileType.join('/') }}</b> 的文件
  30. </div>
  31. </template>
  32. </el-upload>
  33. </div>
  34. </template>
  35. <script lang="ts" setup>
  36. import { propTypes } from '@/utils/propTypes'
  37. import { getAccessToken, getTenantId } from '@/utils/auth'
  38. import type { UploadInstance, UploadUserFile, UploadProps, UploadRawFile } from 'element-plus'
  39. import { isArray, isString } from '@/utils/is'
  40. defineOptions({ name: 'UploadFile' })
  41. const message = useMessage() // 消息弹窗
  42. const emit = defineEmits(['update:modelValue'])
  43. const props = defineProps({
  44. modelValue: propTypes.oneOfType<string | string[]>([String, Array<String>]).isRequired,
  45. title: propTypes.string.def('文件上传'),
  46. updateUrl: propTypes.string.def(import.meta.env.VITE_UPLOAD_URL),
  47. fileType: propTypes.array.def(['doc', 'xls', 'ppt', 'txt', 'pdf']), // 文件类型, 例如['png', 'jpg', 'jpeg']
  48. fileSize: propTypes.number.def(5), // 大小限制(MB)
  49. limit: propTypes.number.def(5), // 数量限制
  50. autoUpload: propTypes.bool.def(true), // 自动上传
  51. drag: propTypes.bool.def(false), // 拖拽上传
  52. isShowTip: propTypes.bool.def(true) // 是否显示提示
  53. })
  54. // ========== 上传相关 ==========
  55. const valueRef = ref(props.modelValue)
  56. const uploadRef = ref<UploadInstance>()
  57. const uploadList = ref<UploadUserFile[]>([])
  58. const fileList = ref<UploadUserFile[]>([])
  59. const uploadNumber = ref<number>(0)
  60. const uploadHeaders = ref({
  61. Authorization: 'Bearer ' + getAccessToken(),
  62. 'tenant-id': getTenantId()
  63. })
  64. // 文件上传之前判断
  65. const beforeUpload: UploadProps['beforeUpload'] = (file: UploadRawFile) => {
  66. if (fileList.value.length >= props.limit) {
  67. message.error(`上传文件数量不能超过${props.limit}个!`)
  68. return false
  69. }
  70. let fileExtension = ''
  71. if (file.name.lastIndexOf('.') > -1) {
  72. fileExtension = file.name.slice(file.name.lastIndexOf('.') + 1)
  73. }
  74. const isImg = props.fileType.some((type: string) => {
  75. if (file.type.indexOf(type) > -1) return true
  76. return !!(fileExtension && fileExtension.indexOf(type) > -1)
  77. })
  78. const isLimit = file.size < props.fileSize * 1024 * 1024
  79. if (!isImg) {
  80. message.error(`文件格式不正确, 请上传${props.fileType.join('/')}格式!`)
  81. return false
  82. }
  83. if (!isLimit) {
  84. message.error(`上传文件大小不能超过${props.fileSize}MB!`)
  85. return false
  86. }
  87. message.success('正在上传文件,请稍候...')
  88. uploadNumber.value++
  89. }
  90. // 处理上传的文件发生变化
  91. // const handleFileChange = (uploadFile: UploadFile): void => {
  92. // uploadRef.value.data.path = uploadFile.name
  93. // }
  94. // 文件上传成功
  95. const handleFileSuccess: UploadProps['onSuccess'] = (res: any): void => {
  96. message.success('上传成功')
  97. const fileListNew = fileList.value
  98. fileListNew.pop()
  99. fileList.value = fileListNew
  100. uploadList.value.push({ name: res.data, url: res.data })
  101. if (uploadList.value.length == uploadNumber.value) {
  102. fileList.value = fileList.value.concat(uploadList.value)
  103. uploadList.value = []
  104. uploadNumber.value = 0
  105. emitUpdateModelValue()
  106. }
  107. }
  108. // 文件数超出提示
  109. const handleExceed: UploadProps['onExceed'] = (): void => {
  110. message.error(`上传文件数量不能超过${props.limit}个!`)
  111. }
  112. // 上传错误提示
  113. const excelUploadError: UploadProps['onError'] = (): void => {
  114. message.error('导入数据失败,请您重新上传!')
  115. }
  116. // 删除上传文件
  117. const handleRemove = (file) => {
  118. const findex = fileList.value.map((f) => f.name).indexOf(file.name)
  119. if (findex > -1) {
  120. fileList.value.splice(findex, 1)
  121. emitUpdateModelValue()
  122. }
  123. }
  124. const handlePreview: UploadProps['onPreview'] = (uploadFile) => {
  125. console.log(uploadFile)
  126. }
  127. // 监听模型绑定值变动
  128. watch(
  129. () => props.modelValue,
  130. () => {
  131. const files: string[] = []
  132. // 情况1:字符串
  133. if (isString(props.modelValue)) {
  134. // 情况1.1:逗号分隔的多值
  135. if (props.modelValue.includes(',')) {
  136. files.concat(props.modelValue.split(','))
  137. } else if (props.modelValue.length > 0) {
  138. files.push(props.modelValue)
  139. }
  140. } else if (isArray(props.modelValue)) {
  141. // 情况2:字符串
  142. files.concat(props.modelValue)
  143. } else {
  144. throw new Error('不支持的 modelValue 类型')
  145. }
  146. fileList.value = files.map((url: string) => {
  147. return { url, name: url.substring(url.lastIndexOf('/') + 1) } as UploadUserFile
  148. })
  149. },
  150. { immediate: true }
  151. )
  152. // 发送文件链接列表更新
  153. const emitUpdateModelValue = () => {
  154. // 情况1:数组结果
  155. let result: string | string[] = fileList.value.map((file) => file.url!)
  156. // 情况2:逗号分隔的字符串
  157. if (isString(props.modelValue)) {
  158. result = result.join(',')
  159. }
  160. emit('update:modelValue', result)
  161. }
  162. </script>
  163. <style scoped lang="scss">
  164. .upload-file-uploader {
  165. margin-bottom: 5px;
  166. }
  167. :deep(.upload-file-list .el-upload-list__item) {
  168. position: relative;
  169. margin-bottom: 10px;
  170. line-height: 2;
  171. border: 1px solid #e4e7ed;
  172. }
  173. :deep(.el-upload-list__item-file-name) {
  174. max-width: 250px;
  175. }
  176. :deep(.upload-file-list .ele-upload-list__item-content) {
  177. display: flex;
  178. justify-content: space-between;
  179. align-items: center;
  180. color: inherit;
  181. }
  182. :deep(.ele-upload-list__item-content-action .el-link) {
  183. margin-right: 10px;
  184. }
  185. </style>