@lambo-design/upload-file 1.0.0-beta.2 → 1.0.0-beta.21

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