@conecli/cone-render 0.9.1-shop2.28 → 0.9.1-shop2.29

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.
@@ -1 +1 @@
1
- import Taro from '@tarojs/taro'
2
1
  isSupportHybridHttpRequest,
3
2
  draInterfaceCustomReport,
4
3
  draBusinessCustomReport,
5
4
  isIosDevice,
6
5
  isJdApp,
7
6
  isPc,
8
7
  isJdAndHarmonyDevice,
9
8
  jsonHeader = 'application/json;charset=utf-8',
10
9
  formDataHeader = 'application/x-www-form-urlencoded',
11
10
  HYBRID: 'httpRequest_hybrid',
12
11
  TARO: 'httpRequest_taro',
13
12
  #requestTimeStamp: number
14
13
  #responseTimeStamp: number
15
14
  #getResTimeStamp: number
16
15
  #reportType: string
17
16
  #callbackFunction: string
18
17
 
19
18
  async request({
20
19
  url,
21
20
  method = 'POST',
22
21
  timeout = 7000,
23
22
  isColorVerify = false,
24
23
  ...otherOptions
25
24
  }): Promise<Taro.request.SuccessCallbackResult<any>> {
26
25
  const { header: otherHeader, ...otherOpts } = otherOptions
27
26
  const header = {
28
27
  'content-type':
29
28
  method === 'POST'
30
29
  ? RequestHeaderContentType.formDataHeader
31
30
  : RequestHeaderContentType.jsonHeader,
32
31
  ...otherHeader,
33
32
  }
34
33
  let getRequestUrl = url
35
34
  let requestTask
36
35
  const data = otherOpts?.data
37
36
  const isGatewayRequest =
38
37
  url === this.api.apiFunc && typeof data === 'object'
39
38
  if (isGatewayRequest) {
40
39
  getRequestUrl = this._handleSpecialGatewayUrl(url)
41
40
  const shouldUseHybridRequest = this._shouldUseHybridRequest()
42
41
  if (shouldUseHybridRequest) {
43
42
  requestTask = this._hybridRequest(getRequestUrl, data)
44
43
  } else {
45
44
  otherOpts.data = await this._prepareGatewayReqData(data, isColorVerify)
46
45
  requestTask = this._taroRequest(
47
46
  getRequestUrl,
48
47
  otherOpts,
49
48
  method,
50
49
  timeout,
51
50
  header,
52
51
  )
53
52
  }
54
53
  } else {
55
54
  requestTask = this._taroRequest(
56
55
  getRequestUrl,
57
56
  otherOpts,
58
57
  method,
59
58
  timeout,
60
59
  header,
61
60
  )
62
61
  }
63
62
  const requestTimeoutPromise =
64
63
  new Promise<ServiceInterFace.RequestPromiseRes>((resolve) => {
65
64
  setTimeout(() => {
66
65
  resolve({
67
66
  statusCode: 500,
68
67
  resTimeoutState: true,
69
68
  errMsg: 'request timeout',
70
69
  })
71
70
  }, timeout)
72
71
  })
73
72
  return Promise.race([requestTask, requestTimeoutPromise]).then(
74
73
  (res: any) => {
75
74
  if (res && res.statusCode === 500 && res.resTimeoutState) {
76
75
  if (this.#reportType === HTTP_REQUEST_TYPE.HYBRID) {
77
76
  this._clearFunction(this.#callbackFunction)
78
77
  } else {
79
78
  requestTask.abort && requestTask.abort()
80
79
  }
81
80
  }
82
81
  this._handleReportInterfaceError(url, data, timeout, res)
83
82
  return res
84
83
  },
85
84
  )
86
85
  }
87
86
 
88
87
  _handleSpecialGatewayUrl(url: string): string {
89
88
  if (isPc && window.location.hostname.includes('.jd.hk')) {
90
89
  return this.api.hkApiFunc
91
90
  }
92
91
  if (
93
92
  isJdApp &&
94
93
  window?.shopGlobalSwitch?.dualProtocol &&
95
94
  !window.location.href.includes('jshopx_vconsole')
96
95
  ) {
97
96
  return window?.shopGlobalSwitch?.dualProtocol?.apiTestApp || url
98
97
  }
99
98
  return url
100
99
  }
101
100
 
102
101
  _shouldUseHybridRequest(): boolean {
103
102
  try {
104
103
  if (!isJdApp || !isSupportHybridHttpRequest || isJdAndHarmonyDevice) {
105
104
  return false
106
105
  }
107
106
  const configData = global.getDynamicConfig('hybridHttpSwitch')
108
107
  const { globalOn = false, grayscale = {} } = configData || {}
109
108
  const buildType = process.env.BUILD_TYPE || ''
110
109
  const isInvokeGray = globalOn || grayscale[buildType]
111
110
  console.log(
112
111
  '使用hybrid请求是否命中灰度,isInvokeGray:',
113
112
  isInvokeGray,
114
113
  '获取mpaas配置hybridHttpSwitch原始数据configData',
115
114
  configData,
116
115
  )
117
116
  const hasWindowXWebView = !!window.XWebView
118
117
  return isInvokeGray && hasWindowXWebView
119
118
  } catch (e) {
120
119
  console.log('获取是否使用Hybrid请求出错,e:', e)
121
120
  return false
122
121
  }
123
122
  }
124
123
 
125
124
  _hybridRequest(url: string, data: any): Promise<any> {
126
125
  return new Promise((resolve, reject) => {
127
126
  try {
128
127
  const changeCurrentUrl = url.startsWith('//') ? `https:${url}` : url
129
128
  const reqParams = {
130
129
  url: isIosDevice ? `${changeCurrentUrl}/` : changeCurrentUrl,
131
130
  functionId: data?.functionId,
132
131
  body: data?.body,
133
132
  headerType: '1',
134
133
  }
135
134
  const callbackFunction = this._generateCallbackFunction()
136
135
  this.#callbackFunction = callbackFunction
137
136
  this.#requestTimeStamp = Date.now()
138
137
  this.#reportType = HTTP_REQUEST_TYPE.HYBRID
139
138
  window.XWebView.callNative(
140
139
  'ColorQueryPlugin',
141
140
  'colorRequest',
142
141
  JSON.stringify(reqParams),
143
142
  callbackFunction,
144
143
  '1',
145
144
  )
146
145
  window[callbackFunction] = (result) => {
147
146
  this.#responseTimeStamp = Date.now()
148
147
  try {
149
148
  const resultObj =
150
149
  typeof result === 'string' ? JSON.parse(result) : result
151
150
  resolve(resultObj)
152
151
  } catch (error) {
153
152
  const errMsg = 'hybrid网络请求JSON解析失败, error: ' + error
154
153
  draBusinessCustomReport({
155
154
  type: `${HTTP_REQUEST_TYPE.HYBRID}_jsonParseError`,
156
155
  errMsg,
157
156
  result,
158
157
  })
159
158
  reject({ errMsg })
160
159
  }
161
160
  this._clearFunction(callbackFunction)
162
161
  }
163
162
  } catch (error) {
164
163
  reject({
165
164
  errMsg:
166
165
  'hybrid网络请求,App下调用window.XWebView.callNative出错, error: ' +
167
166
  error,
168
167
  })
169
168
  }
170
169
  })
171
170
  }
172
171
 
173
172
  _generateCallbackFunction() {
174
173
  return `hybridHttpRequestCallback_${Date.now()}_${Math.ceil(
175
174
  Math.random() * 100000,
176
175
  )}`
177
176
  }
178
177
 
179
178
  _clearFunction(functionName: string) {
180
179
  try {
181
180
  delete window[functionName]
182
181
  } catch (e) {
183
182
  window[functionName] = undefined
184
183
  }
185
184
  }
186
185
 
187
186
  async _prepareGatewayReqData(
188
187
  data: any,
189
188
  isColorVerify: boolean,
190
189
  ): Promise<any> {
191
190
  const { functionId } = data
192
191
  console.log('获取当前是否需要color加固', isColorVerify, functionId)
193
192
  if (isColorVerify) {
194
193
  const { h5st } = await colorSign.paramsSign(data)
195
194
  h5st && (data.h5st = encodeURI(h5st))
196
195
  console.log(`${functionId}的apiReq_h5st===>:${h5st}`)
197
196
  }
198
197
  const { jsToken } = await colorSign.getFmInfo()
199
198
  console.log(`${functionId}的api jsToken指纹===>:${jsToken}`)
200
199
  jsToken && (data['x-api-eid-token'] = jsToken)
201
200
  return data
202
201
  }
203
202
 
204
203
  _taroRequest(
205
204
  url: string,
206
205
  otherOpts: any,
207
206
  method: string,
208
207
  timeout: number,
209
208
  header: string,
210
209
  ): Promise<any> {
211
210
  const reqParam = {
212
211
  url,
213
212
  method,
214
213
  timeout,
215
214
  header,
216
215
  credentials: 'include',
217
216
  ...otherOpts,
218
217
  }
219
218
  this.#requestTimeStamp = Date.now()
220
219
  this.#reportType = HTTP_REQUEST_TYPE.TARO
221
220
  return Taro.request(reqParam)
222
221
  }
223
222
 
224
223
  _reportRequestTime(url: string, data: any): void {
225
224
  this.#getResTimeStamp = Date.now()
226
225
  if (this.#reportType === HTTP_REQUEST_TYPE.TARO) {
227
226
  this.#responseTimeStamp = this.#getResTimeStamp
228
227
  }
229
228
  draInterfaceCustomReport(
230
229
  {
231
230
  type: `${this.#reportType}_consumeTime`,
232
231
  url,
233
232
  functionId: data?.functionId,
234
233
  requestTimeStamp: this.#requestTimeStamp,
235
234
  responseTimeStamp: this.#responseTimeStamp,
236
235
  errMsg: `使用${this.#reportType}调用接口请求响应耗时`,
237
236
  source: 'remote',
238
237
  },
239
238
  {
240
239
  consumeTime: `${this.#responseTimeStamp - this.#requestTimeStamp}ms`,
241
240
  getResTime: `${this.#getResTimeStamp - this.#requestTimeStamp}ms`,
242
241
  },
243
242
  )
244
243
  }
245
244
 
246
245
  _handleReportInterfaceError(
247
246
  url: string,
248
247
  reqData: any,
249
248
  timeOut: any,
250
249
  res: any,
251
250
  ): void {
252
251
  const source = 'remote'
253
252
  const requestType = this.#reportType
254
253
  let errorType = ''
255
254
  let subMsg = ''
256
255
  let reportFlag = false
257
256
  if (res) {
258
257
  const { statusCode, data, status, resTimeoutState } = res
259
258
  if (statusCode === 500 && resTimeoutState) {
260
259
  reportFlag = true
261
260
  errorType = 'timeout'
262
261
  subMsg = `接口请求超时${timeOut}ms`
263
262
  } else if ((statusCode === 200 || status === '0') && data) {
264
263
  const resCode = Object.prototype.hasOwnProperty.call(data, 'code')
265
264
  ? Number(data.code)
266
265
  : -1
267
266
  const isSuccess = resCode === 0 || resCode === 200
268
267
  if (!isSuccess) {
269
268
  reportFlag = true
270
269
  errorType = 'dataError'
271
270
  subMsg = '接口请求返回数据异常'
272
271
  }
273
272
  } else {
274
273
  reportFlag = true
275
274
  errorType = 'statusError'
276
275
  subMsg = '接口请求本身异常'
277
276
  }
278
277
  }
279
278
  reportFlag && draInterfaceCustomReport({
280
279
  subMsg,
281
280
  url,
282
281
  source,
283
282
  requestType,
284
283
  errorType,
285
284
  ...reqData,
286
285
  ...res,
287
286
  })
288
287
  }
288
+ import Taro from '@tarojs/taro'
289
289
  isSupportHybridHttpRequest,
290
290
  draInterfaceCustomReport,
291
291
  draBusinessCustomReport,
292
292
  jsonHeader = 'application/json;charset=utf-8',
293
293
  formDataHeader = 'application/x-www-form-urlencoded',
294
294
  HYBRID: 'httpRequest_hybrid',
295
295
  TARO: 'httpRequest_taro',
296
296
  #requestTimeStamp: number
297
297
  #responseTimeStamp: number
298
298
  #getResTimeStamp: number
299
299
  #reportType: string
300
300
  #callbackFunction: string
301
301
 
302
302
  async request({
303
303
  url,
304
304
  method = 'POST',
305
305
  timeout = 7000,
306
306
  isColorVerify = false,
307
307
  ...otherOptions
308
308
  }): Promise<Taro.request.SuccessCallbackResult<any>> {
309
309
  const { header: otherHeader, ...otherOpts } = otherOptions
310
310
  const header = {
311
311
  'content-type':
312
312
  method === 'POST'
313
313
  ? RequestHeaderContentType.formDataHeader
314
314
  : RequestHeaderContentType.jsonHeader,
315
315
  ...otherHeader,
316
316
  }
317
317
  let getRequestUrl = url
318
318
  let requestTask
319
319
  const data = otherOpts?.data
320
320
  const isGatewayRequest =
321
321
  url === this.api.apiFunc && typeof data === 'object'
322
322
  if (isGatewayRequest) {
323
323
  getRequestUrl = this._handleSpecialGatewayUrl(url)
324
324
  const shouldUseHybridRequest = this._shouldUseHybridRequest()
325
325
  if (shouldUseHybridRequest) {
326
326
  requestTask = this._hybridRequest(getRequestUrl, data)
327
327
  } else {
328
328
  otherOpts.data = await this._prepareGatewayReqData(data, isColorVerify)
329
329
  requestTask = this._taroRequest(
330
330
  getRequestUrl,
331
331
  otherOpts,
332
332
  method,
333
333
  timeout,
334
334
  header,
335
335
  )
336
336
  }
337
337
  } else {
338
338
  requestTask = this._taroRequest(
339
339
  getRequestUrl,
340
340
  otherOpts,
341
341
  method,
342
342
  timeout,
343
343
  header,
344
344
  )
345
345
  }
346
346
  const requestTimeoutPromise =
347
347
  new Promise<ServiceInterFace.RequestPromiseRes>((resolve) => {
348
348
  setTimeout(() => {
349
349
  resolve({
350
350
  statusCode: 500,
351
351
  resTimeoutState: true,
352
352
  errMsg: 'request timeout',
353
353
  })
354
354
  }, timeout)
355
355
  })
356
356
  return Promise.race([requestTask, requestTimeoutPromise]).then(
357
357
  (res: any) => {
358
358
  if (res && res.statusCode === 500 && res.resTimeoutState) {
359
359
  if (this.#reportType === HTTP_REQUEST_TYPE.HYBRID) {
360
360
  this._clearFunction(this.#callbackFunction)
361
361
  } else {
362
362
  requestTask.abort && requestTask.abort()
363
363
  }
364
364
  }
365
365
  this._handleReportInterfaceError(url, data, timeout, res)
366
366
  return res
367
367
  },
368
368
  )
369
369
  }
370
370
 
371
371
  _handleSpecialGatewayUrl(url: string): string {
372
372
  if (isPc && window.location.hostname.includes('.jd.hk')) {
373
373
  return this.api.hkApiFunc
374
374
  }
375
375
  if (
376
376
  isJdApp &&
377
377
  window?.shopGlobalSwitch?.dualProtocol &&
378
378
  !window.location.href.includes('jshopx_vconsole')
379
379
  ) {
380
380
  return window?.shopGlobalSwitch?.dualProtocol?.apiTestApp || url
381
381
  }
382
382
  return url
383
383
  }
384
384
 
385
385
  _shouldUseHybridRequest(): boolean {
386
386
  try {
387
387
  if (!isJdApp || !isSupportHybridHttpRequest || isJdAndHarmonyDevice) {
388
388
  return false
389
389
  }
390
390
  const configData = global.getDynamicConfig('hybridHttpSwitch')
391
391
  const { globalOn = false, grayscale = {} } = configData || {}
392
392
  const buildType = process.env.BUILD_TYPE || ''
393
393
  const isInvokeGray = globalOn || grayscale[buildType]
394
394
  console.log(
395
395
  '使用hybrid请求是否命中灰度,isInvokeGray:',
396
396
  isInvokeGray,
397
397
  '获取mpaas配置hybridHttpSwitch原始数据configData',
398
398
  configData,
399
399
  )
400
400
  const hasWindowXWebView = !!window.XWebView
401
401
  return isInvokeGray && hasWindowXWebView
402
402
  } catch (e) {
403
403
  console.log('获取是否使用Hybrid请求出错,e:', e)
404
404
  return false
405
405
  }
406
406
  }
407
407
 
408
408
  _hybridRequest(url: string, data: any): Promise<any> {
409
409
  return new Promise((resolve, reject) => {
410
410
  try {
411
411
  const changeCurrentUrl = url.startsWith('//') ? `https:${url}` : url
412
412
  const reqParams = {
413
413
  url: isIosDevice ? `${changeCurrentUrl}/` : changeCurrentUrl,
414
414
  functionId: data?.functionId,
415
415
  body: data?.body,
416
416
  headerType: '0',
417
417
  }
418
418
  const callbackFunction = this._generateCallbackFunction()
419
419
  this.#callbackFunction = callbackFunction
420
420
  this.#requestTimeStamp = Date.now()
421
421
  this.#reportType = HTTP_REQUEST_TYPE.HYBRID
422
422
  window.XWebView.callNative(
423
423
  'ColorQueryPlugin',
424
424
  'colorRequest',
425
425
  JSON.stringify(reqParams),
426
426
  callbackFunction,
427
427
  '1',
428
428
  )
429
429
  window[callbackFunction] = (result) => {
430
430
  this.#responseTimeStamp = Date.now()
431
431
  try {
432
432
  const resultObj =
433
433
  typeof result === 'string' ? JSON.parse(result) : result
434
434
  resolve(resultObj)
435
435
  } catch (error) {
436
436
  const errMsg = 'hybrid网络请求JSON解析失败, error: ' + error
437
437
  draBusinessCustomReport({
438
438
  type: `${HTTP_REQUEST_TYPE.HYBRID}_jsonParseError`,
439
439
  errMsg,
440
440
  result,
441
441
  })
442
442
  reject({ errMsg })
443
443
  }
444
444
  this._clearFunction(callbackFunction)
445
445
  }
446
446
  } catch (error) {
447
447
  reject({
448
448
  errMsg:
449
449
  'hybrid网络请求,App下调用window.XWebView.callNative出错, error: ' +
450
450
  error,
451
451
  })
452
452
  }
453
453
  })
454
454
  }
455
455
 
456
456
  _generateCallbackFunction() {
457
457
  return `hybridHttpRequestCallback_${Date.now()}_${Math.ceil(
458
458
  Math.random() * 100000,
459
459
  )}`
460
460
  }
461
461
 
462
462
  _clearFunction(functionName: string) {
463
463
  try {
464
464
  delete window[functionName]
465
465
  } catch (e) {
466
466
  window[functionName] = undefined
467
467
  }
468
468
  }
469
469
 
470
470
  async _prepareGatewayReqData(
471
471
  data: any,
472
472
  isColorVerify: boolean,
473
473
  ): Promise<any> {
474
474
  const { functionId } = data
475
475
  console.log('获取当前是否需要color加固', isColorVerify, functionId)
476
476
  if (isColorVerify) {
477
477
  const { h5st } = await colorSign.paramsSign(data)
478
478
  h5st && (data.h5st = encodeURI(h5st))
479
479
  console.log(`${functionId}的apiReq_h5st===>:${h5st}`)
480
480
  }
481
481
  const { jsToken } = await colorSign.getFmInfo()
482
482
  console.log(`${functionId}的api jsToken指纹===>:${jsToken}`)
483
483
  jsToken && (data['x-api-eid-token'] = jsToken)
484
484
  return data
485
485
  }
486
486
 
487
487
  _taroRequest(
488
488
  url: string,
489
489
  otherOpts: any,
490
490
  method: string,
491
491
  timeout: number,
492
492
  header: string,
493
493
  ): Promise<any> {
494
494
  const reqParam = {
495
495
  url,
496
496
  method,
497
497
  timeout,
498
498
  header,
499
499
  credentials: 'include',
500
500
  ...otherOpts,
501
501
  }
502
502
  this.#requestTimeStamp = Date.now()
503
503
  this.#reportType = HTTP_REQUEST_TYPE.TARO
504
504
  return Taro.request(reqParam)
505
505
  }
506
506
 
507
507
  _reportRequestTime(url: string, data: any): void {
508
508
  this.#getResTimeStamp = Date.now()
509
509
  if (this.#reportType === HTTP_REQUEST_TYPE.TARO) {
510
510
  this.#responseTimeStamp = this.#getResTimeStamp
511
511
  }
512
512
  draInterfaceCustomReport(
513
513
  {
514
514
  type: `${this.#reportType}_consumeTime`,
515
515
  url,
516
516
  functionId: data?.functionId,
517
517
  requestTimeStamp: this.#requestTimeStamp,
518
518
  responseTimeStamp: this.#responseTimeStamp,
519
519
  errMsg: `使用${this.#reportType}调用接口请求响应耗时`,
520
520
  source: 'remote',
521
521
  },
522
522
  {
523
523
  consumeTime: `${this.#responseTimeStamp - this.#requestTimeStamp}ms`,
524
524
  getResTime: `${this.#getResTimeStamp - this.#requestTimeStamp}ms`,
525
525
  },
526
526
  )
527
527
  }
528
528
 
529
529
  _handleReportInterfaceError(
530
530
  url: string,
531
531
  reqData: any,
532
532
  timeOut: any,
533
533
  res: any,
534
534
  ): void {
535
535
  const source = 'remote'
536
536
  const requestType = this.#reportType
537
537
  let errorType = ''
538
538
  let subMsg = ''
539
539
  let reportFlag = false
540
540
  if (res) {
541
541
  const { statusCode, data, status, resTimeoutState } = res
542
542
  if (statusCode === 500 && resTimeoutState) {
543
543
  reportFlag = true
544
544
  errorType = 'timeout'
545
545
  subMsg = `接口请求超时${timeOut}ms`
546
546
  } else if ((statusCode === 200 || status === '0') && data) {
547
547
  const resCode = Object.prototype.hasOwnProperty.call(data, 'code')
548
548
  ? Number(data.code)
549
549
  : -1
550
550
  const isSuccess = resCode === 0 || resCode === 200
551
551
  if (!isSuccess) {
552
552
  reportFlag = true
553
553
  errorType = 'dataError'
554
554
  subMsg = '接口请求返回数据异常'
555
555
  }
556
556
  } else {
557
557
  reportFlag = true
558
558
  errorType = 'statusError'
559
559
  subMsg = '接口请求本身异常'
560
560
  }
561
561
  }
562
562
  reportFlag &&
563
563
  draInterfaceCustomReport({
564
564
  subMsg,
565
565
  url,
566
566
  source,
567
567
  requestType,
568
568
  errorType,
569
569
  ...reqData,
570
570
  ...res,
571
571
  })
572
572
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@conecli/cone-render",
3
- "version": "0.9.1-shop2.28",
3
+ "version": "0.9.1-shop2.29",
4
4
  "private": false,
5
5
  "files": [
6
6
  "dist/"