ci-plus 1.5.2 → 1.5.3

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": "ci-plus",
3
- "version": "1.5.2",
3
+ "version": "1.5.3",
4
4
  "description": "ci组件库",
5
5
  "main": "./index.ts",
6
6
  "scripts": {
@@ -12,7 +12,7 @@
12
12
  "vue",
13
13
  "element-plus",
14
14
  "ui组件库二次封装",
15
- "标识卡模板中将下道工序改为显示名称"
15
+ "utils中的附件下载方法新增一个downFileFetchV2函数"
16
16
  ],
17
17
  "type": "module",
18
18
  "author": {
@@ -62,6 +62,14 @@ const downFileFn = function (blob: Blob | BlobPart, fileName: string) {
62
62
  type Obj = {
63
63
  [key: string]: any
64
64
  }
65
+
66
+ interface Config {
67
+ method?: string, // 请求方法,默认为 GET | POST
68
+ headers?: Object, // 请求头
69
+ cbpercentage?: Function // 获取下载进度的回调函数
70
+ fileName?: string, // 下载后的文件名
71
+ chunkSize?: number, // 每次下载的块大小,默认为 10KB
72
+ }
65
73
  const ajaxBox = {
66
74
  downFile: function (blob: Blob | BlobPart, fileName: string) {
67
75
  downFileFn(blob, fileName);
@@ -80,18 +88,28 @@ const ajaxBox = {
80
88
  * 如果解析失败,则直接以原始格式下载blob对象。
81
89
  */
82
90
 
83
-
84
- downFileFetch: function (fillAddress: string, fileName: string, method: string, headers: Obj, params: Obj, cbpercentage: Function) {
91
+ downFileFetch: function (fillAddress: string, fileName: string, method?: string, headers?: Obj, params?: Obj, cbpercentage?: Function) {
85
92
  let options = {
86
93
  method: method || 'GET',
87
94
  }
95
+ let _headers
96
+ if (options.method === 'GET' || method === 'get') {
97
+ _headers = {
98
+ 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
99
+ }
100
+
101
+ } else if (options.method === 'POST' || method === 'post') {
102
+ _headers = {
103
+ 'Content-Type': 'application/json', //设置为json格式
104
+ }
105
+ }
88
106
  // 判断请求是否为get请求,若是get请求,则将params参数拼接在url后面
89
107
  if (method === 'GET') {
90
108
  const queryString = new URLSearchParams(params).toString()
91
109
  fillAddress = fillAddress + '?' + queryString
92
110
  }
93
111
  if (headers) {
94
- options['headers'] = headers
112
+ options['headers'] = headers || _headers
95
113
  }
96
114
  if ((method === 'post' || method === 'POST') && params) {
97
115
  options['body'] = JSON.stringify(params)
@@ -151,10 +169,111 @@ const ajaxBox = {
151
169
  }
152
170
  }
153
171
 
172
+ readBlobInChunks()
173
+ })
174
+ },
175
+
176
+ // 下载文件:2.0版本
177
+ /**
178
+ * 下载文件函数
179
+ * @param url string 必传 表示服务端接口url地址
180
+ * @param params Obj 可选 请求参数
181
+ * @param config Config 可选 请求配置
182
+ * Config Config {
183
+ method?: string, // 请求方法,默认为 GET | POST
184
+ headers?: Object, // 请求头,默认值根据method方法判断
185
+ cbpercentage?: Function // 获取下载进度的回调函数
186
+ fileName?: string, // 下载后的文件名默认:'附件'
187
+ chunkSize?: number, // 每次下载的块大小,默认为 10KB
188
+ }
189
+ * 将给定的blob对象转换为文件,并提供一个下载链接以供用户下载。
190
+ * 如果blob对象是JSON格式,则尝试解析为JavaScript对象,并显示相应的错误消息。
191
+ * 如果解析失败,则直接以原始格式下载blob对象。
192
+ */
193
+
194
+ downFileFetchV2: function (url: string, params?: Obj, config?: Config) {
195
+ let options = {
196
+ method: config?.method || 'GET',
197
+ }
198
+ let _headers
199
+ let _fileName = config?.fileName || '附件'
200
+ if (options.method === 'GET' || options.method === 'get') {
201
+ _headers = {
202
+ 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
203
+ }
204
+
205
+ } else if (options.method === 'POST' || options.method === 'post') {
206
+ _headers = {
207
+ 'Content-Type': 'application/json', // 设置为json格式
208
+ }
209
+ }
210
+ // 数组请求头: 优先使用传递的请求头,如果没有传递,则使用默认的请求头
211
+ options['headers'] = config?.headers || _headers
212
+
213
+ // 判断请求是否为get请求,若是get请求,则将params参数拼接在url后面
214
+ if (options.method === 'GET') {
215
+ const queryString = new URLSearchParams(params).toString()
216
+ url = url + '?' + queryString
217
+ }
218
+ // 处理post请求的参数
219
+ if ((options.method === 'post' || options.method === 'POST') && params) {
220
+ options['body'] = JSON.stringify(params)
221
+ }
222
+
223
+ // 定义每次读取的数据块大小(字节)
224
+ let chunkSize = config?.chunkSize || 10240 // 例如,10KB
225
+
226
+ // 初始化已下载的字节数
227
+ let downloaded = 0
228
+ // 初始化 Blob 切片起始点
229
+ let start = 0
230
+
231
+ fetch(url, options)
232
+ .then((res) => {
233
+ // console.log('resssssss: ', res)
234
+ if (!res.ok) {
235
+ throw new Error('Network response was not ok')
236
+ }
237
+ return res.blob()
238
+ })
239
+ .then((blob) => {
240
+ // console.log('blob: ', blob)
241
+ const reader = new FileReader() // 创建 FileReader 对象
242
+ const readBlobInChunks = () => {
243
+ // 创建一个 Blob 切片,大小为 chunkSize
244
+ const chunkBlob = blob.slice(start, start + chunkSize)
245
+ start += chunkSize // 更新起始点
246
+ // 读取 Blob 切片
247
+ reader.readAsArrayBuffer(chunkBlob)
248
+ reader.onload = () => {
249
+ // 累加已下载的字节数
250
+ // downloaded += reader.result.byteLength
251
+ if (typeof reader.result === 'string' || reader.result === null) {
252
+ console.log('reader.result不为 ArrayBuffer');
253
+ // 处理字符串的情况,可能需要转换为 ArrayBuffer 或者其他处理逻辑
254
+ } else {
255
+ downloaded += reader.result.byteLength;
256
+ }
257
+ // 计算下载进度百分比
258
+ const total = blob.size
259
+ const progress = ((downloaded / total) * 100).toFixed(2)
260
+ config?.cbpercentage && config?.cbpercentage(progress) // 如果传递了获取下载进度的回调函数,则调用回调函数传递进度百分比
261
+ console.log(`下载进度: ${progress}%`)
262
+ // 如果还有数据未读取,则继续读取
263
+ if (start < total) {
264
+ readBlobInChunks()
265
+ } else {
266
+ console.log('下载完成.')
267
+ // 处理下载完成的数据,例如将其保存或显示
268
+ downFileFn(blob, _fileName)
269
+ }
270
+ }
271
+ }
154
272
  readBlobInChunks()
155
273
  })
156
274
  }
157
275
 
276
+
158
277
  /*
159
278
  sendJSONP: function (url, callbackName, callback) {
160
279
  const script = document.createElement('script');
@@ -182,10 +301,9 @@ export default ajaxBox;
182
301
 
183
302
 
184
303
  /**
185
- * 使用下载示例
304
+ * 使用下载示例 get
186
305
  const exportFile = () => {
187
306
  let rowData = InventoryTableRef.value!.getSelectionRows()
188
-
189
307
  const url = storageModule + 'storage_standing_book_batch_export_get/'
190
308
  const headers = {
191
309
  'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
@@ -204,5 +322,26 @@ const exportFile = () => {
204
322
  headers,
205
323
  params,
206
324
  )
325
+ }
326
+ 使用下载示例 post
327
+ const exportFile = () => {
328
+ let rowData = WeighingTableRef.value!.getSelectionRows()
329
+ const url = storageModule + 'weighing_record_batch_export_post/'
330
+ const headers = {
331
+ 'Content-Type': 'application/json', //设置为json格式
332
+ }
333
+ const params = {
334
+ ids: rowData.map((v: { id: any }) => v.id), // 直接数组形式
335
+ state: 1,
336
+ org_id: UserData.orgId,
337
+ }
338
+ console.log('params', params)
339
+ CiPlus.Fn.ajaxBox.downFileFetch(
340
+ url,
341
+ '称重记录数据(批次).xlsx',
342
+ 'POST',
343
+ headers,
344
+ params,
345
+ )
207
346
  }
208
347
  */