@lambo-design/upload-file 1.0.0-beta.2 → 1.0.0-beta.20
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 +12 -5
- package/src/index.vue +138 -41
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.20",
|
|
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
|
-
"
|
|
12
|
+
"devDependencies": {
|
|
13
13
|
"axios": "^0.24.0",
|
|
14
14
|
"axios-cache-plugin": "^0.1.0",
|
|
15
|
-
"@lambo-design/core": "4.7.1-beta.
|
|
16
|
-
"@lambo-design/shared": "1.0.0-beta.
|
|
15
|
+
"@lambo-design/core": "^4.7.1-beta.139",
|
|
16
|
+
"@lambo-design/shared": "^1.0.0-beta.201"
|
|
17
17
|
},
|
|
18
|
-
"scripts": {
|
|
18
|
+
"scripts": {
|
|
19
|
+
"release": "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,10 +1,10 @@
|
|
|
1
1
|
<template>
|
|
2
2
|
<div class="upload-file-box">
|
|
3
3
|
<div class="upload-btn-box">
|
|
4
|
-
<Button :size="buttonSize" icon="md-
|
|
4
|
+
<Button :size="buttonSize" icon="md-add" type="primary" :loading="loading"
|
|
5
5
|
@click="UploadFile()" ghost v-if="uploadBtn">
|
|
6
|
-
<span v-if="!loading"
|
|
7
|
-
<span v-else
|
|
6
|
+
<span v-if="!loading">上传附件</span>
|
|
7
|
+
<span v-else>正在上传...</span>
|
|
8
8
|
</Button>
|
|
9
9
|
<a v-if="!uploadBtn" @click="UploadFile()">
|
|
10
10
|
<Icon type="md-add"></Icon>
|
|
@@ -15,16 +15,17 @@
|
|
|
15
15
|
:multiple="multiple"
|
|
16
16
|
:accept="accept"
|
|
17
17
|
@change="readFile($event, 1)"/>
|
|
18
|
-
|
|
18
|
+
<div class="upload-file-list" v-if="showFileList && resultList.length > 0">
|
|
19
19
|
<List border size="small">
|
|
20
20
|
<ListItem v-for="item in resultList" :key="item.fileCode">
|
|
21
21
|
<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>
|
|
22
|
+
<a :href="item.fileUrl ? item.fileUrl : downloadUrl + item.fileCode">
|
|
23
|
+
{{ item.fileName }}
|
|
24
|
+
<span class="size" style="margin-left: 5px" v-if="item.showSize">({{ item.showSize }})</span>
|
|
25
25
|
</a>
|
|
26
26
|
</div>
|
|
27
27
|
<div span="3" style="text-align: right">
|
|
28
|
+
<Button v-if="showPreview" @click="previewFile(item)" type="text">预览</Button>
|
|
28
29
|
<Button @click="deleteFile(item.fileCode)" type="text">删除</Button>
|
|
29
30
|
</div>
|
|
30
31
|
</ListItem>
|
|
@@ -34,8 +35,9 @@
|
|
|
34
35
|
</template>
|
|
35
36
|
|
|
36
37
|
<script>
|
|
37
|
-
import
|
|
38
|
-
import
|
|
38
|
+
import ajax from "@lambo-design/shared/utils/ajax";
|
|
39
|
+
import config from "@lambo-design/shared/config/config";
|
|
40
|
+
import {formatterSizeUnit} from "@lambo-design/shared/utils/file";
|
|
39
41
|
|
|
40
42
|
export default {
|
|
41
43
|
components: {},
|
|
@@ -62,11 +64,17 @@ export default {
|
|
|
62
64
|
type: Boolean,
|
|
63
65
|
default: false
|
|
64
66
|
},
|
|
67
|
+
//限制上传数量
|
|
68
|
+
limitFileNum: {
|
|
69
|
+
type: Number,
|
|
70
|
+
required: false,
|
|
71
|
+
default: 0
|
|
72
|
+
},
|
|
65
73
|
//是否按钮形式
|
|
66
74
|
uploadBtn: {
|
|
67
75
|
type: Boolean,
|
|
68
76
|
required: false,
|
|
69
|
-
default:
|
|
77
|
+
default: true
|
|
70
78
|
},
|
|
71
79
|
//是否展示文件列表
|
|
72
80
|
showFileList: {
|
|
@@ -74,12 +82,26 @@ export default {
|
|
|
74
82
|
required: false,
|
|
75
83
|
default: true
|
|
76
84
|
},
|
|
85
|
+
//是否显示预览文件按钮
|
|
86
|
+
showPreview: {
|
|
87
|
+
type: Boolean,
|
|
88
|
+
required: false,
|
|
89
|
+
default: false
|
|
90
|
+
},
|
|
77
91
|
//其他需要传递的参数
|
|
78
92
|
otherParam: {//其他需要传递的参数
|
|
79
93
|
type: String,
|
|
80
94
|
required: false,
|
|
81
95
|
default: ""
|
|
82
96
|
},
|
|
97
|
+
//文件编码列表
|
|
98
|
+
fileIdList: {
|
|
99
|
+
type: Array,
|
|
100
|
+
required: false,
|
|
101
|
+
default: () => {
|
|
102
|
+
return []
|
|
103
|
+
}
|
|
104
|
+
},
|
|
83
105
|
//文件列表
|
|
84
106
|
fileList: {
|
|
85
107
|
type: Array,
|
|
@@ -91,17 +113,27 @@ export default {
|
|
|
91
113
|
//上传文件上下文
|
|
92
114
|
ossServerContext: {
|
|
93
115
|
type: String,
|
|
94
|
-
default:
|
|
116
|
+
default: config.ossServerContext
|
|
95
117
|
},
|
|
96
118
|
//上传文件Url
|
|
97
|
-
ossFilePutUrl:{
|
|
119
|
+
ossFilePutUrl: {
|
|
98
120
|
type: String,
|
|
99
121
|
default: "/oss/file/put"
|
|
100
122
|
},
|
|
101
123
|
//获取文件url
|
|
102
|
-
ossFileGetUrl:{
|
|
103
|
-
type:String,
|
|
104
|
-
default:"/oss/file/get/"
|
|
124
|
+
ossFileGetUrl: {
|
|
125
|
+
type: String,
|
|
126
|
+
default: "/oss/file/get/"
|
|
127
|
+
},
|
|
128
|
+
//文件预览所需文件流服务host
|
|
129
|
+
fileOnlinePreviewHost: {
|
|
130
|
+
type: String,
|
|
131
|
+
default: "http://pub-manage-server:8080"
|
|
132
|
+
},
|
|
133
|
+
//预览文件url
|
|
134
|
+
ossFilePreviewUrl: {
|
|
135
|
+
type: String,
|
|
136
|
+
default: "/anon/oss/file/getObjToOutputStream/"
|
|
105
137
|
}
|
|
106
138
|
},
|
|
107
139
|
|
|
@@ -120,19 +152,28 @@ export default {
|
|
|
120
152
|
resultList: []
|
|
121
153
|
}
|
|
122
154
|
},
|
|
155
|
+
computed: {
|
|
156
|
+
downloadUrl: function () {
|
|
157
|
+
return this.ossServerContext + this.ossFileGetUrl;
|
|
158
|
+
}
|
|
159
|
+
},
|
|
123
160
|
methods: {
|
|
124
161
|
UploadFile: function () {
|
|
125
|
-
this
|
|
162
|
+
if (this.limitFileNum !== 0 && this.resultList.length >= this.limitFileNum) {
|
|
163
|
+
alert('最多只能上传' + this.limitFileNum + '个文件');
|
|
164
|
+
} else {
|
|
165
|
+
this.$refs.uploadInput.click();
|
|
166
|
+
}
|
|
126
167
|
},
|
|
127
168
|
readFile: function (e, num) {
|
|
128
169
|
let self = this;
|
|
129
170
|
//读取文件
|
|
130
|
-
|
|
171
|
+
self.loading = true;
|
|
131
172
|
for (let file of e.target.files) {
|
|
132
|
-
|
|
133
|
-
var reg = new RegExp("\.(" + this.fileSuffix + ")$", "i");
|
|
173
|
+
const reg = new RegExp("\.(" + this.fileSuffix + ")$", "i");
|
|
134
174
|
if (!reg.test(file.name)) {
|
|
135
175
|
alert('文件类型必须是doc,docx,txt,xls,xlsx,jpg,jpeg,png中的一种');
|
|
176
|
+
self.loading = false;
|
|
136
177
|
return false;
|
|
137
178
|
} else {
|
|
138
179
|
self.attachmentFile.fileName = file;
|
|
@@ -144,33 +185,31 @@ export default {
|
|
|
144
185
|
}
|
|
145
186
|
}
|
|
146
187
|
self.attachmentFile.fileType = self.attachmentFile.fileName.name.substring(self.attachmentFile.fileName.name.lastIndexOf(".") + 1);
|
|
147
|
-
|
|
148
188
|
}
|
|
149
|
-
self.
|
|
150
|
-
self.loadtoServer();
|
|
189
|
+
self.doUpload();
|
|
151
190
|
},
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
if (afile.size == 0) {
|
|
191
|
+
doUpload: function () {
|
|
192
|
+
let self = this;
|
|
193
|
+
const allFile = this.$refs.uploadInput.files;
|
|
194
|
+
if (allFile.size === 0) {
|
|
157
195
|
alert("不能传入空文件");
|
|
196
|
+
self.loading = false;
|
|
158
197
|
return false;
|
|
159
198
|
}
|
|
160
199
|
let formData = new FormData();
|
|
161
|
-
for (let file of
|
|
200
|
+
for (let file of allFile) {
|
|
162
201
|
formData.append('file', file);
|
|
163
202
|
}
|
|
164
203
|
let resultList = [];
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
if (response.data.code
|
|
169
|
-
|
|
204
|
+
const uploadUrl = self.ossServerContext + self.ossFilePutUrl;
|
|
205
|
+
const downloadUrl = self.ossServerContext + self.ossFileGetUrl;
|
|
206
|
+
ajax.post(uploadUrl, formData, {payload: true}).then(response => {
|
|
207
|
+
if (response.data.code === 1) {
|
|
208
|
+
const result = response.data.data;
|
|
170
209
|
self.loading = false;
|
|
171
210
|
if (result.length >= 1) {
|
|
172
211
|
result.forEach(item => {
|
|
173
|
-
|
|
212
|
+
const tmp = {
|
|
174
213
|
fileUrl: downloadUrl + item.fileId,
|
|
175
214
|
fileCode: item.fileId,
|
|
176
215
|
fileName: item.fileName,
|
|
@@ -179,7 +218,6 @@ export default {
|
|
|
179
218
|
showSize: formatterSizeUnit(item.length),
|
|
180
219
|
otherParam: self.otherParam
|
|
181
220
|
};
|
|
182
|
-
|
|
183
221
|
resultList.push(tmp);
|
|
184
222
|
self.resultList.push(tmp);
|
|
185
223
|
});
|
|
@@ -199,12 +237,32 @@ export default {
|
|
|
199
237
|
self.$refs.uploadInput.value = null
|
|
200
238
|
});
|
|
201
239
|
},
|
|
240
|
+
|
|
241
|
+
encode64(value) {
|
|
242
|
+
let typeValue = typeof value;
|
|
243
|
+
if (typeValue === 'string' || typeValue === 'number') {
|
|
244
|
+
return window.btoa(
|
|
245
|
+
window.encodeURIComponent(value)
|
|
246
|
+
.replace(/%([0-9A-F]{2})/g,
|
|
247
|
+
function toSolidBytes(match, p1) {
|
|
248
|
+
return String.fromCharCode('0x' + p1);
|
|
249
|
+
}));
|
|
250
|
+
} else {
|
|
251
|
+
return value;
|
|
252
|
+
}
|
|
253
|
+
},
|
|
254
|
+
previewFile: function (item) {
|
|
255
|
+
if (item.fileCode) {
|
|
256
|
+
const url = this.fileOnlinePreviewHost + this.ossServerContext + this.ossFilePreviewUrl + item.fileCode + "?fullfilename=" + item.fileName + "." + item.fileType;
|
|
257
|
+
window.open(config.fileOnlinePreviewContext + '/onlinePreview?url=' + encodeURIComponent(this.encode64(url)));
|
|
258
|
+
}
|
|
259
|
+
},
|
|
202
260
|
deleteFile: function (fileCode) {
|
|
203
261
|
if (fileCode) {
|
|
204
|
-
|
|
205
|
-
|
|
262
|
+
let result = [];
|
|
263
|
+
let deletes = []
|
|
206
264
|
this.resultList.forEach(item => {
|
|
207
|
-
if (item.fileCode
|
|
265
|
+
if (item.fileCode !== fileCode) {
|
|
208
266
|
result.push(item);
|
|
209
267
|
} else {
|
|
210
268
|
deletes.push(item)
|
|
@@ -217,12 +275,51 @@ export default {
|
|
|
217
275
|
this.$emit("upload-result", this.resultList, deletes);
|
|
218
276
|
}
|
|
219
277
|
}
|
|
278
|
+
},
|
|
279
|
+
initFileList: function (value) {
|
|
280
|
+
if (this.resultList.length === 0) {
|
|
281
|
+
let requests = []
|
|
282
|
+
for (const fileId of value) {
|
|
283
|
+
requests.push(ajax.get(config.upmsServerContext + "/oss/file/getMetaData/" + fileId))
|
|
284
|
+
}
|
|
285
|
+
Promise.all(requests).then((response) => {
|
|
286
|
+
response.forEach(item => {
|
|
287
|
+
this.resultList.push({
|
|
288
|
+
fileCode: item.data.data.fileId,
|
|
289
|
+
fileUrl: this.ossServerContext + this.ossFileGetUrl + item.data.data.fileId,
|
|
290
|
+
fileName: item.data.data.fileName,
|
|
291
|
+
fileType: item.data.data.fileName.substring(item.data.data.fileName.lastIndexOf(".") + 1),
|
|
292
|
+
size: item.data.data.length,
|
|
293
|
+
showSize: formatterSizeUnit(item.data.data.length),
|
|
294
|
+
otherParam: this.otherParam
|
|
295
|
+
})
|
|
296
|
+
})
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
},
|
|
300
|
+
arraysEqual(arr1, arr2) {
|
|
301
|
+
if (arr1.length !== arr2.length) return false;
|
|
302
|
+
for (let i = 0; i < arr1.length; i++) {
|
|
303
|
+
if (arr1[i] !== arr2[i]) return false;
|
|
304
|
+
}
|
|
305
|
+
return true;
|
|
220
306
|
}
|
|
221
307
|
},
|
|
222
308
|
watch: {
|
|
223
|
-
fileList:
|
|
224
|
-
|
|
225
|
-
|
|
309
|
+
fileList: {
|
|
310
|
+
handler: function (value) {
|
|
311
|
+
this.resultList = this.fileList
|
|
312
|
+
},
|
|
313
|
+
immediate:true,
|
|
314
|
+
deep: true
|
|
315
|
+
},
|
|
316
|
+
fileIdList: {
|
|
317
|
+
handler: function (newValue, oldValue) {
|
|
318
|
+
if (newValue.length > 0 && !this.arraysEqual(newValue, oldValue)) {
|
|
319
|
+
this.initFileList(newValue)
|
|
320
|
+
}
|
|
321
|
+
},
|
|
322
|
+
deep: true
|
|
226
323
|
}
|
|
227
324
|
}
|
|
228
325
|
}
|