ci-plus 1.6.4 → 1.6.6

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.
@@ -15,7 +15,7 @@ import { ElMessage } from 'element-plus'
15
15
  * 如果blob对象是JSON格式,则尝试解析为JavaScript对象,并显示相应的错误消息。
16
16
  * 如果解析失败,则直接以原始格式下载blob对象。
17
17
  */
18
- const downFileFn = function (blob: Blob | BlobPart, fileName: string) {
18
+ const downFileFn = function (blob: Blob | BlobPart, fileName: string, resolve?: Function, reject?: Function) {
19
19
  // 创建一个新的Blob对象,指定类型为application/json,假设blob是我们需要转换的Blob对象
20
20
  let b = new Blob([blob], {
21
21
  type: 'application/json',
@@ -35,7 +35,7 @@ const downFileFn = function (blob: Blob | BlobPart, fileName: string) {
35
35
  let response = JSON.parse(text as string)
36
36
  console.log(response) // 这里是解析后object对象
37
37
  ElMessage.error(response.msg)
38
- return
38
+ return reject && reject(response)
39
39
  } catch (error) {
40
40
  // 如果解析失败,将Blob对象转换为URL并创建下载链接
41
41
  // console.error('Error parsing JSON', error)
@@ -47,8 +47,9 @@ const downFileFn = function (blob: Blob | BlobPart, fileName: string) {
47
47
  link.setAttribute('download', fileName)
48
48
  document.body.appendChild(link)
49
49
  link.click()
50
- ElMessage.success('导出完成!请打开浏览器自带下载器查看文件!')
50
+ ElMessage.success('操作成功,请打开浏览器自带下载器查看文件!')
51
51
  link = null
52
+ resolve && resolve({ msg: '操作成功,请打开浏览器自带下载器查看文件!' })
52
53
  }
53
54
  }
54
55
 
@@ -69,6 +70,7 @@ interface Config {
69
70
  cbpercentage?: Function // 获取下载进度的回调函数
70
71
  fileName?: string, // 下载后的文件名
71
72
  chunkSize?: number, // 每次下载的块大小,默认为 10KB
73
+ body?: string, // 请求体
72
74
  }
73
75
  const ajaxBox = {
74
76
  downFile: function (blob: Blob | BlobPart, fileName: string) {
@@ -267,7 +269,7 @@ const ajaxBox = {
267
269
  console.log('下载完成.')
268
270
  // 处理下载完成的数据,例如将其保存或显示
269
271
  downFileFn(blob, _fileName)
270
- resolve(true)
272
+ // resolve(true)
271
273
  }
272
274
  }
273
275
  }
@@ -277,7 +279,120 @@ const ajaxBox = {
277
279
  reject(error)
278
280
  })
279
281
  })
280
- }
282
+ },
283
+
284
+ // 下载文件:3.0版本:优先使用传递的文件名,若不传递文件问就使用后端返回的文件名
285
+ downFileFetchV3: function (url: string, params?: Obj, config?: Config) {
286
+ let options: any = {
287
+ method: config?.method || 'GET',
288
+ }
289
+ let _headers
290
+ let _fileName = ''
291
+ if (options.method === 'GET' || options.method === 'get') {
292
+ _headers = {
293
+ 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
294
+ }
295
+ } else if (options.method === 'POST' || options.method === 'post') {
296
+ _headers = {
297
+ 'Content-Type': 'application/json', // 设置为json格式
298
+ }
299
+ }
300
+ // 数组请求头: 优先使用传递的请求头,如果没有传递,则使用默认的请求头
301
+ options['headers'] = config?.headers || _headers
302
+
303
+ // 判断请求是否为get请求,若是get请求,则将params参数拼接在url后面
304
+ if (options.method === 'GET') {
305
+ const queryString = new URLSearchParams(params).toString()
306
+ url = url + '?' + queryString
307
+ }
308
+ // 处理post请求的参数
309
+ if ((options.method === 'post' || options.method === 'POST') && params) {
310
+ options['body'] = JSON.stringify(params)
311
+ }
312
+
313
+ // 定义每次读取的数据块大小(字节)
314
+ let chunkSize = config?.chunkSize || 10240 // 例如,10KB
315
+
316
+ // 初始化已下载的字节数
317
+ let downloaded = 0
318
+ // 初始化 Blob 切片起始点
319
+ let start = 0
320
+
321
+ return new Promise((resolve, reject) => {
322
+ fetch(url, options)
323
+ .then((res: any) => {
324
+ if (config?.fileName && res.ok) {
325
+ // 如果传递了文件名,则直接返回 Blob
326
+ _fileName = config?.fileName || '附件.xlsx'
327
+ return res.blob().then((blob: Blob) => ({ blob, _fileName }))
328
+ } else if (!config?.fileName && res.ok) {
329
+ // 如果没传递文件名,则使后端返回文件名
330
+ // 获取响应头中的 Content-Disposition(此属性中包含文件名)如:'attachment; filename="测试文件.xlsx"'
331
+ const contentDisposition = res.headers.get('Content-Disposition')
332
+ console.log('后端返回的文件名字符串: ', contentDisposition)
333
+ if (contentDisposition) {
334
+ // 解析文件名
335
+ const fileNameMatch = contentDisposition.match(/filename="(.+)"/)
336
+ // console.log('文件名数组: ', fileNameMatch)
337
+ if (fileNameMatch.length > 1) {
338
+ // 匹配到文件名,则将文件名解码并赋值给 _fileName
339
+ _fileName = decodeURIComponent(fileNameMatch[1])
340
+ console.log('后端返回的文件名: ', _fileName)
341
+ return res.blob().then((blob: Blob) => ({ blob, _fileName }))
342
+ }
343
+ }
344
+ }
345
+
346
+ if (!res.ok) {
347
+ console.log('res: ', res)
348
+ throw new Error('网络响应不正常!或其他错误')
349
+ }
350
+ // 如果没有找到文件名,直接返回 Blob
351
+ return res.blob().then((blob: Blob) => ({ blob, _fileName }))
352
+ })
353
+ .then(({ blob, _fileName }) => {
354
+ // console.log('blob: ', blob, _fileName)
355
+ const reader = new FileReader() // 创建 FileReader 对象
356
+ const readBlobInChunks = () => {
357
+ // 创建一个 Blob 切片,大小为 chunkSize
358
+ const chunkBlob = blob.slice(start, start + chunkSize)
359
+ start += chunkSize // 更新起始点
360
+ // 读取 Blob 切片
361
+ reader.readAsArrayBuffer(chunkBlob)
362
+ reader.onload = () => {
363
+ // 累加已下载的字节数
364
+ // downloaded += reader.result.byteLength
365
+ if (typeof reader.result === 'string' || reader.result === null) {
366
+ console.log('reader.result不为 ArrayBuffer')
367
+ // 处理字符串的情况,可能需要转换为 ArrayBuffer 或者其他处理逻辑
368
+ } else {
369
+ downloaded += reader.result.byteLength
370
+ }
371
+ // 计算下载进度百分比
372
+ const total = blob.size
373
+ const progress = ((downloaded / total) * 100).toFixed(2)
374
+ config?.cbpercentage && config?.cbpercentage(progress) // 如果传递了获取下载进度的回调函数,则调用回调函数传递进度百分比
375
+ console.log(`下载进度: ${progress}%`)
376
+ // 如果还有数据未读取,则继续读取
377
+ if (start < total) {
378
+ readBlobInChunks()
379
+ } else {
380
+ console.log('下载完成.')
381
+ // 处理下载完成的数据,例如将其保存或显示
382
+ downFileFn(blob, _fileName, resolve, reject)
383
+ // resolve(true)
384
+ }
385
+ }
386
+ }
387
+ readBlobInChunks()
388
+ })
389
+ .catch((error) => {
390
+ console.error('下载文件时出错:', error)
391
+ reject(error)
392
+ })
393
+ })
394
+ },
395
+
281
396
 
282
397
 
283
398
  /*
@@ -363,4 +478,27 @@ const exportFile = () => {
363
478
  loading.close() // 关闭加载中
364
479
  })
365
480
  }
481
+ // 版本3.0:提供了.then和.catch方法,可以更方便地处理异步操作的结果和错误。
482
+ const loading = ElLoading.service({
483
+ lock: true,
484
+ text: 'Loading',
485
+ background: 'rgba(0, 0, 0, 0.7)',
486
+ })
487
+ const url = CrossAuditModule + 'generate_report_get/'
488
+ const params = {
489
+ ids: ids,
490
+ }
491
+ ajaxBox
492
+ .downFileFetchV3(url, params, {
493
+ method: 'POST',
494
+ })
495
+ .then((res) => {
496
+ console.log('成功: ', res)
497
+ })
498
+ .catch((err) => {
499
+ console.log('失败: ', err)
500
+ }).finally(() => {
501
+ loading.close() // 关闭加载中
502
+ })
503
+
366
504
  */