@lambo-design/upload-file 1.0.0-beta.3 → 1.0.0-beta.31

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.
Files changed (3) hide show
  1. package/index.js +1 -0
  2. package/package.json +13 -6
  3. package/src/index.vue +196 -58
package/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  import LamboUploadFile from './src/index.vue'
2
2
  export default LamboUploadFile
3
+ export { LamboUploadFile }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lambo-design/upload-file",
3
- "version": "1.0.0-beta.3",
3
+ "version": "1.0.0-beta.31",
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
- "dependencies": {
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
- <Button :size="buttonSize" icon="md-cloud-upload" type="primary" :loading="loading"
5
- @click="UploadFile()" ghost v-if="uploadBtn">
6
- <span v-if="!loading">{{title}}</span>
7
- <span v-else>Loading...</span>
8
- </Button>
9
- <a v-if="!uploadBtn" @click="UploadFile()">
10
- <Icon type="md-add"></Icon>
11
- 上传附件
12
- </a>
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="false" ref="uploadInput" style="position:absolute; clip:rect(0 0 0 0);"
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
- <div class="upload-file-list" v-if="showFileList && resultList.length > 0">
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="deleteFile(item.fileCode)" type="text">删除</Button>
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 axios from 'axios';
38
- import {formatterSizeUnit} from "@lambo-design/shared/utils/date";
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|jpg|jpeg|png|gif"
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: false
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
- fileCode: "",
169
+ fileId: "",
116
170
  fileType: "",
117
171
  fileName: "",
118
172
  size: ""
@@ -120,21 +174,41 @@ 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.$refs.uploadInput.click();
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
- // var file = e.target.files[0];
198
+ self.loading = true;
131
199
  for (let file of e.target.files) {
132
- // fileSuffix
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('文件类型必须是doc,docx,txt,xls,xlsx,jpg,jpeg,png中的一种');
202
+ alert(self.dynamicFileTypeAlertMessage);
203
+ self.$refs.uploadInput.value = null; // 清空文件输入框
204
+ self.loading = false;
136
205
  return false;
137
206
  } else {
207
+ if (this.fileMaxSize && file.size > this.fileMaxSize * 1024 * 1024) {
208
+ self.$Message.error("上传文件大小需要小于" + this.fileMaxSize + "MB");
209
+ self.loading = false;
210
+ return
211
+ }
138
212
  self.attachmentFile.fileName = file;
139
213
  if (self.attachmentFile.fileName.name.indexOf("/") > 0) {
140
214
  self.attachmentFile.fileName = self.attachmentFile.fileName.name.substring(self.attachmentFile.fileName.name.lastIndexOf("/") + 1);
@@ -144,42 +218,39 @@ export default {
144
218
  }
145
219
  }
146
220
  self.attachmentFile.fileType = self.attachmentFile.fileName.name.substring(self.attachmentFile.fileName.name.lastIndexOf(".") + 1);
147
-
148
221
  }
149
- self.loading = true;
150
- self.loadtoServer();
222
+ self.doUpload();
151
223
  },
152
- loadtoServer: function () {
153
- var self = this;
154
- let inputDOM = this.$refs.uploadInput;
155
- var afile = inputDOM.files;
156
- if (afile.size == 0) {
224
+ doUpload: function () {
225
+ let self = this;
226
+ const allFile = this.$refs.uploadInput.files;
227
+ if (allFile.size === 0) {
157
228
  alert("不能传入空文件");
229
+ self.loading = false;
158
230
  return false;
159
231
  }
160
232
  let formData = new FormData();
161
- for (let file of afile) {
233
+ for (let file of allFile) {
162
234
  formData.append('file', file);
163
235
  }
164
236
  let resultList = [];
165
- let uploadUrl = self.ossServerContext + self.ossFilePutUrl;
166
- let downloadUrl = self.ossServerContext + self.ossFileGetUrl;
167
- axios.post(uploadUrl, formData).then(response => {
168
- if (response.data.code == "1") {
169
- var result = response.data.data;
237
+ const uploadUrl = self.ossServerContext + self.ossFilePutUrl;
238
+ const downloadUrl = self.ossServerContext + self.ossFileGetUrl;
239
+ ajax.post(uploadUrl, formData, {payload: true}).then(response => {
240
+ if (response.data.code === 1) {
241
+ const result = response.data.data;
170
242
  self.loading = false;
171
243
  if (result.length >= 1) {
172
244
  result.forEach(item => {
173
- let tmp = {
245
+ const tmp = {
174
246
  fileUrl: downloadUrl + item.fileId,
175
- fileCode: item.fileId,
247
+ fileId: item.fileId,
176
248
  fileName: item.fileName,
177
249
  fileType: item.fileName.substring(item.fileName.lastIndexOf(".") + 1),
178
250
  size: item.length,
179
251
  showSize: formatterSizeUnit(item.length),
180
252
  otherParam: self.otherParam
181
253
  };
182
-
183
254
  resultList.push(tmp);
184
255
  self.resultList.push(tmp);
185
256
  });
@@ -199,12 +270,35 @@ export default {
199
270
  self.$refs.uploadInput.value = null
200
271
  });
201
272
  },
202
- deleteFile: function (fileCode) {
203
- if (fileCode) {
204
- var result = [];
205
- var deletes = []
273
+
274
+ encode64(value) {
275
+ let typeValue = typeof value;
276
+ if (typeValue === 'string' || typeValue === 'number') {
277
+ return window.btoa(
278
+ window.encodeURIComponent(value)
279
+ .replace(/%([0-9A-F]{2})/g,
280
+ function toSolidBytes(match, p1) {
281
+ return String.fromCharCode('0x' + p1);
282
+ }));
283
+ } else {
284
+ return value;
285
+ }
286
+ },
287
+ previewFile: function (item) {
288
+ if (item.fileId || item.fileCode) {
289
+ const url = this.fileOnlinePreviewHost + this.ossServerContext + this.ossFilePreviewUrl + item.fileId ? item.fileId : item.fileCode + "?fullfilename=" + item.fileName + "." + item.fileType;
290
+ window.open(config.fileOnlinePreviewContext + '/onlinePreview?url=' + encodeURIComponent(this.encode64(url)));
291
+ }
292
+ },
293
+ deleteFile: function (fileId) {
294
+ if (fileId) {
295
+ let result = [];
296
+ let deletes = []
206
297
  this.resultList.forEach(item => {
207
- if (item.fileCode != fileCode) {
298
+ if (!item.fileId) {
299
+ item.fileId = item.fileCode;
300
+ }
301
+ if (item.fileId !== fileId) {
208
302
  result.push(item);
209
303
  } else {
210
304
  deletes.push(item)
@@ -217,13 +311,57 @@ export default {
217
311
  this.$emit("upload-result", this.resultList, deletes);
218
312
  }
219
313
  }
314
+ },
315
+ initFileList: function (value) {
316
+ if (this.resultList.length === 0) {
317
+ let requests = []
318
+ for (const fileId of value) {
319
+ requests.push(ajax.get(config.upmsServerContext + "/oss/file/getMetaData/" + fileId))
320
+ }
321
+ Promise.all(requests).then((response) => {
322
+ response.forEach(item => {
323
+ this.resultList.push({
324
+ fileId: item.data.data.fileId,
325
+ fileUrl: this.ossServerContext + this.ossFileGetUrl + item.data.data.fileId,
326
+ fileName: item.data.data.fileName,
327
+ fileType: item.data.data.fileName.substring(item.data.data.fileName.lastIndexOf(".") + 1),
328
+ size: item.data.data.length,
329
+ showSize: formatterSizeUnit(item.data.data.length),
330
+ otherParam: this.otherParam
331
+ })
332
+ })
333
+ });
334
+ }
335
+ },
336
+ arraysEqual(arr1, arr2) {
337
+ if (arr1.length !== arr2.length) return false;
338
+ for (let i = 0; i < arr1.length; i++) {
339
+ if (arr1[i] !== arr2[i]) return false;
340
+ }
341
+ return true;
220
342
  }
221
343
  },
222
344
  watch: {
223
- fileList: function (value) {
224
- var data = this.fileList;
225
- this.resultList = data;
345
+ fileList: {
346
+ handler: function (value) {
347
+ this.resultList = this.fileList
348
+ },
349
+ immediate:true,
350
+ deep: true
351
+ },
352
+ fileIdList: {
353
+ handler: function (newValue, oldValue) {
354
+ if (newValue.length > 0 && !this.arraysEqual(newValue, oldValue)) {
355
+ this.initFileList(newValue)
356
+ } else if (newValue.length === 0) {
357
+ this.resultList = []
358
+ }
359
+ },
360
+ deep: true
226
361
  }
362
+ },
363
+ mounted() {
364
+ this.initFileList(this.fileIdList)
227
365
  }
228
366
  }
229
367
  </script>