Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.

Commit 7279ac1

Browse files
committed
feat: 增加 beforeRemove 属性
1 parent 44d2cb5 commit 7279ac1

File tree

3 files changed

+124
-92
lines changed

3 files changed

+124
-92
lines changed

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ npm install vue-ele-upload-file --save
1616
### 用法
1717

1818
```js
19-
import EleUploadFile from 'vue-ele-upload-file'
19+
import EleUploadFile from "vue-ele-upload-file";
2020

2121
export default {
2222
components: {
2323
EleUploadFile
2424
}
25-
}
25+
};
2626
```
2727

2828
## 示例
@@ -42,7 +42,7 @@ export default {
4242
data() {
4343
return {
4444
file: []
45-
}
45+
};
4646
},
4747
methods: {
4848
// 对请求结果处理, 返回对象
@@ -51,10 +51,10 @@ export default {
5151
url: URL.createObjectURL(file.raw),
5252
name: file.name,
5353
size: file.size
54-
}
54+
};
5555
}
5656
}
57-
}
57+
};
5858
</script>
5959
```
6060

@@ -128,6 +128,14 @@ export default {
128128
type: Boolean,
129129
default: true
130130
},
131+
// 是否显示上传成功的提示
132+
isShowSuccessTip: {
133+
type: Boolean,
134+
default: true
135+
},
136+
// 删除前的操作
137+
// 同 element-ui upload 组件
138+
beforeRemove: Function
131139
// 设置上传的请求头部
132140
// 同 element-ui upload 组件
133141
headers: Object,

lib/EleUploadFile.vue

Lines changed: 110 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,17 @@
2222
v-if="!disabled"
2323
>
2424
<!-- 上传按钮 -->
25-
<el-button
26-
size="medium"
27-
type="primary"
28-
>{{btnText}}</el-button>
25+
<el-button size="medium" type="primary">{{ btnText }}</el-button>
2926
<!-- 上传提示 -->
30-
<div
31-
class="el-upload__tip"
32-
slot="tip"
33-
v-if="showTip"
34-
>
27+
<div class="el-upload__tip" slot="tip" v-if="showTip">
3528
请上传
3629
<template v-if="fileSize">
3730
大小不超过
38-
<b style="color: #F56C6C">{{fileSize}}MB</b>
31+
<b style="color: #f56c6c;">{{ fileSize }}MB</b>
3932
</template>
4033
<template v-if="fileType">
4134
格式为
42-
<b style="color: #F56C6C">{{fileType.join('/')}}</b>
35+
<b style="color: #f56c6c;">{{ fileType.join("/") }}</b>
4336
</template>
4437
的文件
4538
</div>
@@ -59,11 +52,11 @@
5952
</template>
6053

6154
<script>
62-
import EleUploadList from './EleUploadList'
55+
import EleUploadList from "./EleUploadList";
6356
export default {
64-
name: 'EleUploadFile',
57+
name: "EleUploadFile",
6558
components: {
66-
EleUploadList
59+
EleUploadList,
6760
},
6861
props: {
6962
//
@@ -72,7 +65,7 @@ export default {
7265
// 同 element-ui upload 组件
7366
action: {
7467
type: String,
75-
required: true
68+
required: true,
7669
},
7770
// 大小限制(MB)
7871
fileSize: Number,
@@ -87,32 +80,37 @@ export default {
8780
// 是否显示文件大小
8881
isShowSize: {
8982
type: Boolean,
90-
default: true
83+
default: true,
9184
},
9285
// 是否显示下载
9386
isCanDownload: {
9487
type: Boolean,
95-
default: true
88+
default: true,
9689
},
9790
// 是否可删除
9891
isCanDelete: {
9992
type: Boolean,
100-
default: true
93+
default: true,
10194
},
10295
// 是否显示删除确认弹窗
10396
isShowDeleteConfim: {
10497
type: Boolean,
105-
default: true
98+
default: true,
10699
},
107100
// 是否可上传相同文件
108101
isCanUploadSame: {
109102
type: Boolean,
110-
default: true
103+
default: true,
111104
},
112105
// 是否显示提示
113106
isShowTip: {
114107
type: Boolean,
115-
default: true
108+
default: true,
109+
},
110+
// 是否显示上传成功的提示
111+
isShowSuccessTip: {
112+
type: Boolean,
113+
default: true,
116114
},
117115
// 设置上传的请求头部
118116
// 同 element-ui upload 组件
@@ -121,7 +119,7 @@ export default {
121119
// 同 element-ui upload 组件
122120
multiple: {
123121
type: Boolean,
124-
default: true
122+
default: true,
125123
},
126124
// 上传时附带的额外参数
127125
// 同 element-ui upload 组件
@@ -130,147 +128,173 @@ export default {
130128
// 同 element-ui upload 组件
131129
name: {
132130
type: String,
133-
default: 'file'
131+
default: "file",
134132
},
135133
// 支持发送 cookie 凭证信息
136134
// 同 element-ui upload 组件
137135
withCredentials: {
138136
type: Boolean,
139-
default: false
137+
default: false,
140138
},
141139
// 接受上传的文件类型
142140
// 同 element-ui upload 组件
143141
accept: String,
144142
// 最大允许上传个数
145143
// 同 element-ui upload 组件
146-
limit: Number
144+
limit: Number,
145+
// 删除前的操作
146+
// 同 element-ui upload 组件
147+
beforeRemove: Function,
147148
},
148-
data () {
149+
data() {
149150
return {
150-
fileList: []
151-
}
151+
fileList: [],
152+
};
152153
},
153154
computed: {
154155
// 按钮文本
155-
btnText () {
156+
btnText() {
156157
if (this.placeholder) {
157-
return this.placeholder
158+
return this.placeholder;
158159
} else {
159160
if (this.multiple) {
160-
return '上传文件'
161+
return "上传文件";
161162
} else {
162-
return '上传单个文件'
163+
return "上传单个文件";
163164
}
164165
}
165166
},
166167
// 是否显示提示
167-
showTip () {
168-
return this.isShowTip && (this.fileType || this.fileSize)
168+
showTip() {
169+
return this.isShowTip && (this.fileType || this.fileSize);
169170
},
170171
// 列表
171-
list () {
172-
let temp = 1
172+
list() {
173+
let temp = 1;
173174
if (this.value) {
174175
// 首先将值转为数组
175-
const list = Array.isArray(this.value) ? this.value : [this.value]
176+
const list = Array.isArray(this.value) ? this.value : [this.value];
176177
// 然后将数组转为对象数组
177178
return list.map((item) => {
178-
if (typeof item === 'string') {
179-
item = { name: item, url: item }
179+
if (typeof item === "string") {
180+
item = { name: item, url: item };
180181
}
181-
item.uid = item.uid || new Date().getTime() + temp++
182-
return item
183-
})
182+
item.uid = item.uid || new Date().getTime() + temp++;
183+
return item;
184+
});
184185
} else {
185-
return []
186+
return [];
186187
}
187-
}
188+
},
188189
},
189190
methods: {
190191
// 文件改变
191-
handleChange (file, fileList) {
192-
this.fileList = fileList
192+
handleChange(file, fileList) {
193+
this.fileList = fileList;
193194
},
194195
// 上传前校检格式和大小
195-
handleBeforeUpload (file) {
196+
handleBeforeUpload(file) {
196197
// 校检文件类型
197198
if (this.fileType) {
198-
let fileExtension = ''
199-
if (file.name.lastIndexOf('.') > -1) {
200-
fileExtension = file.name.slice(file.name.lastIndexOf('.') + 1)
199+
let fileExtension = "";
200+
if (file.name.lastIndexOf(".") > -1) {
201+
fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
201202
}
202203
const isTypeOk = this.fileType.some((type) => {
203-
if (file.type.indexOf(type) > -1) return true
204-
if (fileExtension && fileExtension.indexOf(type) > -1) return true
205-
return false
206-
})
204+
if (file.type.indexOf(type) > -1) return true;
205+
if (fileExtension && fileExtension.indexOf(type) > -1) return true;
206+
return false;
207+
});
207208
208209
if (!isTypeOk) {
209-
this.$message.error(`文件格式不正确, 请上传${this.fileType.join('/')}格式文件!`)
210-
return false
210+
this.$message.error(
211+
`文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`
212+
);
213+
return false;
211214
}
212215
}
213216
214217
// 校检文件大小
215218
if (this.fileSize) {
216-
const isLt = file.size / 1024 / 1024 < this.fileSize
219+
const isLt = file.size / 1024 / 1024 < this.fileSize;
217220
if (!isLt) {
218-
this.$message.error(`上传文件大小不能超过 ${this.fileSize} MB!`)
219-
return false
221+
this.$message.error(`上传文件大小不能超过 ${this.fileSize} MB!`);
222+
return false;
220223
}
221224
}
222225
223226
// 校检相同文件
224227
if (!this.isCanUploadSame) {
225-
const isSame = this.list.some((item) => item.name + item.size === file.name + file.size)
228+
const isSame = this.list.some(
229+
(item) => item.name + item.size === file.name + file.size
230+
);
226231
if (isSame) {
227-
this.$message.error(`此文件已上传!`)
228-
return false
232+
this.$message.error(`此文件已上传!`);
233+
return false;
229234
}
230235
}
231-
return true
236+
return true;
232237
},
233238
// 文件个数超出
234-
handleExceed () {
235-
this.$message.error(`最多上传${this.limit}个文件`)
239+
handleExceed() {
240+
this.$message.error(`最多上传${this.limit}个文件`);
236241
},
237242
// 上传失败
238-
handleUploadError (err) {
239-
this.$message.error('上传失败, 请重试')
240-
this.$emit('error', err)
243+
handleUploadError(err) {
244+
this.$message.error("上传失败, 请重试");
245+
this.$emit("error", err);
241246
},
242247
// 上传成功回调
243-
handleUploadSuccess (response, file) {
244-
this.$message.success('上传成功')
248+
handleUploadSuccess(response, file) {
249+
if (this.isShowSuccessTip) {
250+
this.$message.success("上传成功");
251+
}
245252
if (this.responseFn) {
246-
response = this.responseFn(response, file, this.list)
253+
response = this.responseFn(response, file, this.list);
247254
}
248255
if (this.multiple) {
249-
this.$emit('input', [...this.list, response])
256+
this.$emit("input", [...this.list, response]);
250257
} else {
251-
this.$emit('input', response)
258+
this.$emit("input", response);
252259
}
253260
254261
// 上传成功
255-
this.$emit('success', response, this.list)
262+
this.$emit("success", response, this.list);
263+
},
264+
handleRemove(index) {
265+
if (!this.beforeRemove) {
266+
this.doRemove(index);
267+
} else if (typeof this.beforeRemove === "function") {
268+
const before = this.beforeRemove(this.list[index], this.list);
269+
if (before && before.then) {
270+
before.then(
271+
() => {
272+
this.doRemove(index);
273+
},
274+
() => {}
275+
);
276+
} else if (before !== false) {
277+
this.doRemove(index);
278+
}
279+
}
256280
},
257281
// 删除
258-
handleRemove (index) {
259-
this.$emit('remove', this.list[index], this.list)
260-
this.fileList.splice(index, 1)
282+
doRemove(index) {
283+
this.$emit("remove", this.list[index], this.list);
284+
this.fileList.splice(index, 1);
261285
if (this.multiple) {
262-
const data = [...this.list]
263-
data.splice(index, 1)
264-
this.$emit('input', data || [])
286+
const data = [...this.list];
287+
data.splice(index, 1);
288+
this.$emit("input", data || []);
265289
} else {
266-
this.$emit('input', null)
290+
this.$emit("input", null);
267291
}
268-
}
292+
},
269293
},
270-
created () {
271-
this.fileList = this.list
272-
}
273-
}
294+
created() {
295+
this.fileList = this.list;
296+
},
297+
};
274298
</script>
275299

276300
<style>

0 commit comments

Comments
 (0)