@lambo-design/upload-file 1.0.0-beta.3 → 1.0.0-beta.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +1 -0
- package/package.json +13 -6
- package/src/index.vue +195 -58
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lambo-design/upload-file",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.30",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"author": "lambo",
|
|
@@ -9,11 +9,18 @@
|
|
|
9
9
|
"access": "public",
|
|
10
10
|
"registry": "https://registry.npmjs.org/"
|
|
11
11
|
},
|
|
12
|
-
"
|
|
13
|
-
"@lambo-design/core": "^4.7.1-beta",
|
|
14
|
-
"@lambo-design/shared": "^1.0.0-beta",
|
|
12
|
+
"devDependencies": {
|
|
15
13
|
"axios": "^0.24.0",
|
|
16
|
-
"axios-cache-plugin": "^0.1.0"
|
|
14
|
+
"axios-cache-plugin": "^0.1.0",
|
|
15
|
+
"@lambo-design/core": "^4.7.1-beta.162",
|
|
16
|
+
"@lambo-design/shared": "^1.0.0-beta.267"
|
|
17
17
|
},
|
|
18
|
-
"scripts": {
|
|
18
|
+
"scripts": {
|
|
19
|
+
"release-upload-file": "pnpm release-beta && git push --follow-tags && pnpm re-publish",
|
|
20
|
+
"release-major": "standard-version --release-as major",
|
|
21
|
+
"release-minor": "standard-version --release-as minor",
|
|
22
|
+
"release-patch": "standard-version --release-as patch",
|
|
23
|
+
"release-beta": "standard-version --prerelease beta",
|
|
24
|
+
"re-publish": "pnpm publish --access public --no-git-checks"
|
|
25
|
+
}
|
|
19
26
|
}
|
package/src/index.vue
CHANGED
|
@@ -1,31 +1,37 @@
|
|
|
1
1
|
<template>
|
|
2
2
|
<div class="upload-file-box">
|
|
3
|
-
<div class="upload-btn-box">
|
|
4
|
-
<
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
<
|
|
11
|
-
|
|
12
|
-
|
|
3
|
+
<div class="upload-btn-box" v-if="!disabled">
|
|
4
|
+
<slot>
|
|
5
|
+
<Button :size="buttonSize" icon="md-add" type="primary" :loading="loading"
|
|
6
|
+
@click="UploadFile()" ghost v-if="uploadBtn">
|
|
7
|
+
<span v-if="!loading">上传附件</span>
|
|
8
|
+
<span v-else>正在上传...</span>
|
|
9
|
+
</Button>
|
|
10
|
+
<a v-if="!uploadBtn" @click="UploadFile()">
|
|
11
|
+
<Icon type="md-add"></Icon>
|
|
12
|
+
上传附件
|
|
13
|
+
</a>
|
|
14
|
+
<span style="margin-left: 10px; color: #ed4014">
|
|
15
|
+
{{ descInfo }}
|
|
16
|
+
</span>
|
|
17
|
+
</slot>
|
|
13
18
|
</div>
|
|
14
|
-
<input type="file" :disabled="
|
|
19
|
+
<input type="file" :disabled="disabled" ref="uploadInput" style="position:absolute; clip:rect(0 0 0 0);display: none"
|
|
15
20
|
:multiple="multiple"
|
|
16
21
|
:accept="accept"
|
|
17
22
|
@change="readFile($event, 1)"/>
|
|
18
|
-
|
|
23
|
+
<div class="upload-file-list" v-if="showFileList && resultList.length > 0">
|
|
19
24
|
<List border size="small">
|
|
20
|
-
<ListItem v-for="item in resultList" :key="item.fileCode">
|
|
25
|
+
<ListItem v-for="item in resultList" :key="item.fileId ? item.fileId : item.fileCode">
|
|
21
26
|
<div span="21">
|
|
22
|
-
<a :href="item.fileUrl">
|
|
23
|
-
{{item.fileName}}
|
|
24
|
-
<span class="size" style="margin-left: 5px" v-if="item.showSize">({{item.showSize}})</span>
|
|
27
|
+
<a :href="item.fileUrl ? item.fileUrl : downloadUrl + item.fileId ? item.fileId : item.fileCode">
|
|
28
|
+
{{ item.fileName }}
|
|
29
|
+
<span class="size" style="margin-left: 5px" v-if="item.showSize">({{ item.showSize }})</span>
|
|
25
30
|
</a>
|
|
26
31
|
</div>
|
|
27
32
|
<div span="3" style="text-align: right">
|
|
28
|
-
<Button @click="
|
|
33
|
+
<Button v-if="showPreview" @click="previewFile(item)" type="text">预览</Button>
|
|
34
|
+
<Button v-if="!disabled" @click="deleteFile(item.fileId ? item.fileId : item.fileCode)" type="text">删除</Button>
|
|
29
35
|
</div>
|
|
30
36
|
</ListItem>
|
|
31
37
|
</List>
|
|
@@ -34,8 +40,9 @@
|
|
|
34
40
|
</template>
|
|
35
41
|
|
|
36
42
|
<script>
|
|
37
|
-
import
|
|
38
|
-
import
|
|
43
|
+
import ajax from "@lambo-design/shared/utils/ajax";
|
|
44
|
+
import config from "@lambo-design/shared/config/config";
|
|
45
|
+
import {formatterSizeUnit} from "@lambo-design/shared/utils/file";
|
|
39
46
|
|
|
40
47
|
export default {
|
|
41
48
|
components: {},
|
|
@@ -44,12 +51,18 @@ export default {
|
|
|
44
51
|
accept: {//txt,word,excel
|
|
45
52
|
type: String,
|
|
46
53
|
required: false,
|
|
47
|
-
default: "text/plain,application/msword,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
|
54
|
+
default: "application/pdf,text/plain,application/msword,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/zip,.rar"
|
|
48
55
|
},
|
|
49
56
|
fileSuffix: {
|
|
50
57
|
type: String,
|
|
51
58
|
required: false,
|
|
52
|
-
default: "pdf|doc|docx|txt|xls|xlsx|
|
|
59
|
+
default: "pdf|doc|docx|txt|xls|xlsx|zip|rar"
|
|
60
|
+
},
|
|
61
|
+
//描述信息
|
|
62
|
+
descInfo: {
|
|
63
|
+
type: String,
|
|
64
|
+
required: false,
|
|
65
|
+
default: ""
|
|
53
66
|
},
|
|
54
67
|
//标题
|
|
55
68
|
title: {
|
|
@@ -62,11 +75,28 @@ export default {
|
|
|
62
75
|
type: Boolean,
|
|
63
76
|
default: false
|
|
64
77
|
},
|
|
78
|
+
//是否禁用
|
|
79
|
+
disabled: {
|
|
80
|
+
type: Boolean,
|
|
81
|
+
default: false
|
|
82
|
+
},
|
|
83
|
+
//上传图片最大限制 单位:MB
|
|
84
|
+
fileMaxSize: {
|
|
85
|
+
type: Number,
|
|
86
|
+
required: false,
|
|
87
|
+
default: null
|
|
88
|
+
},
|
|
89
|
+
//限制上传数量
|
|
90
|
+
limitFileNum: {
|
|
91
|
+
type: Number,
|
|
92
|
+
required: false,
|
|
93
|
+
default: 0
|
|
94
|
+
},
|
|
65
95
|
//是否按钮形式
|
|
66
96
|
uploadBtn: {
|
|
67
97
|
type: Boolean,
|
|
68
98
|
required: false,
|
|
69
|
-
default:
|
|
99
|
+
default: true
|
|
70
100
|
},
|
|
71
101
|
//是否展示文件列表
|
|
72
102
|
showFileList: {
|
|
@@ -74,12 +104,26 @@ export default {
|
|
|
74
104
|
required: false,
|
|
75
105
|
default: true
|
|
76
106
|
},
|
|
107
|
+
//是否显示预览文件按钮
|
|
108
|
+
showPreview: {
|
|
109
|
+
type: Boolean,
|
|
110
|
+
required: false,
|
|
111
|
+
default: false
|
|
112
|
+
},
|
|
77
113
|
//其他需要传递的参数
|
|
78
114
|
otherParam: {//其他需要传递的参数
|
|
79
115
|
type: String,
|
|
80
116
|
required: false,
|
|
81
117
|
default: ""
|
|
82
118
|
},
|
|
119
|
+
//文件编码列表
|
|
120
|
+
fileIdList: {
|
|
121
|
+
type: Array,
|
|
122
|
+
required: false,
|
|
123
|
+
default: () => {
|
|
124
|
+
return []
|
|
125
|
+
}
|
|
126
|
+
},
|
|
83
127
|
//文件列表
|
|
84
128
|
fileList: {
|
|
85
129
|
type: Array,
|
|
@@ -91,17 +135,27 @@ export default {
|
|
|
91
135
|
//上传文件上下文
|
|
92
136
|
ossServerContext: {
|
|
93
137
|
type: String,
|
|
94
|
-
default:
|
|
138
|
+
default: config.ossServerContext
|
|
95
139
|
},
|
|
96
140
|
//上传文件Url
|
|
97
|
-
ossFilePutUrl:{
|
|
141
|
+
ossFilePutUrl: {
|
|
98
142
|
type: String,
|
|
99
143
|
default: "/oss/file/put"
|
|
100
144
|
},
|
|
101
145
|
//获取文件url
|
|
102
|
-
ossFileGetUrl:{
|
|
103
|
-
type:String,
|
|
104
|
-
default:"/oss/file/get/"
|
|
146
|
+
ossFileGetUrl: {
|
|
147
|
+
type: String,
|
|
148
|
+
default: "/oss/file/get/"
|
|
149
|
+
},
|
|
150
|
+
//文件预览所需文件流服务host
|
|
151
|
+
fileOnlinePreviewHost: {
|
|
152
|
+
type: String,
|
|
153
|
+
default: "http://pub-manage-server:8080"
|
|
154
|
+
},
|
|
155
|
+
//预览文件url
|
|
156
|
+
ossFilePreviewUrl: {
|
|
157
|
+
type: String,
|
|
158
|
+
default: "/anon/oss/file/getObjToOutputStream/"
|
|
105
159
|
}
|
|
106
160
|
},
|
|
107
161
|
|
|
@@ -112,7 +166,7 @@ export default {
|
|
|
112
166
|
loading: false,
|
|
113
167
|
attachmentFile: {
|
|
114
168
|
// fileUrl:"",
|
|
115
|
-
|
|
169
|
+
fileId: "",
|
|
116
170
|
fileType: "",
|
|
117
171
|
fileName: "",
|
|
118
172
|
size: ""
|
|
@@ -120,21 +174,40 @@ export default {
|
|
|
120
174
|
resultList: []
|
|
121
175
|
}
|
|
122
176
|
},
|
|
177
|
+
computed: {
|
|
178
|
+
downloadUrl: function () {
|
|
179
|
+
return this.ossServerContext + this.ossFileGetUrl;
|
|
180
|
+
},
|
|
181
|
+
dynamicFileTypeAlertMessage() {
|
|
182
|
+
// 使用 fileSuffix 动态生成提示信息
|
|
183
|
+
const fileExtensions = this.fileSuffix.split('|').join(',');
|
|
184
|
+
return `文件类型必须是 ${fileExtensions} 中的一种`;
|
|
185
|
+
}
|
|
186
|
+
},
|
|
123
187
|
methods: {
|
|
124
188
|
UploadFile: function () {
|
|
125
|
-
this
|
|
189
|
+
if (this.limitFileNum !== 0 && this.resultList.length >= this.limitFileNum) {
|
|
190
|
+
alert('最多只能上传' + this.limitFileNum + '个文件');
|
|
191
|
+
} else {
|
|
192
|
+
this.$refs.uploadInput.click();
|
|
193
|
+
}
|
|
126
194
|
},
|
|
127
195
|
readFile: function (e, num) {
|
|
128
196
|
let self = this;
|
|
129
197
|
//读取文件
|
|
130
|
-
|
|
198
|
+
self.loading = true;
|
|
131
199
|
for (let file of e.target.files) {
|
|
132
|
-
|
|
133
|
-
var reg = new RegExp("\.(" + this.fileSuffix + ")$", "i");
|
|
200
|
+
const reg = new RegExp("\.(" + this.fileSuffix + ")$", "i");
|
|
134
201
|
if (!reg.test(file.name)) {
|
|
135
|
-
alert(
|
|
202
|
+
alert(self.dynamicFileTypeAlertMessage);
|
|
203
|
+
this.$refs.uploadInput.value = null; // 清空文件输入框 self.loading = false;
|
|
136
204
|
return false;
|
|
137
205
|
} else {
|
|
206
|
+
if (this.fileMaxSize && file.size > this.fileMaxSize * 1024 * 1024) {
|
|
207
|
+
self.$Message.error("上传文件大小需要小于" + this.fileMaxSize + "MB");
|
|
208
|
+
self.loading = false;
|
|
209
|
+
return
|
|
210
|
+
}
|
|
138
211
|
self.attachmentFile.fileName = file;
|
|
139
212
|
if (self.attachmentFile.fileName.name.indexOf("/") > 0) {
|
|
140
213
|
self.attachmentFile.fileName = self.attachmentFile.fileName.name.substring(self.attachmentFile.fileName.name.lastIndexOf("/") + 1);
|
|
@@ -144,42 +217,39 @@ export default {
|
|
|
144
217
|
}
|
|
145
218
|
}
|
|
146
219
|
self.attachmentFile.fileType = self.attachmentFile.fileName.name.substring(self.attachmentFile.fileName.name.lastIndexOf(".") + 1);
|
|
147
|
-
|
|
148
220
|
}
|
|
149
|
-
self.
|
|
150
|
-
self.loadtoServer();
|
|
221
|
+
self.doUpload();
|
|
151
222
|
},
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
if (afile.size == 0) {
|
|
223
|
+
doUpload: function () {
|
|
224
|
+
let self = this;
|
|
225
|
+
const allFile = this.$refs.uploadInput.files;
|
|
226
|
+
if (allFile.size === 0) {
|
|
157
227
|
alert("不能传入空文件");
|
|
228
|
+
self.loading = false;
|
|
158
229
|
return false;
|
|
159
230
|
}
|
|
160
231
|
let formData = new FormData();
|
|
161
|
-
for (let file of
|
|
232
|
+
for (let file of allFile) {
|
|
162
233
|
formData.append('file', file);
|
|
163
234
|
}
|
|
164
235
|
let resultList = [];
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
if (response.data.code
|
|
169
|
-
|
|
236
|
+
const uploadUrl = self.ossServerContext + self.ossFilePutUrl;
|
|
237
|
+
const downloadUrl = self.ossServerContext + self.ossFileGetUrl;
|
|
238
|
+
ajax.post(uploadUrl, formData, {payload: true}).then(response => {
|
|
239
|
+
if (response.data.code === 1) {
|
|
240
|
+
const result = response.data.data;
|
|
170
241
|
self.loading = false;
|
|
171
242
|
if (result.length >= 1) {
|
|
172
243
|
result.forEach(item => {
|
|
173
|
-
|
|
244
|
+
const tmp = {
|
|
174
245
|
fileUrl: downloadUrl + item.fileId,
|
|
175
|
-
|
|
246
|
+
fileId: item.fileId,
|
|
176
247
|
fileName: item.fileName,
|
|
177
248
|
fileType: item.fileName.substring(item.fileName.lastIndexOf(".") + 1),
|
|
178
249
|
size: item.length,
|
|
179
250
|
showSize: formatterSizeUnit(item.length),
|
|
180
251
|
otherParam: self.otherParam
|
|
181
252
|
};
|
|
182
|
-
|
|
183
253
|
resultList.push(tmp);
|
|
184
254
|
self.resultList.push(tmp);
|
|
185
255
|
});
|
|
@@ -199,12 +269,35 @@ export default {
|
|
|
199
269
|
self.$refs.uploadInput.value = null
|
|
200
270
|
});
|
|
201
271
|
},
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
272
|
+
|
|
273
|
+
encode64(value) {
|
|
274
|
+
let typeValue = typeof value;
|
|
275
|
+
if (typeValue === 'string' || typeValue === 'number') {
|
|
276
|
+
return window.btoa(
|
|
277
|
+
window.encodeURIComponent(value)
|
|
278
|
+
.replace(/%([0-9A-F]{2})/g,
|
|
279
|
+
function toSolidBytes(match, p1) {
|
|
280
|
+
return String.fromCharCode('0x' + p1);
|
|
281
|
+
}));
|
|
282
|
+
} else {
|
|
283
|
+
return value;
|
|
284
|
+
}
|
|
285
|
+
},
|
|
286
|
+
previewFile: function (item) {
|
|
287
|
+
if (item.fileId || item.fileCode) {
|
|
288
|
+
const url = this.fileOnlinePreviewHost + this.ossServerContext + this.ossFilePreviewUrl + item.fileId ? item.fileId : item.fileCode + "?fullfilename=" + item.fileName + "." + item.fileType;
|
|
289
|
+
window.open(config.fileOnlinePreviewContext + '/onlinePreview?url=' + encodeURIComponent(this.encode64(url)));
|
|
290
|
+
}
|
|
291
|
+
},
|
|
292
|
+
deleteFile: function (fileId) {
|
|
293
|
+
if (fileId) {
|
|
294
|
+
let result = [];
|
|
295
|
+
let deletes = []
|
|
206
296
|
this.resultList.forEach(item => {
|
|
207
|
-
if (item.
|
|
297
|
+
if (!item.fileId) {
|
|
298
|
+
item.fileId = item.fileCode;
|
|
299
|
+
}
|
|
300
|
+
if (item.fileId !== fileId) {
|
|
208
301
|
result.push(item);
|
|
209
302
|
} else {
|
|
210
303
|
deletes.push(item)
|
|
@@ -217,13 +310,57 @@ export default {
|
|
|
217
310
|
this.$emit("upload-result", this.resultList, deletes);
|
|
218
311
|
}
|
|
219
312
|
}
|
|
313
|
+
},
|
|
314
|
+
initFileList: function (value) {
|
|
315
|
+
if (this.resultList.length === 0) {
|
|
316
|
+
let requests = []
|
|
317
|
+
for (const fileId of value) {
|
|
318
|
+
requests.push(ajax.get(config.upmsServerContext + "/oss/file/getMetaData/" + fileId))
|
|
319
|
+
}
|
|
320
|
+
Promise.all(requests).then((response) => {
|
|
321
|
+
response.forEach(item => {
|
|
322
|
+
this.resultList.push({
|
|
323
|
+
fileId: item.data.data.fileId,
|
|
324
|
+
fileUrl: this.ossServerContext + this.ossFileGetUrl + item.data.data.fileId,
|
|
325
|
+
fileName: item.data.data.fileName,
|
|
326
|
+
fileType: item.data.data.fileName.substring(item.data.data.fileName.lastIndexOf(".") + 1),
|
|
327
|
+
size: item.data.data.length,
|
|
328
|
+
showSize: formatterSizeUnit(item.data.data.length),
|
|
329
|
+
otherParam: this.otherParam
|
|
330
|
+
})
|
|
331
|
+
})
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
},
|
|
335
|
+
arraysEqual(arr1, arr2) {
|
|
336
|
+
if (arr1.length !== arr2.length) return false;
|
|
337
|
+
for (let i = 0; i < arr1.length; i++) {
|
|
338
|
+
if (arr1[i] !== arr2[i]) return false;
|
|
339
|
+
}
|
|
340
|
+
return true;
|
|
220
341
|
}
|
|
221
342
|
},
|
|
222
343
|
watch: {
|
|
223
|
-
fileList:
|
|
224
|
-
|
|
225
|
-
|
|
344
|
+
fileList: {
|
|
345
|
+
handler: function (value) {
|
|
346
|
+
this.resultList = this.fileList
|
|
347
|
+
},
|
|
348
|
+
immediate:true,
|
|
349
|
+
deep: true
|
|
350
|
+
},
|
|
351
|
+
fileIdList: {
|
|
352
|
+
handler: function (newValue, oldValue) {
|
|
353
|
+
if (newValue.length > 0 && !this.arraysEqual(newValue, oldValue)) {
|
|
354
|
+
this.initFileList(newValue)
|
|
355
|
+
} else if (newValue.length === 0) {
|
|
356
|
+
this.resultList = []
|
|
357
|
+
}
|
|
358
|
+
},
|
|
359
|
+
deep: true
|
|
226
360
|
}
|
|
361
|
+
},
|
|
362
|
+
mounted() {
|
|
363
|
+
this.initFileList(this.fileIdList)
|
|
227
364
|
}
|
|
228
365
|
}
|
|
229
366
|
</script>
|