xianniu-ui 0.5.8 → 0.6.0

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.5.8",
3
+ "version": "0.6.0",
4
4
  "private": false,
5
5
  "main": "lib/xianniu-ui.umd.min.js",
6
6
  "scripts": {
@@ -21,7 +21,9 @@
21
21
  "public"
22
22
  ],
23
23
  "dependencies": {
24
+ "ali-oss": "^6.17.1",
24
25
  "core-js": "^3.6.5",
26
+ "uuid": "^9.0.0",
25
27
  "vue": "^2.7.14",
26
28
  "vue-lottie": "^0.2.1",
27
29
  "vue-router": "^3.2.0",
@@ -34,7 +36,6 @@
34
36
  "@vue/cli-plugin-router": "~4.5.0",
35
37
  "@vue/cli-service": "~4.5.0",
36
38
  "@vue/component-compiler-utils": "^2.6.0",
37
- "axios": "^1.4.0",
38
39
  "babel-eslint": "^10.1.0",
39
40
  "babel-plugin-component": "^1.1.1",
40
41
  "babel-plugin-module-resolver": "^2.7.1",
@@ -15,7 +15,6 @@
15
15
  :on-error="onError"
16
16
  :before-upload="onBeforeUpload"
17
17
  :style="styles"
18
- :headers="uploadHeaders"
19
18
  :on-exceed="onExceed"
20
19
  :on-change="onChange"
21
20
  >
@@ -98,9 +97,9 @@
98
97
 
99
98
  <script>
100
99
  import ElImageViewer from "element-ui/packages/image/src/image-viewer";
101
- import axios from "axios";
100
+ import Client from "@/oss";
102
101
  import uploadPop from "./upload-pop.vue";
103
- const MAX_WARNING = 1024 * 10 * 1024;
102
+ // const MAX_WARNING = 1024 * 10 * 1024;
104
103
  export default {
105
104
  name: "XnUpload",
106
105
  inheritAttrs: false,
@@ -113,10 +112,6 @@ export default {
113
112
  type: String,
114
113
  default: "picture-card",
115
114
  },
116
- hiddenUpload: {
117
- type: Boolean,
118
- default: false,
119
- },
120
115
  preview: {
121
116
  type: Boolean,
122
117
  default: false,
@@ -136,22 +131,11 @@ export default {
136
131
  },
137
132
  accept: {
138
133
  type: [Array, String],
139
- default: () => ["jpg", "jpeg", "png", "pdf"],
134
+ default: () => "*",
140
135
  },
141
136
  maxSize: {
142
137
  type: Number,
143
- default: 1024 * 200 * 1024, // 最大限制 50M
144
- },
145
- compress: {
146
- type: Number,
147
- default: 0,
148
- },
149
- type: {
150
- type: String,
151
- default: "front",
152
- validator: (val) => {
153
- return ["front", "back"].includes(val);
154
- },
138
+ default: 1024 * 200 * 1024, // 最大限制 200M
155
139
  },
156
140
  styles: {
157
141
  type: Object,
@@ -163,17 +147,13 @@ export default {
163
147
  isShowImageView: false,
164
148
  imageView: "",
165
149
  isHidden: false,
166
- // actionParams: {
167
- // action: '',
168
- // },
169
- uploadHeaders: {
170
- xnToken: "",
171
- },
172
150
  viewList: [],
173
151
  files: [],
174
152
  successFiles: [],
175
153
  isUploading: false,
176
154
  file: {},
155
+ oss: null,
156
+ client: null,
177
157
  };
178
158
  },
179
159
  computed: {
@@ -190,89 +170,93 @@ export default {
190
170
  fileList: {
191
171
  handler(n) {
192
172
  this.successFiles = n;
193
- if (this.limit === n.length) {
194
- this.isHidden = true;
195
- } else {
196
- this.isHidden = false;
197
- }
173
+ this.isHidden = this.limit === n.length;
198
174
  },
199
175
  immediate: true,
200
176
  },
201
177
  },
202
- created() {},
178
+ created() {
179
+ this.client = new Client({
180
+ stsUrl: this.$XN.stsUrl || "",
181
+ });
182
+ },
203
183
  beforeDestroy() {
204
184
  this.$emit("update:fileList", []);
205
185
  },
206
186
  methods: {
207
- onBeforeUpload(file) {
208
- let fileExt = file.name.substring(file.name.lastIndexOf(".") + 1);
209
- // 判断上传格式
210
- fileExt = `${fileExt}`.toLowerCase();
211
- if (!this.accept.includes(fileExt) && this.accept !== "*") {
212
- this.$message.warning(`请上传指定格式【${this.accept}】`);
213
- return false;
214
- }
187
+ async onBeforeUpload(file) {
215
188
  this.file = file;
216
- if (file.size > MAX_WARNING) {
217
- this.bigFileWarning();
218
- }
219
- return this.onExceedSize(file.size);
189
+ return Promise.all([
190
+ this.checkFileExt(file),
191
+ this.onExceedSize(file.size),
192
+ this.getStsToken(),
193
+ ])
194
+ .then(() => {
195
+ return Promise.resolve();
196
+ })
197
+ .catch((err) => {
198
+ return Promise.reject(err);
199
+ });
200
+ },
201
+ async getStsToken() {
202
+ return new Promise((resolve, reject) => {
203
+ this.client
204
+ .getStsToken()
205
+ .then((res) => {
206
+ this.oss = res;
207
+ resolve();
208
+ })
209
+ .catch((err) => {
210
+ reject(err);
211
+ });
212
+ });
213
+ },
214
+ checkFileExt(file) {
215
+ return new Promise((resolve, reject) => {
216
+ let fileExt = file.name.substring(file.name.lastIndexOf(".") + 1);
217
+ // 判断上传格式
218
+ fileExt = `${fileExt}`.toLowerCase();
219
+ if (!this.accept.includes(fileExt) && this.accept !== "*") {
220
+ this.$message.warning(`请上传指定格式【${this.accept}】`);
221
+ reject();
222
+ }
223
+ resolve();
224
+ });
220
225
  },
221
226
  onExceedSize(size) {
222
- if (size > this.maxSize) {
223
- this.$message.warning(
224
- `最大不能超过${this.$format.bytesToSize(this.maxSize)}`
225
- );
226
- return Promise.reject();
227
- }
228
- return Promise.resolve();
227
+ return new Promise((resolve, reject) => {
228
+ if (size > this.maxSize) {
229
+ this.$message.warning(
230
+ `最大不能超过${this.$format.bytesToSize(this.maxSize)}`
231
+ );
232
+ reject();
233
+ }
234
+ resolve();
235
+ });
229
236
  },
230
237
  onChange(file, fileList) {
231
238
  this.files = fileList;
232
239
  },
233
240
  async onHttpUpload(file) {
234
- const formData = new FormData();
235
- const _file = file.file;
236
- formData.append("file", _file);
237
241
  this.isUploading = true;
238
242
  this.$emit("on-uploaded", false);
239
- axios({
240
- method: "post",
241
- url: this.$XN.uploadUrl || "",
242
- data: formData,
243
- headers: {
244
- "Content-Type": "application/x-www-form-urlencoded",
245
- xnToken: this.$storage.get("xnToken"),
246
- },
247
- onUploadProgress: (progress) => {
248
- let _progress = Math.round((progress.loaded / progress.total) * 100);
249
- _progress = _progress === 100 ? 99 : _progress;
250
- file.onProgress({ percent: _progress });
251
- },
252
- })
243
+ this.oss
244
+ .upload(file)
253
245
  .then((res) => {
254
- const { name, size, ext, imgFlag, url, fileId } = res.data.data;
255
- this.successFiles.push({ name, size, ext, imgFlag, url, fileId });
256
- file.onSuccess();
246
+ this.successFiles.push(res);
257
247
  this.$emit("update:fileList", this.successFiles);
258
248
  this.$emit("on-success", this.successFiles);
259
249
  this.$emit("on-uploaded", true);
260
250
  this.isUploading = false;
261
- if (this.file.size > MAX_WARNING) {
262
- this.$notify.closeAll();
263
- this.bigFileSucces();
264
- }
265
251
  })
266
252
  .catch((err) => {
267
253
  console.log(err);
268
- this.$notify.closeAll();
269
254
  this.$emit("update:fileList", this.successFiles);
270
- file.onError();
271
255
  });
272
256
  },
273
257
 
274
- onError() {
275
- // console.log(err);
258
+ onError(err) {
259
+ console.log(err);
276
260
  this.$message.error("上传失败,请重试");
277
261
  },
278
262
  onSubmitUpload() {
@@ -281,7 +265,6 @@ export default {
281
265
  onAbort() {
282
266
  this.$refs.upload.abort();
283
267
  },
284
-
285
268
  onExceed() {
286
269
  this.$message.warning(`上传总数不能超过【${this.limit}】个`);
287
270
  },
@@ -327,25 +310,8 @@ export default {
327
310
  closeViewer() {
328
311
  this.isShowImageView = false;
329
312
  },
330
- bigFileWarning() {
331
- return this.$notify({
332
- title: "提示",
333
- duration: 0,
334
- dangerouslyUseHTMLString: true,
335
- message: `
336
- <p class="text-primary">当前文件体积过大,请您耐心等待,在此期间请勿刷新页面。</p>
337
- <p>名称:${this.file.name}</p>
338
- <p>体积:${this.fileSize}</p>
339
- `,
340
- type: "warning",
341
- });
342
- },
343
- bigFileSucces() {
344
- return this.$notify({
345
- title: "提示",
346
- message: "上传成功",
347
- type: "success",
348
- });
313
+ abortUpload() {
314
+ return this.oss.oss.cancel();
349
315
  },
350
316
  },
351
317
  };
@@ -1,21 +1,21 @@
1
1
  <template>
2
2
  <el-popover width="300" trigger="hover">
3
3
  <el-form label-width="80px" size="mini">
4
- <el-form-item label="文件名">
4
+ <el-form-item label="文件名" class="mb-5">
5
5
  <div :title="file.name" class="tip-filename">
6
6
  {{ file.name }}
7
7
  </div>
8
8
  </el-form-item>
9
- <el-form-item label="文件大小">
9
+ <el-form-item label="文件大小" class="mb-0">
10
10
  {{ $format.bytesToSize(file.size) }}
11
11
  </el-form-item>
12
- <el-form-item label="文件格式">
12
+ <el-form-item label="文件格式" class="mb-0">
13
13
  {{ file.ext }}
14
14
  </el-form-item>
15
- <el-form-item label="文件类型">
15
+ <el-form-item label="文件类型" class="mb-0">
16
16
  {{ file.imgFlag ? "图片" : "文件" }}
17
17
  </el-form-item>
18
- <el-form-item label="操作">
18
+ <el-form-item label="操作" class="mb-0">
19
19
  <el-link
20
20
  type="primary"
21
21
  :underline="false"
package/src/area/index.js CHANGED
@@ -12857,10 +12857,10 @@ export default [{
12857
12857
  'cityCode': '710000',
12858
12858
  'subCitys': [{
12859
12859
  'cityName': '全部',
12860
- 'cityCode': '710001',
12860
+ 'cityCode': '710100',
12861
12861
  'subCitys': [{
12862
12862
  'cityName': '全部',
12863
- 'cityCode': '710001',
12863
+ 'cityCode': '710101',
12864
12864
  }]
12865
12865
  }]
12866
12866
  },
@@ -12869,10 +12869,10 @@ export default [{
12869
12869
  'cityCode': '810000',
12870
12870
  'subCitys': [{
12871
12871
  'cityName': '全部',
12872
- 'cityCode': '810000',
12872
+ 'cityCode': '810100',
12873
12873
  'subCitys': [{
12874
12874
  'cityName': '全部',
12875
- 'cityCode': '810000',
12875
+ 'cityCode': '810101',
12876
12876
  }]
12877
12877
  }]
12878
12878
  },
@@ -12881,10 +12881,10 @@ export default [{
12881
12881
  'cityCode': '820000',
12882
12882
  'subCitys': [{
12883
12883
  'cityName': '全部',
12884
- 'cityCode': '820000',
12884
+ 'cityCode': '820100',
12885
12885
  'subCitys': [{
12886
12886
  'cityName': '全部',
12887
- 'cityCode': '820000',
12887
+ 'cityCode': '820101',
12888
12888
  }]
12889
12889
  }]
12890
12890
  }]
@@ -0,0 +1,93 @@
1
+ const OSS = require('ali-oss')
2
+ import $dayjs from '@/utils/dayjs'
3
+ const { v4: uuidv4 } = require('uuid');
4
+
5
+ class Client {
6
+ constructor(params = {}) {
7
+ this.uploadHost = null
8
+ this.token = localStorage.getItem('xnToken')
9
+ this.stsUrl = params.stsUrl || ''
10
+ this.oss = null
11
+ }
12
+ _getToken() {
13
+ const _token = localStorage.getItem('xnToken')
14
+ let token = ''
15
+ if (_token) {
16
+ try {
17
+ token = JSON.parse(_token)
18
+ } catch (error) {
19
+ token = _token
20
+ }
21
+ }
22
+ return token
23
+ }
24
+ getExt(file) {
25
+ let fileExt = file.name.substring(file.name.lastIndexOf(".") + 1);
26
+ // 判断上传格式
27
+ return `${fileExt}`.toLowerCase();
28
+ }
29
+ isImg(file) {
30
+ return ~~file.type.indexOf('image') > -1
31
+ }
32
+ getFileNameUUID() {
33
+ const uuid = uuidv4()
34
+ return uuid
35
+ }
36
+ getStsToken() {
37
+ return new Promise((resolve, reject) => {
38
+ if (!this.stsUrl) {
39
+ return console.error('获取临时凭证地址不能为空')
40
+ }
41
+ fetch(this.stsUrl + '?xnToken=' + this.token).then(response => response.json()).then((res) => {
42
+ const { data: { accessKeyId, accessKeySecret, securityToken: stsToken, uploadHost, bucket, region } } = res
43
+ const obj = {
44
+ accessKeyId,
45
+ accessKeySecret,
46
+ stsToken,
47
+ uploadHost,
48
+ bucket,
49
+ region
50
+ }
51
+ this.uploadHost = uploadHost
52
+ this.oss = new OSS({
53
+ ...obj,
54
+ })
55
+ resolve(this)
56
+ }).catch((err) => {
57
+ reject(err)
58
+ })
59
+
60
+ })
61
+ }
62
+ upload(file, headers = {}) {
63
+ const currentFile = file.file
64
+ const fileName = currentFile.name
65
+ const newFileName = this.getFileNameUUID() + '.' + this.getExt(currentFile)
66
+ const path = `accessory/${$dayjs().format('YYYY/MM/DD')}/`
67
+ return new Promise((resolve, reject) => {
68
+ this.oss.multipartUpload(path + newFileName, currentFile, {
69
+ ...headers,
70
+ progress(p) {
71
+ const _progress = parseFloat(p * 100)
72
+ file.onProgress({ percent: _progress });
73
+ }
74
+ }).then(res => {
75
+ file.onSuccess()
76
+ const obj = {
77
+ name: fileName,
78
+ size: currentFile.size,
79
+ ext: this.getExt(currentFile),
80
+ imgFlag: this.isImg(currentFile),
81
+ url: this.uploadHost + res.name,
82
+ }
83
+ resolve(obj)
84
+ }).catch(err => {
85
+ file.onError();
86
+ reject(err)
87
+ })
88
+ })
89
+ }
90
+ }
91
+
92
+
93
+ export default Client