xianniu-ui 0.8.51 → 0.8.53

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xianniu-ui",
3
- "version": "0.8.51",
3
+ "version": "0.8.53",
4
4
  "private": false,
5
5
  "main": "lib/xianniu-ui.umd.min.js",
6
6
  "scripts": {
@@ -120,7 +120,6 @@ export default {
120
120
  const res = this.findParent(_value, this.flattenResult).map(
121
121
  (item) => item.cityCode
122
122
  );
123
- console.log("123", res);
124
123
  return res;
125
124
  }
126
125
  },
@@ -246,28 +245,26 @@ export default {
246
245
  // });
247
246
  // },
248
247
  handleChange(code) {
249
- const changeResults = Array.isArray(code)
250
- ? code.map((c) => this.getRes(c))
251
- : this.getRes(code);
248
+ // 判断是否是多选
249
+ const isMultiple = Array.isArray(code) && Array.isArray(code[0]);
252
250
 
253
- const value = changeResults.map(({ cityCodeLast }) =>
254
- this.valueKey ? code : cityCodeLast
255
- );
256
-
257
- this.$emit("on-change", value);
251
+ // 获取变更结果
252
+ const changeResults = (isMultiple ? code : [code]).map(this.getRes);
258
253
 
259
- const cityResults = changeResults.map((res) => {
260
- const { city, cityCode, cityName, cityCodeLast, cityNameLast } = res;
261
- return {
262
- city,
263
- cityCode,
264
- cityName,
265
- cityCodeLast,
266
- cityNameLast,
267
- };
268
- });
254
+ // 生成 value cityResults
255
+ const [value, cityResults] = changeResults.reduce(
256
+ (acc, res) => {
257
+ const { city, cityCode, cityName, cityCodeLast, cityNameLast } = res;
258
+ acc[0].push(this.valueKey ? code : cityCodeLast);
259
+ acc[1].push({ city, cityCode, cityName, cityCodeLast, cityNameLast });
260
+ return acc;
261
+ },
262
+ [[], []]
263
+ );
269
264
 
270
- this.$emit("on-city", cityResults);
265
+ // 触发事件
266
+ this.$emit("on-change", isMultiple ? value : value[0]);
267
+ this.$emit("on-city", isMultiple ? cityResults : cityResults[0]);
271
268
  },
272
269
  // 获取结果
273
270
  getRes(cityCode) {
@@ -13,7 +13,6 @@
13
13
  v-bind="$attrs"
14
14
  :file-list.sync="fileList"
15
15
  :http-request="onHttpUpload"
16
- :on-error="onError"
17
16
  :before-upload="onBeforeUpload"
18
17
  :style="{ ...styles, ...idCardSizeData }"
19
18
  :on-exceed="onExceed"
@@ -38,7 +37,13 @@
38
37
  </slot>
39
38
  </template>
40
39
 
41
- <div slot="file" slot-scope="{ file }" :class="{'xn-upload--slot':['picture-card', 'idcard'].includes(listType)}">
40
+ <div
41
+ slot="file"
42
+ slot-scope="{ file }"
43
+ :class="{
44
+ 'xn-upload--slot': ['picture-card', 'idcard'].includes(listType),
45
+ }"
46
+ >
42
47
  <template v-if="['list'].includes(listType)">
43
48
  <a
44
49
  class="el-upload-list__item-name"
@@ -89,6 +94,7 @@
89
94
  </template>
90
95
  <template v-if="['picture-card', 'idcard'].includes(listType)">
91
96
  <uploadPop :file="file" @on-download="handleDownload(file)"></uploadPop>
97
+ <div v-if="file.status === 'fail'">失败</div>
92
98
  <template v-if="$utils.isImg(file)">
93
99
  <el-image
94
100
  class="el-upload-list__item-thumbnail"
@@ -245,7 +251,6 @@ export default {
245
251
  imageView: "",
246
252
  isHidden: false,
247
253
  viewList: [],
248
- files: [],
249
254
  successFiles: [],
250
255
  isUploading: false,
251
256
  file: {},
@@ -254,6 +259,8 @@ export default {
254
259
  idCardSizeData: {},
255
260
  isShowAV: false,
256
261
  avUrl: "",
262
+ realFileList: [],
263
+ reUploadFile: {},
257
264
  };
258
265
  },
259
266
  computed: {
@@ -265,11 +272,14 @@ export default {
265
272
  fileSize() {
266
273
  return this.$format.bytesToSize(this.file.size);
267
274
  },
275
+ isMultiple() {
276
+ return this.$attrs.multiple;
277
+ },
268
278
  },
269
279
  watch: {
270
280
  fileList: {
271
281
  handler(n) {
272
- this.successFiles = n;
282
+ // this.successFiles = n;
273
283
  this.isHidden = this.limit === n.length;
274
284
  },
275
285
  immediate: true,
@@ -344,29 +354,43 @@ export default {
344
354
  });
345
355
  },
346
356
  onChange(file, fileList) {
347
- this.files = fileList;
357
+ this.realFileList = fileList;
348
358
  },
349
359
  async onHttpUpload(file) {
360
+ this.handleUpload(file);
361
+ },
362
+ handleUpload(file) {
350
363
  this.isUploading = true;
351
364
  this.$emit("on-uploaded", false);
352
365
  this.oss
353
366
  .upload(file)
354
367
  .then((res) => {
355
368
  this.successFiles.push(res);
356
- this.$emit("update:fileList", this.successFiles);
369
+
370
+ this.realFileList.forEach((item, idx) => {
371
+ if (item.uid === res.file.uid) {
372
+ delete res.file;
373
+ this.$set(this.realFileList, idx, res);
374
+ }
375
+ });
376
+
377
+ this.$emit("update:fileList", this.realFileList);
357
378
  this.$emit("on-file", this.res);
358
379
  this.$emit("on-success", this.successFiles);
359
380
  this.$emit("on-uploaded", true);
360
381
  this.isUploading = false;
361
382
  })
362
- .catch(() => {
363
- this.$emit("update:fileList", this.successFiles);
383
+ .catch(({ fileName }) => {
384
+ this.$notify.error({
385
+ title: "上传失败",
386
+ dangerouslyUseHTMLString: true,
387
+ message:`<div><p>文件名:</p>${fileName}</div>`,
388
+ });
364
389
  });
365
390
  },
366
-
367
- onError() {
368
- this.$message.error("上传失败,请重试");
369
- },
391
+ // onError() {
392
+ // this.$message.error("上传失败,请重试");
393
+ // },
370
394
  onSubmitUpload() {
371
395
  this.$refs.upload.submit();
372
396
  },
package/src/oss/index.js CHANGED
@@ -1,130 +1,121 @@
1
- const OSS = require('ali-oss')
2
- import $dayjs from '@/utils/dayjs'
1
+ const OSS = require('ali-oss');
2
+ import $dayjs from '@/utils/dayjs';
3
3
  const { v4: uuidv4 } = require('uuid');
4
+
4
5
  class Client {
5
6
  constructor(params = {}) {
6
- this.uploadHost = null
7
- this.stsUrl = params.stsUrl || ''
8
- this.setFileIdUrl = params.setFileIdUrl || ''
9
- this.oss = null
7
+ this.uploadHost = null;
8
+ this.stsUrl = params.stsUrl || '';
9
+ this.setFileIdUrl = params.setFileIdUrl || '';
10
+ this.oss = null;
10
11
  }
12
+
11
13
  getToken() {
12
- const _token = localStorage.getItem('xnToken')
13
- let token = ''
14
- if (_token) {
15
- try {
16
- token = JSON.parse(_token)
17
- } catch (error) {
18
- token = _token
19
- }
14
+ try {
15
+ const _token = localStorage.getItem('xnToken');
16
+ return _token ? JSON.parse(_token) : '';
17
+ } catch (error) {
18
+ console.error('Failed to parse token:', error);
19
+ return '';
20
20
  }
21
- return token
22
21
  }
22
+
23
23
  getExt(file) {
24
- let fileExt = file.name.substring(file.name.lastIndexOf(".") + 1);
25
- // 判断上传格式
26
- return `${fileExt}`.toLowerCase();
24
+ return file.name.split('.').pop().toLowerCase();
27
25
  }
26
+
28
27
  isImg(file) {
29
- return file.type.indexOf('image') > -1
28
+ return file.type.startsWith('image');
30
29
  }
31
- isAV(file){
32
- return file.type.indexOf('audio') > -1 || file.type.indexOf('video') > -1
30
+
31
+ isAV(file) {
32
+ return file.type.startsWith('audio') || file.type.startsWith('video');
33
33
  }
34
+
34
35
  getFileNameUUID() {
35
- const uuid = uuidv4()
36
- return uuid
36
+ return uuidv4();
37
37
  }
38
- getStsToken(file) {
39
- return new Promise((resolve, reject) => {
40
- if (!this.stsUrl) {
41
- console.error('获取临时凭证地址不能为空');
42
- file.onError()
43
- return
44
- }
45
38
 
46
- fetch(this.stsUrl + '?xnToken=' + this.getToken()).then(response => response.json()).then((res) => {
47
- const { data: { accessKeyId, accessKeySecret, securityToken: stsToken, uploadHost, bucket, region } } = res
48
- const obj = {
49
- accessKeyId,
50
- accessKeySecret,
51
- stsToken,
52
- uploadHost,
53
- bucket,
54
- region
55
- }
56
- this.uploadHost = uploadHost
57
- this.oss = new OSS({
58
- ...obj,
59
- })
60
- resolve(this)
61
- }).catch((err) => {
62
- reject(err)
63
- })
39
+ async getStsToken(file) {
40
+ if (!this.stsUrl) {
41
+ console.error('获取临时凭证地址不能为空');
42
+ file.onError();
43
+ return;
44
+ }
64
45
 
65
- })
46
+ try {
47
+ const response = await fetch(this.stsUrl + '?xnToken=' + this.getToken());
48
+ const res = await response.json();
49
+ const { accessKeyId, accessKeySecret, securityToken: stsToken, uploadHost, bucket, region } = res.data;
50
+ this.uploadHost = uploadHost;
51
+ this.oss = new OSS({
52
+ accessKeyId,
53
+ accessKeySecret,
54
+ stsToken,
55
+ bucket,
56
+ region
57
+ });
58
+ } catch (err) {
59
+ console.error('获取临时凭证失败:', err);
60
+ throw err;
61
+ }
66
62
  }
67
- setFileId(params) {
68
- return new Promise((resolve, reject) => {
69
- fetch(this.setFileIdUrl, {
63
+
64
+ async setFileId(params) {
65
+ try {
66
+ const response = await fetch(this.setFileIdUrl, {
70
67
  method: 'POST',
71
68
  headers: {
72
69
  'Content-Type': 'application/json',
73
70
  'xnToken': this.getToken()
74
71
  },
75
72
  body: JSON.stringify(params)
76
- })
77
- .then(response => {
78
- if (!response.ok) {
79
- throw new Error(`HTTP error! status: ${response.status}`);
80
- }
81
- return response.json()
82
- })
83
- .then(({ data }) => {
84
- resolve(data)
85
- }).catch((err) => {
86
- reject(err)
87
- });
88
- })
73
+ });
74
+ const { data } = await response.json();
75
+ return data;
76
+ } catch (err) {
77
+ console.error('设置文件ID失败:', err);
78
+ throw err;
79
+ }
89
80
  }
90
- upload(file, headers = {}) {
91
- const currentFile = file.file
92
- const fileName = currentFile.name
93
- const newFileName = this.getFileNameUUID() + '.' + this.getExt(currentFile)
94
- const path = `accessory/${$dayjs().format('YYYY/MM/DD')}/`
95
- return new Promise((resolve, reject) => {
96
- this.oss.multipartUpload(path + newFileName, currentFile, {
81
+
82
+ async upload(file, headers = {}) {
83
+ const currentFile = file.file;
84
+ const fileName = currentFile.name;
85
+ const newFileName = this.getFileNameUUID() + '.' + this.getExt(currentFile);
86
+ const path = `accessory/${$dayjs().format('YYYY/MM/DD')}/`;
87
+
88
+ try {
89
+ await this.getStsToken(file);
90
+
91
+ const res = await this.oss.multipartUpload(path + newFileName, currentFile, {
97
92
  ...headers,
98
- progress(p) {
99
- const _progress = parseFloat(p * 100)
93
+ progress: (p) => {
94
+ const _progress = parseFloat(p * 100);
100
95
  file.onProgress({ percent: _progress });
101
96
  }
102
- }).then(async res => {
103
- file.onSuccess()
104
- const obj = {
105
- name: fileName,
106
- size: currentFile.size,
107
- ext: this.getExt(currentFile),
108
- imgFlag: ~~this.isImg(currentFile),
109
- isAV: ~~this.isAV(currentFile),
110
- url: this.uploadHost + res.name,
111
- accessoryName: fileName,
112
- accessorySize: currentFile.size
113
- }
114
- this.setFileId(obj).then((res) => {
115
- resolve({ ...obj, fileId: res.fileId })
116
- }).catch((err) => {
117
- file.onError();
118
- reject(err)
119
- })
97
+ });
120
98
 
121
- }).catch(err => {
122
- file.onError();
123
- reject(err)
124
- })
125
- })
99
+ const obj = {
100
+ name: fileName,
101
+ size: currentFile.size,
102
+ ext: this.getExt(currentFile),
103
+ imgFlag: Number(this.isImg(currentFile)),
104
+ isAV: Number(this.isAV(currentFile)),
105
+ url: this.uploadHost + res.name,
106
+ accessoryName: fileName,
107
+ accessorySize: currentFile.size
108
+ };
109
+
110
+ const fileIdResponse = await this.setFileId(obj);
111
+ file.onSuccess();
112
+ return { ...obj, fileId: fileIdResponse.fileId, file: currentFile };
113
+ } catch (err) {
114
+ file.onError();
115
+ console.error('上传文件失败:', err);
116
+ throw err;
117
+ }
126
118
  }
127
119
  }
128
120
 
129
-
130
- export default Client
121
+ export default Client;