@conecli/cone-render 0.8.20-shop.151 → 0.8.20-shop.153

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