2
2
<div class =" upload-file" >
3
3
<el-upload
4
4
ref =" uploadRef"
5
- :multiple =" props.limit > 1"
6
- name =" file"
7
- v-model =" valueRef"
8
5
v-model:file-list =" fileList"
9
- :show-file-list =" true"
10
- :auto-upload =" autoUpload"
11
6
:action =" updateUrl"
7
+ :auto-upload =" autoUpload"
8
+ :before-upload =" beforeUpload"
9
+ :drag =" drag"
12
10
:headers =" uploadHeaders"
13
11
:limit =" props.limit"
14
- :drag =" drag"
15
- :before-upload =" beforeUpload"
16
- :on-exceed =" handleExceed"
17
- :on-success =" handleFileSuccess"
12
+ :multiple =" props.limit > 1"
18
13
:on-error =" excelUploadError"
19
- :on-remove = " handleRemove "
14
+ :on-exceed = " handleExceed "
20
15
:on-preview =" handlePreview"
16
+ :on-remove =" handleRemove"
17
+ :on-success =" handleFileSuccess"
18
+ :show-file-list =" true"
21
19
class =" upload-file-uploader"
20
+ name =" file"
22
21
>
23
- <el-button type =" primary" ><Icon icon =" ep:upload-filled" />选取文件</el-button >
22
+ <el-button type =" primary" >
23
+ <Icon icon =" ep:upload-filled" />
24
+ 选取文件
25
+ </el-button >
24
26
<template v-if =" isShowTip " #tip >
25
27
<div style =" font-size : 8px " >
26
28
大小不超过 <b style =" color : #f56c6c " >{{ fileSize }}MB</b >
35
37
<script lang="ts" setup>
36
38
import { propTypes } from ' @/utils/propTypes'
37
39
import { getAccessToken , getTenantId } from ' @/utils/auth'
38
- import type { UploadInstance , UploadUserFile , UploadProps , UploadRawFile } from ' element-plus'
39
- import { isArray , isString } from ' @/utils/is'
40
+ import type { UploadInstance , UploadProps , UploadRawFile , UploadUserFile } from ' element-plus'
41
+ import { isString } from ' @/utils/is'
40
42
41
43
defineOptions ({ name: ' UploadFile' })
42
44
@@ -54,8 +56,8 @@ const props = defineProps({
54
56
drag: propTypes .bool .def (false ), // 拖拽上传
55
57
isShowTip: propTypes .bool .def (true ) // 是否显示提示
56
58
})
59
+
57
60
// ========== 上传相关 ==========
58
- const valueRef = ref (props .modelValue )
59
61
const uploadRef = ref <UploadInstance >()
60
62
const uploadList = ref <UploadUserFile []>([])
61
63
const fileList = ref <UploadUserFile []>([])
@@ -64,6 +66,7 @@ const uploadHeaders = ref({
64
66
Authorization: ' Bearer ' + getAccessToken (),
65
67
' tenant-id' : getTenantId ()
66
68
})
69
+
67
70
// 文件上传之前判断
68
71
const beforeUpload: UploadProps [' beforeUpload' ] = (file : UploadRawFile ) => {
69
72
if (fileList .value .length >= props .limit ) {
@@ -97,12 +100,10 @@ const beforeUpload: UploadProps['beforeUpload'] = (file: UploadRawFile) => {
97
100
// 文件上传成功
98
101
const handleFileSuccess: UploadProps [' onSuccess' ] = (res : any ): void => {
99
102
message .success (' 上传成功' )
100
- const fileListNew = fileList .value
101
- fileListNew .pop ()
102
- fileList .value = fileListNew
103
+ fileList .value .shift ()
103
104
uploadList .value .push ({ name: res .data , url: res .data })
104
105
if (uploadList .value .length == uploadNumber .value ) {
105
- fileList .value = fileList . value . concat ( uploadList .value )
106
+ fileList .value . push ( ... uploadList .value )
106
107
uploadList .value = []
107
108
uploadNumber .value = 0
108
109
emitUpdateModelValue ()
@@ -131,29 +132,25 @@ const handlePreview: UploadProps['onPreview'] = (uploadFile) => {
131
132
// 监听模型绑定值变动
132
133
watch (
133
134
() => props .modelValue ,
134
- () => {
135
- const files: string [] = []
135
+ (val : string | string []) => {
136
+ if (! val ) {
137
+ fileList .value = [] // fix:处理掉缓存,表单重置后上传组件的内容并没有重置
138
+ return
139
+ }
140
+
141
+ fileList .value = [] // 保障数据为空
136
142
// 情况1:字符串
137
- if (isString (props .modelValue )) {
138
- // 情况1.1:逗号分隔的多值
139
- if (props .modelValue .includes (' ,' )) {
140
- files .concat (props .modelValue .split (' ,' ))
141
- } else if (props .modelValue .length > 0 ) {
142
- files .push (props .modelValue )
143
- }
144
- } else if (isArray (props .modelValue )) {
145
- // 情况2:字符串
146
- files .concat (props .modelValue )
147
- } else if (props .modelValue == null ) {
148
- // 情况3:undefined 不处理
149
- } else {
150
- throw new Error (' 不支持的 modelValue 类型' )
143
+ if (isString (val )) {
144
+ fileList .value .push (
145
+ ... val .split (' ,' ).map ((url ) => ({ name: url .substring (url .lastIndexOf (' /' ) + 1 ), url }))
146
+ )
151
147
}
152
- fileList .value = files .map ((url : string ) => {
153
- return { url , name: url .substring (url .lastIndexOf (' /' ) + 1 ) } as UploadUserFile
154
- })
148
+ // 情况2:数组
149
+ fileList .value .push (
150
+ ... (val as string []).map ((url ) => ({ name: url .substring (url .lastIndexOf (' /' ) + 1 ), url }))
151
+ )
155
152
},
156
- { immediate: true }
153
+ { immediate: true , deep: true }
157
154
)
158
155
// 发送文件链接列表更新
159
156
const emitUpdateModelValue = () => {
@@ -166,7 +163,7 @@ const emitUpdateModelValue = () => {
166
163
emit (' update:modelValue' , result )
167
164
}
168
165
</script >
169
- <style scoped lang="scss">
166
+ <style lang="scss" scoped >
170
167
.upload-file-uploader {
171
168
margin-bottom : 5px ;
172
169
}
0 commit comments