|
|
@@ -4,7 +4,7 @@
|
|
4
|
4
|
:action="uploadFileUrl"
|
|
5
|
5
|
:before-upload="handleBeforeUpload"
|
|
6
|
6
|
:file-list="fileList"
|
|
7
|
|
- :limit="1"
|
|
|
7
|
+ :limit="limit"
|
|
8
|
8
|
:on-error="handleUploadError"
|
|
9
|
9
|
:on-exceed="handleExceed"
|
|
10
|
10
|
:on-success="handleUploadSuccess"
|
|
|
@@ -26,7 +26,7 @@
|
|
26
|
26
|
|
|
27
|
27
|
<!-- 文件列表 -->
|
|
28
|
28
|
<transition-group class="upload-file-list el-upload-list el-upload-list--text" name="el-fade-in-linear" tag="ul">
|
|
29
|
|
- <li :key="file.uid" class="el-upload-list__item ele-upload-list__item-content" v-for="(file, index) in list">
|
|
|
29
|
+ <li :key="file.uid" class="el-upload-list__item ele-upload-list__item-content" v-for="(file, index) in fileList">
|
|
30
|
30
|
<el-link :href="file.url" :underline="false" target="_blank">
|
|
31
|
31
|
<span class="el-icon-document"> {{ getFileName(file.name) }} </span>
|
|
32
|
32
|
</el-link>
|
|
|
@@ -42,9 +42,15 @@
|
|
42
|
42
|
import { getToken } from "@/utils/auth";
|
|
43
|
43
|
|
|
44
|
44
|
export default {
|
|
|
45
|
+ name: "FileUpload",
|
|
45
|
46
|
props: {
|
|
46
|
47
|
// 值
|
|
47
|
48
|
value: [String, Object, Array],
|
|
|
49
|
+ // 数量限制
|
|
|
50
|
+ limit: {
|
|
|
51
|
+ type: Number,
|
|
|
52
|
+ default: 5,
|
|
|
53
|
+ },
|
|
48
|
54
|
// 大小限制(MB)
|
|
49
|
55
|
fileSize: {
|
|
50
|
56
|
type: Number,
|
|
|
@@ -70,30 +76,35 @@ export default {
|
|
70
|
76
|
fileList: [],
|
|
71
|
77
|
};
|
|
72
|
78
|
},
|
|
|
79
|
+ watch: {
|
|
|
80
|
+ value: {
|
|
|
81
|
+ handler(val) {
|
|
|
82
|
+ if (val) {
|
|
|
83
|
+ let temp = 1;
|
|
|
84
|
+ // 首先将值转为数组
|
|
|
85
|
+ const list = Array.isArray(val) ? val : this.value.split(',');
|
|
|
86
|
+ // 然后将数组转为对象数组
|
|
|
87
|
+ this.fileList = list.map(item => {
|
|
|
88
|
+ if (typeof item === "string") {
|
|
|
89
|
+ item = { name: item, url: item };
|
|
|
90
|
+ }
|
|
|
91
|
+ item.uid = item.uid || new Date().getTime() + temp++;
|
|
|
92
|
+ return item;
|
|
|
93
|
+ });
|
|
|
94
|
+ } else {
|
|
|
95
|
+ this.fileList = [];
|
|
|
96
|
+ return [];
|
|
|
97
|
+ }
|
|
|
98
|
+ },
|
|
|
99
|
+ deep: true,
|
|
|
100
|
+ immediate: true
|
|
|
101
|
+ }
|
|
|
102
|
+ },
|
|
73
|
103
|
computed: {
|
|
74
|
104
|
// 是否显示提示
|
|
75
|
105
|
showTip() {
|
|
76
|
106
|
return this.isShowTip && (this.fileType || this.fileSize);
|
|
77
|
107
|
},
|
|
78
|
|
- // 列表
|
|
79
|
|
- list() {
|
|
80
|
|
- let temp = 1;
|
|
81
|
|
- if (this.value) {
|
|
82
|
|
- // 首先将值转为数组
|
|
83
|
|
- const list = Array.isArray(this.value) ? this.value : [this.value];
|
|
84
|
|
- // 然后将数组转为对象数组
|
|
85
|
|
- return list.map((item) => {
|
|
86
|
|
- if (typeof item === "string") {
|
|
87
|
|
- item = { name: item, url: item };
|
|
88
|
|
- }
|
|
89
|
|
- item.uid = item.uid || new Date().getTime() + temp++;
|
|
90
|
|
- return item;
|
|
91
|
|
- });
|
|
92
|
|
- } else {
|
|
93
|
|
- this.fileList = [];
|
|
94
|
|
- return [];
|
|
95
|
|
- }
|
|
96
|
|
- },
|
|
97
|
108
|
},
|
|
98
|
109
|
methods: {
|
|
99
|
110
|
// 上传前校检格式和大小
|
|
|
@@ -126,7 +137,7 @@ export default {
|
|
126
|
137
|
},
|
|
127
|
138
|
// 文件个数超出
|
|
128
|
139
|
handleExceed() {
|
|
129
|
|
- this.$message.error(`只允许上传单个文件`);
|
|
|
140
|
+ this.$message.error(`上传文件数量不能超过 ${this.limit} 个!`);
|
|
130
|
141
|
},
|
|
131
|
142
|
// 上传失败
|
|
132
|
143
|
handleUploadError(err) {
|
|
|
@@ -135,12 +146,13 @@ export default {
|
|
135
|
146
|
// 上传成功回调
|
|
136
|
147
|
handleUploadSuccess(res, file) {
|
|
137
|
148
|
this.$message.success("上传成功");
|
|
138
|
|
- this.$emit("input", res.data.url);
|
|
|
149
|
+ this.fileList.push({ name: res.data.url, url: res.data.url });
|
|
|
150
|
+ this.$emit("input", this.listToString(this.fileList));
|
|
139
|
151
|
},
|
|
140
|
152
|
// 删除文件
|
|
141
|
153
|
handleDelete(index) {
|
|
142
|
154
|
this.fileList.splice(index, 1);
|
|
143
|
|
- this.$emit("input", '');
|
|
|
155
|
+ this.$emit("input", this.listToString(this.fileList));
|
|
144
|
156
|
},
|
|
145
|
157
|
// 获取文件名称
|
|
146
|
158
|
getFileName(name) {
|
|
|
@@ -149,11 +161,17 @@ export default {
|
|
149
|
161
|
} else {
|
|
150
|
162
|
return "";
|
|
151
|
163
|
}
|
|
|
164
|
+ },
|
|
|
165
|
+ // 对象转成指定字符串分隔
|
|
|
166
|
+ listToString(list, separator) {
|
|
|
167
|
+ let strs = "";
|
|
|
168
|
+ separator = separator || ",";
|
|
|
169
|
+ for (let i in list) {
|
|
|
170
|
+ strs += list[i].url + separator;
|
|
|
171
|
+ }
|
|
|
172
|
+ return strs != '' ? strs.substr(0, strs.length - 1) : '';
|
|
152
|
173
|
}
|
|
153
|
|
- },
|
|
154
|
|
- created() {
|
|
155
|
|
- this.fileList = this.list;
|
|
156
|
|
- },
|
|
|
174
|
+ }
|
|
157
|
175
|
};
|
|
158
|
176
|
</script>
|
|
159
|
177
|
|