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

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