@conecli/cone-render 0.9.1-shop2.38 → 0.9.1-shop2.39

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
  APPID,
3
2
  JshopxColorFunctionIdList,
4
3
  PagesColorFunctionIdList,
5
4
  PAGES_DOMAIN,
6
5
  B2BColorFunctionIdList,
7
6
  MobileVerifyColorList,
8
7
  AppVerifyColorList,
9
8
  JshopxVerifyColorList,
10
9
  BMallVerifyColorList,
11
10
  shopMemberColorFunctionIdList,
12
11
  draBusinessCustomReport,
13
12
  isLanguageForEn,
14
13
  isJdApp,
15
14
  isIosDevice,
16
15
  isAndroidDevice,
17
16
  isJdAndHarmonyDevice,
18
17
  showSuccessToast,
19
18
  showFailToast,
20
19
  isH5,
21
20
  isWxMinAndWxapp,
22
21
  isMin,
23
22
  isPc,
24
23
  draInterfaceCustomReport,
25
24
  callRouterIOS,
26
25
  callRouterAndroid,
27
26
  sendNativePageToShowCartFloatBtn,
28
27
  cookiesStr: '',
29
28
  requestHeaderContentType: {
30
29
  jsonHeader: {
31
30
  'content-type': 'application/json;charset=utf-8',
32
31
  },
33
32
  formDataHeader: {
34
33
  'content-type': 'application/x-www-form-urlencoded',
35
34
  },
36
35
  },
37
36
  requestBaseParam: {
38
37
  data: null,
39
38
  header: {
40
39
  'content-type': 'application/json;charset=utf-8',
41
40
  },
42
41
  },
43
42
  timeOut: 7000,
44
43
  apiClientParams: {
45
44
  clientVersion: isJdApp && jdAppVersionStr !== '' ? jdAppVersionStr : '11.0.0',
46
45
  client: isJdApp ? (isIosDevice ? 'apple' : 'android') : isPc ? 'pc' : 'wh5',
47
46
  area: '1_72_2799_0',
48
47
  screen: `${Math.ceil(window.screen?.width * devicePixelRatio)}*${Math.ceil(
49
48
  window.screen?.height * devicePixelRatio,
50
49
  )}`,
51
50
  uuid: UUID,
52
51
  },
53
52
  public addressFetchInfo: {
54
53
  reqState: boolean;
55
54
  promiseInstance: null | Promise<any>;
56
55
  };
57
56
  public getPromotionConfig: {
58
57
  projectId: null | string | number;
59
58
  pageId: null | string | number;
60
59
  };
61
60
  public resInfo: ServiceInterFace.HttpRequestInfo;
62
61
  public api: ServiceInterFace.HttpApi;
63
62
  public isPageDomain: boolean;
64
63
  public verifyColorList: string[];
65
64
  private isShowJdShopCartFloatBtn: boolean;
66
65
  constructor(opt) {
67
66
  this._init();
68
67
  this.addressFetchInfo = {
69
68
  reqState: false,
70
69
  promiseInstance: null,
71
70
  };
72
71
  this.getPromotionConfig = {
73
72
  projectId: null,
74
73
  pageId: null,
75
74
  };
76
75
  this._userAreaUpdateListen();
77
76
  this.resInfo = this._getResConfig(opt);
78
77
  this.api = api;
79
78
  this.isPageDomain = isH5 ? window?.location?.hostname === PAGES_DOMAIN : false;
80
79
  this.verifyColorList = [
81
80
  ...MobileVerifyColorList,
82
81
  ...AppVerifyColorList,
83
82
  ...JshopxVerifyColorList,
84
83
  ...BMallVerifyColorList,
85
84
  ];
86
85
  this.isShowJdShopCartFloatBtn =
87
86
  jdAppVersionCompare(isJdAndIosDevice ? '13.8.6' : '13.6.2') >= 0;
88
87
  }
89
88
  _init(): void {
90
89
  httpInterceptors.forEach((item) => {
91
90
  Taro.addInterceptor((chain) => item(chain));
92
91
  });
93
92
  }
94
93
  _getResConfig(opt = {}): ServiceInterFace.HttpRequestInfo {
95
94
  return Object.assign({}, DefaultConfig, opt);
96
95
  }
97
96
 
98
97
  _userAreaUpdateListen() {
99
98
  Taro.eventCenter.on(TaroEventType.USER_AREA_UPDATE, (area) => {
100
99
  area &&
101
100
  area !== '' &&
102
101
  this.updateApiClientParamsResInfo({
103
102
  area,
104
103
  });
105
104
  console.log(
106
105
  'cone-render收到原生端获取传来的四级地址信息',
107
106
  area,
108
107
  '更新后的接口底层信息',
109
108
  this.resInfo.apiClientParams,
110
109
  );
111
110
  });
112
111
  }
113
112
  updateApiClientParamsResInfo(opt = {}) {
114
113
  const { area, ...others } = opt;
115
114
  this.resInfo.apiClientParams = {
116
115
  ...this.resInfo.apiClientParams,
117
116
  ...others,
118
117
  };
119
118
  if (area) {
120
119
  this.resInfo.apiClientParams.area = area;
121
120
  }
122
121
  }
123
122
 
124
123
  checkFunctionIdToReturnAppId(functionId) {
125
124
  if (JshopxColorFunctionIdList.includes(functionId)) {
126
125
  return APPID.JSHOPX_COLOR_APPID;
127
126
  } else if (this.isPageDomain && PagesColorFunctionIdList.includes(functionId)) {
128
127
  return APPID.PAGES_COLOR_APPID;
129
128
  } else if (shopMemberColorFunctionIdList.includes(functionId)) {
130
129
  return APPID.SHOP_MEMBER_COLOR_APPID;
131
130
  } else if (B2BColorFunctionIdList.includes(functionId)) {
132
131
  return APPID.B2B_COLOR_APPID;
133
132
  } else if (isPc) {
134
133
  return APPID.MALL_COLOR_APPID;
135
134
  } else {
136
135
  return APPID.SHOP_COLOR_APPID;
137
136
  }
138
137
  }
139
138
 
140
139
  gatewayReq(functionId: string, bodyParams = {}, clientParams = {}): Promise<any> {
141
140
  if (
142
141
  functionId === 'receiveShopCoupon' &&
143
142
  ((isH5 && (window?.isJingGouMiniViewState || window?.isJingxiMiniViewState)) ||
144
143
  isWxMinAndWxapp)
145
144
  ) {
146
145
  clientParams = Object.assign(
147
146
  {},
148
147
  {
149
148
  loginType: '1',
150
149
  loginWQBiz: WXAPP_BIZ_KEY,
151
150
  },
152
151
  clientParams,
153
152
  );
154
153
  }
155
154
  const apiClientParams = Object.assign({}, this.resInfo.apiClientParams, clientParams);
156
155
  typeof apiClientParams.appid === 'undefined' &&
157
156
  (apiClientParams.appid = this.checkFunctionIdToReturnAppId(functionId));
158
157
  const isColorVerify = this.verifyColorList.includes(functionId);
159
158
  const getReqData = {
160
159
  functionId: functionId,
161
160
  body: JSON.stringify(bodyParams),
162
161
  t: Date.now(),
163
162
  ...apiClientParams,
164
163
  };
165
164
  return http
166
165
  .request({
167
166
  url: this.api.apiFunc,
168
167
  data: getReqData,
169
168
  isColorVerify,
170
169
  })
171
170
  .then((res: ServiceInterFace.RequestPromiseRes) => {
172
171
  const { statusCode, data, status, msg } = res;
173
172
  let changeRes: ServiceInterFace.RequestResponseFormatRes = {
174
173
  ok: true,
175
174
  };
176
175
  if ((statusCode === 200 || status === '0') && data) {
177
176
  changeRes = this.dealResponseFormatRes(data);
178
177
  return changeRes;
179
178
  } else {
180
179
  changeRes = {
181
180
  ...data,
182
181
  statusCode,
183
182
  status,
184
183
  msg,
185
184
  ok: false,
186
185
  };
187
186
  return changeRes;
188
187
  }
189
188
  })
190
189
  .catch((res) => {
191
190
  const propertyNames = Object.getOwnPropertyNames(res);
192
191
  const resObject = {};
193
192
  propertyNames.forEach((propertyName) => {
194
193
  resObject[propertyName] = res[propertyName];
195
194
  });
196
195
  draInterfaceCustomReport({
197
196
  subMsg:
198
197
  '接口请求异常,进入catch逻辑,例如h5请求神盾(反爬、小号、风控)或加固拦截返回status为403等',
199
198
  url: this.api.apiFunc,
200
199
  source: 'remote',
201
200
  requestType: res?.requestType,
202
201
  errorType: 'catchError',
203
202
  functionId: getReqData?.functionId,
204
203
  appid: getReqData?.appid,
205
204
  client: getReqData?.client,
206
205
  clientVersion: getReqData?.clientVersion,
207
206
  statusCode: res?.statusCode,
208
207
  status: res?.status,
209
208
  statusText: res?.statusText,
210
209
  code: res?.data?.code || res?.data?.subCode,
211
210
  errMsg: res?.errMsg || res?.msg || res?.message || res?.data?.echo,
212
211
  originReqDataStr: JSON.stringify(getReqData),
213
212
  originResDataStr: JSON.stringify(res, propertyNames),
214
213
  });
215
214
  return {
216
215
  ...resObject,
217
216
  ok: false,
218
217
  } as ServiceInterFace.RequestResponseFormatRes;
219
218
  });
220
219
  }
221
220
 
222
221
  dealResponseFormatRes(data) {
223
222
  let changeRes: ServiceInterFace.RequestResponseFormatRes = {
224
223
  ok: true,
225
224
  };
226
225
  const resCode = Object.prototype.hasOwnProperty.call(data, 'code') ? Number(data.code) : -1;
227
226
  const subCode = Object.prototype.hasOwnProperty.call(data, 'subCode')
228
227
  ? Number(data.subCode)
229
228
  : -1;
230
229
  if (
231
230
  data.success === true ||
232
231
  data.isSuccess ||
233
232
  resCode === 0 ||
234
233
  resCode === 200 ||
235
234
  subCode === 0
236
235
  ) {
237
236
  changeRes = {
238
237
  result: Object.prototype.hasOwnProperty.call(data, 'result')
239
238
  ? data.result
240
239
  : Object.prototype.hasOwnProperty.call(data, 'data')
241
240
  ? data.data
242
241
  : data,
243
242
  code: data.code ?? 200,
244
243
  ok: true,
245
244
  source: data,
246
245
  };
247
246
  return changeRes;
248
247
  } else {
249
248
  changeRes = {
250
249
  ...data,
251
250
  ok: false,
252
251
  };
253
252
  return changeRes;
254
253
  }
255
254
  }
256
255
 
257
256
  async receiveCoupon(param = {}, clientParams) {
258
257
  const loginType = isPc ? { loginType: 3 } : {};
259
258
  return await this.gatewayReq(
260
259
  'receiveShopCoupon',
261
260
  {
262
261
  operation: '3',
263
262
  ...param,
264
263
  },
265
264
  { ...loginType, ...clientParams },
266
265
  );
267
266
  }
268
267
 
269
268
  getCustomerCenterDispatch(params = {}) {
270
269
  return this.gatewayReq('customerCenterDispatch', params, {
271
270
  clientVersion: '9.2.0',
272
271
  client: 'H5',
273
272
  uuid: 88888,
274
273
  appid: 'shopmember_m_jd_com',
275
274
  });
276
275
  }
277
276
 
278
277
  batchAddCart(...args) {
279
278
  let skuIds, num, showToast, giftCard;
280
279
  if (['number', 'string'].includes(typeof args[0])) {
281
280
  [skuIds, num = 1, showToast = true, giftCard = false] = args;
282
281
  } else if (typeof args[0] === 'object' && args[0] !== null) {
283
282
  const params = args[0];
284
283
  skuIds = params.skuIds;
285
284
  num = params.num || 1;
286
285
  showToast = params.showToast !== undefined ? params.showToast : true;
287
286
  giftCard = params.giftCard !== undefined ? params.giftCard : false;
288
287
  } else {
289
288
  console.error('batchAddCart() Invalid arguments!', args)
290
289
  try{
291
290
  const data = {
292
291
  type: 'batchAddCart',
293
292
  subType: 'invalidInputParameter',
294
293
  errMsg: '入参不合法',
295
294
  originData: args
296
295
  }
297
296
  draBusinessCustomReport(data)
298
297
  }catch(e){
299
298
  console.error('draBusinessCustomReport error:', e)
300
299
  }
301
300
  const msg = isLanguageForEn? 'Please retry later': '加入购物车失败,请稍后重试!'
302
301
  showFailToast({
303
302
  title: msg,
304
303
  });
305
304
  return Promise.resolve({
306
305
  ok: false,
307
306
  msg,
308
307
  });
309
308
  }
310
309
  const addToWhereText = giftCard? '购物清单': '购物车'
311
310
  const successMsg = isLanguageForEn? 'Success': `加入${addToWhereText}成功`
312
311
  const failMsg = isLanguageForEn? 'Please retry later': `加入${addToWhereText}失败,请稍后重试!`
313
312
  if (skuIds) {
314
313
  return this._addShopGoodCart(`${skuIds}`, `${num}`, {giftCard})
315
314
  .then((res: ServiceInterFace.SkuToCartDataRes) => {
316
315
  if (res?.ok) {
317
316
  showToast &&
318
317
  !res?.isOpenApp &&
319
318
  showSuccessToast({
320
319
  title: successMsg,
321
320
  });
322
321
  this.isShowJdShopCartFloatBtn &&
323
322
  sendNativePageToShowCartFloatBtn({
324
323
  isShow: true,
325
324
  });
326
325
  } else {
327
326
  showToast &&
328
327
  showFailToast({
329
328
  title: res?.errMsg || failMsg,
330
329
  });
331
330
  }
332
331
  return Promise.resolve(res);
333
332
  })
334
333
  .catch((e) => {
335
334
  console.log('调用加购方法异常=======>', e);
336
335
  return Promise.resolve({
337
336
  ok: false,
338
337
  msg: failMsg,
339
338
  });
340
339
  });
341
340
  } else {
342
341
  try{
343
342
  const data = {
344
343
  type: 'batchAddCart',
345
344
  subType: 'invalidInputParameter',
346
345
  errMsg: '没有传入合法的skuIds',
347
346
  originData: skuIds
348
347
  }
349
348
  draBusinessCustomReport(data)
350
349
  }catch(e){
351
350
  console.error('draBusinessCustomReport error:', e)
352
351
  }
353
352
  return Promise.resolve({
354
353
  ok: false,
355
354
  msg: failMsg,
356
355
  });
357
356
  }
358
357
  }
359
358
  const loginType = isPc ? { loginType: 3 } : {};
360
359
  return this.gatewayReq(
361
360
  'giftCardAddCart',
362
361
  {
363
362
  skuList,
364
363
  num: Number(num)
365
364
  },
366
365
  { ...loginType},
367
366
  );
368
367
  }
369
368
 
370
369
  _addShopGoodCart(skuIdsStr, addCardNum = '1', {giftCard}) {
371
370
  return new Promise((resolve) => {
372
371
  const productIdsArr = skuIdsStr ? `${skuIdsStr}`.split(',') : [];
373
372
  if (productIdsArr.length > 0) {
374
373
  if(giftCard){
375
374
  this.giftCardAddCart(productIdsArr, addCardNum).then((res) => {
376
375
  if(res?.code === '0' && res?.result?.value){
377
376
  resolve({
378
377
  ok: true,
379
378
  })
380
379
  }else{
381
380
  try{
382
381
  const data = {
383
382
  type: 'batchAddCart',
384
383
  subType: 'addCartFailByGiftCard',
385
384
  errMsg: '礼品卡加车接口返回加车失败标识',
386
385
  originData: res
387
386
  }
388
387
  draBusinessCustomReport(data)
389
388
  }catch(e){
390
389
  console.error('draBusinessCustomReport error:', e)
391
390
  }
392
391
  resolve({
393
392
  ok: false,
394
393
  msg: '加购失败,请稍后重试',
395
394
  });
396
395
  }
397
396
  }).catch((e) => {
398
397
  console.error('礼品卡加车失败: ', e);
399
398
  resolve({
400
399
  ok: false,
401
400
  msg: '加购失败,请稍后重试',
402
401
  });
403
402
  });
404
403
  }
405
404
  else
406
405
  if (isJdApp && !isJdAndHarmonyDevice) {
407
406
  console.log('==========>', '原生批量加车!');
408
407
  const nativeAddCartCallBack = `nativeBatchAddCart${Date.now()}`;
409
408
  if (!window[nativeAddCartCallBack]) {
410
409
  window[nativeAddCartCallBack] = (res) => {
411
410
  const getMessageJsonData = typeof res === 'string' ? JSON.parse(res) : res;
412
411
  console.info(
413
412
  '获取批量加车原始数据',
414
413
  res,
415
414
  '====转换后的json数据为===',
416
415
  getMessageJsonData,
417
416
  );
418
417
  try {
419
418
  if (isAndroidDevice) {
420
419
  const isOk = getMessageJsonData?.status === '0';
421
420
  isOk? resolve({
422
421
  ok: true,
423
422
  msg: '已加入购物车!',
424
423
  })
425
424
  : resolve({
426
425
  ok: false,
427
426
  msg: '加购失败,请稍后重试',
428
427
  errMsg: getMessageJsonData?.data?.message ?? '',
429
428
  });
430
429
 
431
430
  if(!isOk){
432
431
  const data = {
433
432
  type: 'batchAddCart',
434
433
  subType: 'addCartFailByH5CallAndroidNative',
435
434
  errMsg: 'H5调用安卓原生加车失败',
436
435
  originData: getMessageJsonData
437
436
  }
438
437
  draBusinessCustomReport(data)
439
438
  }
440
439
  } else if (isIosDevice) {
441
440
  const isOk = getMessageJsonData?.status === '0' && getMessageJsonData?.data?.code === 0;
442
441
  isOk? resolve({
443
442
  ok: true,
444
443
  msg: '已加入购物车!',
445
444
  })
446
445
  : resolve({
447
446
  ok: false,
448
447
  msg: '批量加车失败,请稍后重试',
449
448
  errMsg: getMessageJsonData?.data?.message ?? '',
450
449
  });
451
450
  if(!isOk){
452
451
  const data = {
453
452
  type: 'batchAddCart',
454
453
  subType: 'addCartFailByH5CallIosNative',
455
454
  errMsg: 'H5调用IOS原生加车失败',
456
455
  originData: getMessageJsonData
457
456
  }
458
457
  draBusinessCustomReport(data)
459
458
  }
460
459
  } else {
461
460
  resolve({
462
461
  ok: false,
463
462
  msg: '加购失败,请稍后重试',
464
463
  });
465
464
  const data = {
466
465
  type: 'batchAddCart',
467
466
  subType: 'addCartFailByH5CallUnknownNative',
468
467
  errMsg: 'H5调用未知原生加车失败',
469
468
  }
470
469
  draBusinessCustomReport(data)
471
470
  }
472
471
  } catch (e) {
473
472
  resolve({
474
473
  ok: false,
475
474
  msg: '加购失败,请稍后重试',
476
475
  });
477
476
  console.warn('批量加车转换数据异常', e);
478
477
  const data = {
479
478
  type: 'batchAddCart',
480
479
  subType: 'addCartFailByTransformData',
481
480
  errMsg: '批量加车转换数据异常',
482
481
  originData: e
483
482
  }
484
483
  draBusinessCustomReport(data)
485
484
  }
486
485
  window[nativeAddCartCallBack] = null;
487
486
  };
488
487
  }
489
488
  const productsAddCartList = productIdsArr.map((skuItem) => {
490
489
  return isAndroidDevice
491
490
  ? {
492
491
  sku: skuItem,
493
492
  num: addCardNum,
494
493
  }
495
494
  : {
496
495
  productCode: skuItem,
497
496
  buyCount: addCardNum,
498
497
  };
499
498
  });
500
499
  if (isAndroidDevice) {
501
500
  const androidProductsStr = JSON.stringify(productsAddCartList);
502
501
  callRouterAndroid({
503
502
  routerURL: `router://JDCartModule/addCartUniformWithUrl?wareList=${androidProductsStr}&businessName=storeModel`,
504
503
  routerParam: {
505
504
  wareList: productsAddCartList,
506
505
  },
507
506
  callBackName: nativeAddCartCallBack,
508
507
  });
509
508
  } else if (isIosDevice) {
510
509
  if (isLowAppVer) {
511
510
  const lowVersionAppList = productIdsArr.map((skuItem) => {
512
511
  return {
513
512
  sku: skuItem,
514
513
  num: addCardNum,
515
514
  };
516
515
  });
517
516
  const lowVersionAppListStr = JSON.stringify(lowVersionAppList);
518
517
  const url = `openApp.jdMobile://virtual?params={"category":"jump","des":"updateCartNum","sourceValue":"店铺批量加购","sourceType":"h5","businessName":"storeModel","wareList":${lowVersionAppListStr}}`;
519
518
  jump.side.jdJumpToWeb(url, {});
520
519
  resolve({
521
520
  ok: true,
522
521
  isOpenApp: true,
523
522
  msg: '加购完成, 加购结果请点击购物车确认',
524
523
  });
525
524
  } else {
526
525
  callRouterIOS({
527
526
  routerURL: 'router://JDCartModule/batchAddService',
528
527
  routerParam: {
529
528
  sourceType: 'h5',
530
529
  sourceValue: '店铺批量加购',
531
530
  businessName: 'storeModel',
532
531
  isNewMode: '1',
533
532
  NotPassWebview: '1',
534
533
  products: productsAddCartList,
535
534
  },
536
535
  callBackName: nativeAddCartCallBack,
537
536
  });
538
537
  }
539
538
  }
540
539
  } else if (isMin) {
541
540
  console.log('==========>', '京购批量加车!');
542
541
  const productsAddCartList = productIdsArr.map((skuItem) => {
543
542
  return {
544
543
  skuId: skuItem,
545
544
  buyNum: addCardNum,
546
545
  };
547
546
  });
548
547
  global.wxAppBizInstance
549
548
  .addCart({ commArr: productsAddCartList })
550
549
  .then((res) => {
551
550
  console.log('dealCartSuccess: ', res);
552
551
  resolve({
553
552
  ok: true,
554
553
  msg: '已加入购物车!',
555
554
  data: res,
556
555
  });
557
556
  })
558
557
  .catch((e) => {
559
558
  console.log('京购小程序批量加车失败dealCartFail: ', e);
560
559
  resolve({
561
560
  ok: false,
562
561
  msg: '加购失败,请稍后重试',
563
562
  });
564
563
 
565
564
  const data = {
566
565
  type: 'batchAddCart',
567
566
  subType: 'addCartFailByH5CallMini',
568
567
  errMsg: 'H5调用小程序加车失败',
569
568
  originData: e
570
569
  }
571
570
  draBusinessCustomReport(data)
572
571
  });
573
572
  } else {
574
573
  console.log('==========>', 'M站批量加车!');
575
574
  return this.productAddToCart(skuIdsStr, addCardNum).then((res) => resolve(res));
576
575
  }
577
576
  } else {
578
577
  resolve({
579
578
  ok: false,
580
579
  msg: '加购sku不能为空',
581
580
  });
582
581
  const data = {
583
582
  type: 'batchAddCart',
584
583
  subType: 'addCartFailNoSku',
585
584
  errMsg: '加购sku不能为空',
586
585
  originData: skuIdsStr
587
586
  }
588
587
  draBusinessCustomReport(data)
589
588
  }
590
589
  });
591
590
  }
592
591
 
593
592
  productAddToCart(skuIdsStr, num) {
594
593
  return new Promise((resolve) => {
595
594
  if (window.AddcartToolObj) {
596
595
  window.AddcartToolObj.addCart({
597
596
  skuId: skuIdsStr,
598
597
  num,
599
598
  source: 'sjdpyw',
600
599
  actId: '1',
601
600
  loginType: window?.isJingGouMiniViewState ? '11' : '2',
602
601
  sucFun(res) {
603
602
  console.log('ok,接口请求成功,不一定指的加车成功,加车成功需要看返回码errId为0', res);
604
603
  if (res.errId === '0' && res.cartJson) {
605
604
  resolve({
606
605
  ok: true,
607
606
  msg: '已加入购物车!',
608
607
  ...res,
609
608
  });
610
609
  } else {
611
610
  if (res.errId === '13') {
612
611
  global.doLogin();
613
612
  } else {
614
613
  resolve({
615
614
  ok: false,
616
615
  msg: '加购失败,请稍后重试',
617
616
  ...res,
618
617
  });
619
618
  const data = {
620
619
  type: 'batchAddCart',
621
620
  subType: 'addCartFailByH5CallM',
622
621
  errMsg: 'H5调用M站加车失败',
623
622
  originData: res
624
623
  }
625
624
  draBusinessCustomReport(data)
626
625
  }
627
626
  }
628
627
  },
629
628
  failFun(err) {
630
629
  console.log('fail', err);
631
630
  resolve({
632
631
  ok: false,
633
632
  msg: '加购失败,请稍后重试',
634
633
  ...err,
635
634
  });
636
635
  const data = {
637
636
  type: 'batchAddCart',
638
637
  subType: 'addCartFailByH5CallM',
639
638
  errMsg: 'H5调用M站加车失败',
640
639
  originData: err
641
640
  }
642
641
  draBusinessCustomReport(data)
643
642
  },
644
643
  });
645
644
  } else {
646
645
  resolve({
647
646
  ok: false,
648
647
  msg: '购物车方法检查异常',
649
648
  });
650
649
  const data = {
651
650
  type: 'batchAddCart',
652
651
  subType: 'addCartFailByH5CallM',
653
652
  errMsg: 'H5调用M站加车检查依赖方法异常',
654
653
  }
655
654
  draBusinessCustomReport(data)
656
655
  }
657
656
  });
658
657
  }
659
658
  bMallGetUserInfo = () => {
660
659
  return getBMallUserInfo();
661
660
  };
662
661
  async bMallAddCart(params = {}) {
663
662
  return await this.gatewayReq('bmall_cartAdd', params, {
664
663
  client: 'H5',
665
664
  clientVersion: '5.5.0',
666
665
  });
667
666
  }
668
667
  async bMallChangeCart(params = {}) {
669
668
  return await this.gatewayReq('bmall_cartChange', params, {
670
669
  client: 'H5',
671
670
  clientVersion: '5.5.0',
672
671
  });
673
672
  }
674
673
  async bMallRemoveCart(params = {}) {
675
674
  return await this.gatewayReq(
676
675
  'bmall_cartRemove',
677
676
  params,
678
677
  {
679
678
  client: 'H5',
680
679
  clientVersion: '5.5.0',
681
680
  },
682
681
  );
683
682
  }
684
683
  async bMallGetOmittedProduct(params = {}) {
685
684
  return await this.gatewayReq('bmall_getOmittedProduct', params, {
686
685
  client: 'H5',
687
686
  clientVersion: '5.5.0',
688
687
  });
689
688
  }
690
689
  async bMallAddCartService({ skuId, bMallTag, num, data = {} }) {
691
690
  const params = await getParams({ ...data, skuId, bMallTag, num });
692
691
  return await this.bMallAddCart(params);
693
692
  }
694
693
  async bMallGetSkuNumService(bMallTag = 1) {
695
694
  const params = await getParams({ bMallTag });
696
695
  delete params.operations;
697
696
  return await this.bMallGetOmittedProduct(params);
698
697
  }
699
698
  async bMallRemoveCartService({ skuId, bMallTag, num, skuUuid, data = {} }) {
700
699
  let params = await getParams({ ...data, skuId, bMallTag, num, skuUuid });
701
700
  return await this.bMallRemoveCart(params);
702
701
  }
703
702
  async bMallChangeCartService({ skuId, bMallTag, num, skuUuid, data = {} }) {
704
703
  let params = await getParams({ ...data, skuId, bMallTag, num, skuUuid });
705
704
  return await this.bMallChangeCart(params);
706
705
  }
707
706
 
708
707
  async bMallGetSkuNumApi(needRequest = false, bMallTag = 1) {
709
708
  if (!needRequest && isH5 && window?.bMallCartData) {
710
709
  return window?.bMallCartData;
711
710
  }
712
711
  const cartData = await this.bMallGetSkuNumService(bMallTag);
713
712
  let items;
714
713
  if (cartData.ok && cartData.result) {
715
714
  items = cartData?.result?.items || {};
716
715
  }
717
716
  if (isH5) {
718
717
  window.bMallCartData = items;
719
718
  }
720
719
  return items;
721
720
  }
722
721
 
723
722
  async bMallCartHandle(goodItem, num) {
724
723
  try {
725
724
  const {
726
725
  skuUuid,
727
726
  limitUpper,
728
727
  limitLower,
729
728
  } = goodItem;
730
729
  let cartNum = Number(num);
731
730
  if (limitLower && cartNum && cartNum < limitLower) {
732
731
  cartNum = limitLower;
733
732
  } else if (limitUpper && cartNum && cartNum > limitUpper) {
734
733
  cartNum = limitUpper;
735
734
  }
736
735
  if (skuUuid && cartNum > 0) {
737
736
  return this.bMallChangeCartService({
738
737
  ...goodItem,
739
738
  skuUuid,
740
739
  num: cartNum,
741
740
  });
742
741
  } else if (!skuUuid && cartNum > 0) {
743
742
  return this.bMallAddCartService({ ...goodItem, num: cartNum });
744
743
  } else if (skuUuid && !cartNum) {
745
744
  return this.bMallRemoveCartService({
746
745
  ...goodItem,
747
746
  skuUuid,
748
747
  num: cartNum,
749
748
  });
750
749
  } else {
751
750
  console.error('参数不全');
752
751
  return false;
753
752
  }
754
753
  } catch (e) {
755
754
  console.error(e);
756
755
  }
757
756
  }
757
+ import Taro from '@tarojs/taro';
758
758
  APPID,
759
759
  JshopxColorFunctionIdList,
760
760
  PagesColorFunctionIdList,
761
761
  PAGES_DOMAIN,
762
762
  B2BColorFunctionIdList,
763
763
  MobileVerifyColorList,
764
764
  AppVerifyColorList,
765
765
  JshopxVerifyColorList,
766
766
  BMallVerifyColorList,
767
767
  shopMemberColorFunctionIdList,
768
768
  draBusinessCustomReport,
769
769
  isLanguageForEn,
770
770
  isJdApp,
771
771
  isIosDevice,
772
772
  isAndroidDevice,
773
773
  isJdAndHarmonyDevice,
774
774
  showSuccessToast,
775
775
  showFailToast,
776
776
  isH5,
777
777
  isWxMinAndWxapp,
778
778
  isMin,
779
779
  isPc,
780
780
  draInterfaceCustomReport,
781
781
  callRouterIOS,
782
782
  callRouterAndroid,
783
783
  sendNativePageToShowCartFloatBtn,
784
784
  cookiesStr: '',
785
785
  requestHeaderContentType: {
786
786
  jsonHeader: {
787
787
  'content-type': 'application/json;charset=utf-8',
788
788
  },
789
789
  formDataHeader: {
790
790
  'content-type': 'application/x-www-form-urlencoded',
791
791
  },
792
792
  },
793
793
  requestBaseParam: {
794
794
  data: null,
795
795
  header: {
796
796
  'content-type': 'application/json;charset=utf-8',
797
797
  },
798
798
  },
799
799
  timeOut: 7000,
800
800
  apiClientParams: {
801
801
  clientVersion: isJdApp && jdAppVersionStr !== '' ? jdAppVersionStr : '11.0.0',
802
802
  client: isJdApp ? (isIosDevice ? 'apple' : 'android') : isPc ? 'pc' : 'wh5',
803
803
  area: '1_72_2799_0',
804
804
  screen: `${Math.ceil(window.screen?.width * devicePixelRatio)}*${Math.ceil(
805
805
  window.screen?.height * devicePixelRatio,
806
806
  )}`,
807
807
  uuid: UUID,
808
808
  },
809
809
  public addressFetchInfo: {
810
810
  reqState: boolean;
811
811
  promiseInstance: null | Promise<any>;
812
812
  };
813
813
  public getPromotionConfig: {
814
814
  projectId: null | string | number;
815
815
  pageId: null | string | number;
816
816
  };
817
817
  public resInfo: ServiceInterFace.HttpRequestInfo;
818
818
  public api: ServiceInterFace.HttpApi;
819
819
  public isPageDomain: boolean;
820
820
  public verifyColorList: string[];
821
821
  private isShowJdShopCartFloatBtn: boolean;
822
822
  constructor(opt) {
823
823
  this._init();
824
824
  this.addressFetchInfo = {
825
825
  reqState: false,
826
826
  promiseInstance: null,
827
827
  };
828
828
  this.getPromotionConfig = {
829
829
  projectId: null,
830
830
  pageId: null,
831
831
  };
832
832
  this._userAreaUpdateListen();
833
833
  this.resInfo = this._getResConfig(opt);
834
834
  this.api = api;
835
835
  this.isPageDomain = isH5 ? window?.location?.hostname === PAGES_DOMAIN : false;
836
836
  this.verifyColorList = [
837
837
  ...MobileVerifyColorList,
838
838
  ...AppVerifyColorList,
839
839
  ...JshopxVerifyColorList,
840
840
  ...BMallVerifyColorList,
841
841
  ];
842
842
  this.isShowJdShopCartFloatBtn =
843
843
  jdAppVersionCompare(isJdAndIosDevice ? '13.8.6' : '13.6.2') >= 0;
844
844
  }
845
845
  _init(): void {
846
846
  httpInterceptors.forEach((item) => {
847
847
  Taro.addInterceptor((chain) => item(chain));
848
848
  });
849
849
  }
850
850
  _getResConfig(opt = {}): ServiceInterFace.HttpRequestInfo {
851
851
  return Object.assign({}, DefaultConfig, opt);
852
852
  }
853
853
 
854
854
  _userAreaUpdateListen() {
855
855
  Taro.eventCenter.on(TaroEventType.USER_AREA_UPDATE, (area) => {
856
856
  area &&
857
857
  area !== '' &&
858
858
  this.updateApiClientParamsResInfo({
859
859
  area,
860
860
  });
861
861
  console.log(
862
862
  'cone-render收到原生端获取传来的四级地址信息',
863
863
  area,
864
864
  '更新后的接口底层信息',
865
865
  this.resInfo.apiClientParams,
866
866
  );
867
867
  });
868
868
  }
869
869
  updateApiClientParamsResInfo(opt = {}) {
870
870
  const { area, ...others } = opt;
871
871
  this.resInfo.apiClientParams = {
872
872
  ...this.resInfo.apiClientParams,
873
873
  ...others,
874
874
  };
875
875
  if (area) {
876
876
  this.resInfo.apiClientParams.area = area;
877
877
  }
878
878
  }
879
879
 
880
880
  checkFunctionIdToReturnAppId(functionId) {
881
881
  if (JshopxColorFunctionIdList.includes(functionId)) {
882
882
  return APPID.JSHOPX_COLOR_APPID;
883
883
  } else if (this.isPageDomain && PagesColorFunctionIdList.includes(functionId)) {
884
884
  return APPID.PAGES_COLOR_APPID;
885
885
  } else if (shopMemberColorFunctionIdList.includes(functionId)) {
886
886
  return APPID.SHOP_MEMBER_COLOR_APPID;
887
887
  } else if (B2BColorFunctionIdList.includes(functionId)) {
888
888
  return APPID.B2B_COLOR_APPID;
889
889
  } else if (isPc) {
890
890
  return APPID.MALL_COLOR_APPID;
891
891
  } else {
892
892
  return APPID.SHOP_COLOR_APPID;
893
893
  }
894
894
  }
895
895
 
896
896
  gatewayReq(functionId: string, bodyParams = {}, clientParams = {}): Promise<any> {
897
897
  if (
898
898
  functionId === 'receiveShopCoupon' &&
899
899
  ((isH5 && (window?.isJingGouMiniViewState || window?.isJingxiMiniViewState)) ||
900
900
  isWxMinAndWxapp)
901
901
  ) {
902
902
  clientParams = Object.assign(
903
903
  {},
904
904
  {
905
905
  loginType: '1',
906
906
  loginWQBiz: WXAPP_BIZ_KEY,
907
907
  },
908
908
  clientParams,
909
909
  );
910
910
  }
911
911
  const apiClientParams = Object.assign({}, this.resInfo.apiClientParams, clientParams);
912
912
  typeof apiClientParams.appid === 'undefined' &&
913
913
  (apiClientParams.appid = this.checkFunctionIdToReturnAppId(functionId));
914
914
  const isColorVerify = this.verifyColorList.includes(functionId);
915
915
  const getReqData = {
916
916
  functionId: functionId,
917
917
  body: JSON.stringify(bodyParams),
918
918
  t: Date.now(),
919
919
  ...apiClientParams,
920
920
  };
921
921
  return http
922
922
  .request({
923
923
  url: this.api.apiFunc,
924
924
  data: getReqData,
925
925
  isColorVerify,
926
926
  })
927
927
  .then((res: ServiceInterFace.RequestPromiseRes) => {
928
928
  const { statusCode, data, status, msg } = res;
929
929
  let changeRes: ServiceInterFace.RequestResponseFormatRes = {
930
930
  ok: true,
931
931
  };
932
932
  if ((statusCode === 200 || status === '0') && data) {
933
933
  changeRes = this.dealResponseFormatRes(data);
934
934
  return changeRes;
935
935
  } else {
936
936
  changeRes = {
937
937
  ...data,
938
938
  statusCode,
939
939
  status,
940
940
  msg,
941
941
  ok: false,
942
942
  };
943
943
  return changeRes;
944
944
  }
945
945
  })
946
946
  .catch((res) => {
947
947
  const propertyNames = Object.getOwnPropertyNames(res);
948
948
  const resObject = {};
949
949
  propertyNames.forEach((propertyName) => {
950
950
  resObject[propertyName] = res[propertyName];
951
951
  });
952
952
  draInterfaceCustomReport({
953
953
  subMsg:
954
954
  '接口请求异常,进入catch逻辑,例如h5请求神盾(反爬、小号、风控)或加固拦截返回status为403等',
955
955
  url: this.api.apiFunc,
956
956
  source: 'remote',
957
957
  requestType: res?.requestType,
958
958
  errorType: 'catchError',
959
959
  functionId: getReqData?.functionId,
960
960
  appid: getReqData?.appid,
961
961
  client: getReqData?.client,
962
962
  clientVersion: getReqData?.clientVersion,
963
963
  statusCode: res?.statusCode,
964
964
  status: res?.status,
965
965
  statusText: res?.statusText,
966
966
  code: res?.data?.code || res?.data?.subCode,
967
967
  errMsg: res?.errMsg || res?.msg || res?.message || res?.data?.echo,
968
968
  originReqDataStr: JSON.stringify(getReqData),
969
969
  originResDataStr: JSON.stringify(res, propertyNames),
970
970
  });
971
971
  return {
972
972
  ...resObject,
973
973
  ok: false,
974
974
  } as ServiceInterFace.RequestResponseFormatRes;
975
975
  });
976
976
  }
977
977
 
978
978
  dealResponseFormatRes(data) {
979
979
  let changeRes: ServiceInterFace.RequestResponseFormatRes = {
980
980
  ok: true,
981
981
  };
982
982
  const resCode = Object.prototype.hasOwnProperty.call(data, 'code') ? Number(data.code) : -1;
983
983
  const subCode = Object.prototype.hasOwnProperty.call(data, 'subCode')
984
984
  ? Number(data.subCode)
985
985
  : -1;
986
986
  if (
987
987
  data.success === true ||
988
988
  data.isSuccess ||
989
989
  resCode === 0 ||
990
990
  resCode === 200 ||
991
991
  subCode === 0
992
992
  ) {
993
993
  changeRes = {
994
994
  result: Object.prototype.hasOwnProperty.call(data, 'result')
995
995
  ? data.result
996
996
  : Object.prototype.hasOwnProperty.call(data, 'data')
997
997
  ? data.data
998
998
  : data,
999
999
  code: data.code ?? 200,
1000
1000
  ok: true,
1001
1001
  source: data,
1002
1002
  };
1003
1003
  return changeRes;
1004
1004
  } else {
1005
1005
  changeRes = {
1006
1006
  ...data,
1007
1007
  ok: false,
1008
1008
  };
1009
1009
  return changeRes;
1010
1010
  }
1011
1011
  }
1012
1012
 
1013
1013
  async receiveCoupon(param = {}, clientParams) {
1014
1014
  const loginType = isPc ? { loginType: 3 } : {};
1015
1015
  return await this.gatewayReq(
1016
1016
  'receiveShopCoupon',
1017
1017
  {
1018
1018
  operation: '3',
1019
1019
  ...param,
1020
1020
  },
1021
1021
  { ...loginType, ...clientParams },
1022
1022
  );
1023
1023
  }
1024
1024
 
1025
1025
  getCustomerCenterDispatch(params = {}) {
1026
1026
  return this.gatewayReq('customerCenterDispatch', params, {
1027
1027
  clientVersion: '9.2.0',
1028
1028
  client: 'H5',
1029
1029
  uuid: 88888,
1030
1030
  appid: 'shopmember_m_jd_com',
1031
1031
  });
1032
1032
  }
1033
1033
 
1034
1034
  batchAddCart(...args) {
1035
1035
  let skuIds, num, showToast, giftCard;
1036
1036
  if (['number', 'string'].includes(typeof args[0])) {
1037
1037
  [skuIds, num = 1, showToast = true, giftCard = false] = args;
1038
1038
  } else if (typeof args[0] === 'object' && args[0] !== null) {
1039
1039
  const params = args[0];
1040
1040
  skuIds = params.skuIds;
1041
1041
  num = params.num || 1;
1042
1042
  showToast = params.showToast !== undefined ? params.showToast : true;
1043
1043
  giftCard = params.giftCard !== undefined ? params.giftCard : false;
1044
1044
  } else {
1045
1045
  console.error('batchAddCart() Invalid arguments!', args);
1046
1046
  try {
1047
1047
  const data = {
1048
1048
  type: 'batchAddCart',
1049
1049
  subType: 'invalidInputParameter',
1050
1050
  errMsg: '入参不合法',
1051
1051
  originData: args,
1052
1052
  };
1053
1053
  draBusinessCustomReport(data);
1054
1054
  } catch (e) {
1055
1055
  console.error('draBusinessCustomReport error:', e);
1056
1056
  }
1057
1057
  const msg = isLanguageForEn ? 'Please retry later' : '加入购物车失败,请稍后重试!';
1058
1058
  showFailToast({
1059
1059
  title: msg,
1060
1060
  });
1061
1061
  return Promise.resolve({
1062
1062
  ok: false,
1063
1063
  msg,
1064
1064
  });
1065
1065
  }
1066
1066
  const addToWhereText = giftCard ? '购物清单' : '购物车';
1067
1067
  const successMsg = isLanguageForEn ? 'Success' : `加入${addToWhereText}成功`;
1068
1068
  const failMsg = isLanguageForEn
1069
1069
  ? 'Please retry later'
1070
1070
  : `加入${addToWhereText}失败,请稍后重试!`;
1071
1071
  if (skuIds) {
1072
1072
  return this._addShopGoodCart(`${skuIds}`, `${num}`, { giftCard })
1073
1073
  .then((res: ServiceInterFace.SkuToCartDataRes) => {
1074
1074
  if (res?.ok) {
1075
1075
  showToast &&
1076
1076
  !res?.isOpenApp &&
1077
1077
  showSuccessToast({
1078
1078
  title: successMsg,
1079
1079
  });
1080
1080
  this.isShowJdShopCartFloatBtn &&
1081
1081
  sendNativePageToShowCartFloatBtn({
1082
1082
  isShow: true,
1083
1083
  });
1084
1084
  } else {
1085
1085
  showToast &&
1086
1086
  showFailToast({
1087
1087
  title: res?.errMsg || failMsg,
1088
1088
  });
1089
1089
  }
1090
1090
  return Promise.resolve(res);
1091
1091
  })
1092
1092
  .catch((e) => {
1093
1093
  console.log('调用加购方法异常=======>', e);
1094
1094
  return Promise.resolve({
1095
1095
  ok: false,
1096
1096
  msg: failMsg,
1097
1097
  });
1098
1098
  });
1099
1099
  } else {
1100
1100
  try {
1101
1101
  const data = {
1102
1102
  type: 'batchAddCart',
1103
1103
  subType: 'invalidInputParameter',
1104
1104
  errMsg: '没有传入合法的skuIds',
1105
1105
  originData: skuIds,
1106
1106
  };
1107
1107
  draBusinessCustomReport(data);
1108
1108
  } catch (e) {
1109
1109
  console.error('draBusinessCustomReport error:', e);
1110
1110
  }
1111
1111
  return Promise.resolve({
1112
1112
  ok: false,
1113
1113
  msg: failMsg,
1114
1114
  });
1115
1115
  }
1116
1116
  }
1117
1117
 
1118
1118
  async giftCardAddCart(skuList, num) {
1119
1119
  const loginType = isPc ? { loginType: 3 } : {};
1120
1120
  return this.gatewayReq(
1121
1121
  'giftCardAddCart',
1122
1122
  {
1123
1123
  skuList,
1124
1124
  num: Number(num),
1125
1125
  },
1126
1126
  { ...loginType },
1127
1127
  );
1128
1128
  }
1129
1129
 
1130
1130
  _addShopGoodCart(skuIdsStr, addCardNum = '1', { giftCard }) {
1131
1131
  return new Promise((resolve) => {
1132
1132
  const productIdsArr = skuIdsStr ? `${skuIdsStr}`.split(',') : [];
1133
1133
  if (productIdsArr.length > 0) {
1134
1134
  if (giftCard) {
1135
1135
  this.giftCardAddCart(productIdsArr, addCardNum)
1136
1136
  .then((res) => {
1137
1137
  if (res?.code === '0' && res?.result?.value) {
1138
1138
  resolve({
1139
1139
  ok: true,
1140
1140
  });
1141
1141
  } else {
1142
1142
  try {
1143
1143
  const data = {
1144
1144
  type: 'batchAddCart',
1145
1145
  subType: 'addCartFailByGiftCard',
1146
1146
  errMsg: '礼品卡加车接口返回加车失败标识',
1147
1147
  originData: res,
1148
1148
  };
1149
1149
  draBusinessCustomReport(data);
1150
1150
  } catch (e) {
1151
1151
  console.error('draBusinessCustomReport error:', e);
1152
1152
  }
1153
1153
  resolve({
1154
1154
  ok: false,
1155
1155
  msg: '加购失败,请稍后重试',
1156
1156
  });
1157
1157
  }
1158
1158
  })
1159
1159
  .catch((e) => {
1160
1160
  console.error('礼品卡加车失败: ', e);
1161
1161
  resolve({
1162
1162
  ok: false,
1163
1163
  msg: '加购失败,请稍后重试',
1164
1164
  });
1165
1165
  });
1166
1166
  }
1167
1167
  else if (isJdApp && !isJdAndHarmonyDevice) {
1168
1168
  const nativeAddCartCallBack = `nativeBatchAddCart${
1169
1169
  Date.now() + Number(productIdsArr[0]) + Number(addCardNum)
1170
1170
  }`;
1171
1171
  console.log(
1172
1172
  '==========>',
1173
1173
  '原生批量加车, 回调方法保证唯一添加skuid和数量,nativeAddCartCallBack',
1174
1174
  nativeAddCartCallBack,
1175
1175
  );
1176
1176
  if (!window[nativeAddCartCallBack]) {
1177
1177
  window[nativeAddCartCallBack] = (res) => {
1178
1178
  const getMessageJsonData = typeof res === 'string' ? JSON.parse(res) : res;
1179
1179
  console.info(
1180
1180
  '获取批量加车原始数据',
1181
1181
  res,
1182
1182
  '====转换后的json数据为===',
1183
1183
  getMessageJsonData,
1184
1184
  );
1185
1185
  try {
1186
1186
  if (isAndroidDevice) {
1187
1187
  const isOk = getMessageJsonData?.status === '0';
1188
1188
  isOk
1189
1189
  ? resolve({
1190
1190
  ok: true,
1191
1191
  msg: '已加入购物车!',
1192
1192
  })
1193
1193
  : resolve({
1194
1194
  ok: false,
1195
1195
  msg: '加购失败,请稍后重试',
1196
1196
  errMsg: getMessageJsonData?.data?.message ?? '',
1197
1197
  });
1198
1198
  if (!isOk) {
1199
1199
  const data = {
1200
1200
  type: 'batchAddCart',
1201
1201
  subType: 'addCartFailByH5CallAndroidNative',
1202
1202
  errMsg: 'H5调用安卓原生加车失败',
1203
1203
  originData: getMessageJsonData,
1204
1204
  };
1205
1205
  draBusinessCustomReport(data);
1206
1206
  }
1207
1207
  } else if (isIosDevice) {
1208
1208
  const isOk =
1209
1209
  getMessageJsonData?.status === '0' && getMessageJsonData?.data?.code === 0;
1210
1210
  isOk
1211
1211
  ? resolve({
1212
1212
  ok: true,
1213
1213
  msg: '已加入购物车!',
1214
1214
  })
1215
1215
  : resolve({
1216
1216
  ok: false,
1217
1217
  msg: '批量加车失败,请稍后重试',
1218
1218
  errMsg: getMessageJsonData?.data?.message ?? '',
1219
1219
  });
1220
1220
  if (!isOk) {
1221
1221
  const data = {
1222
1222
  type: 'batchAddCart',
1223
1223
  subType: 'addCartFailByH5CallIosNative',
1224
1224
  errMsg: 'H5调用IOS原生加车失败',
1225
1225
  originData: getMessageJsonData,
1226
1226
  };
1227
1227
  draBusinessCustomReport(data);
1228
1228
  }
1229
1229
  } else {
1230
1230
  resolve({
1231
1231
  ok: false,
1232
1232
  msg: '加购失败,请稍后重试',
1233
1233
  });
1234
1234
  const data = {
1235
1235
  type: 'batchAddCart',
1236
1236
  subType: 'addCartFailByH5CallUnknownNative',
1237
1237
  errMsg: 'H5调用未知原生加车失败',
1238
1238
  };
1239
1239
  draBusinessCustomReport(data);
1240
1240
  }
1241
1241
  } catch (e) {
1242
1242
  resolve({
1243
1243
  ok: false,
1244
1244
  msg: '加购失败,请稍后重试',
1245
1245
  });
1246
1246
  console.warn('批量加车转换数据异常', e);
1247
1247
  const data = {
1248
1248
  type: 'batchAddCart',
1249
1249
  subType: 'addCartFailByTransformData',
1250
1250
  errMsg: '批量加车转换数据异常',
1251
1251
  originData: e,
1252
1252
  };
1253
1253
  draBusinessCustomReport(data);
1254
1254
  }
1255
1255
  window[nativeAddCartCallBack] = null;
1256
1256
  };
1257
1257
  }
1258
1258
  const productsAddCartList = productIdsArr.map((skuItem) => {
1259
1259
  return isAndroidDevice
1260
1260
  ? {
1261
1261
  sku: skuItem,
1262
1262
  num: addCardNum,
1263
1263
  }
1264
1264
  : {
1265
1265
  productCode: skuItem,
1266
1266
  buyCount: addCardNum,
1267
1267
  };
1268
1268
  });
1269
1269
  if (isAndroidDevice) {
1270
1270
  const androidProductsStr = JSON.stringify(productsAddCartList);
1271
1271
  callRouterAndroid({
1272
1272
  routerURL: `router://JDCartModule/addCartUniformWithUrl?wareList=${androidProductsStr}&businessName=storeModel`,
1273
1273
  routerParam: {
1274
1274
  wareList: productsAddCartList,
1275
1275
  },
1276
1276
  callBackName: nativeAddCartCallBack,
1277
1277
  });
1278
1278
  } else if (isIosDevice) {
1279
1279
  if (isLowAppVer) {
1280
1280
  const lowVersionAppList = productIdsArr.map((skuItem) => {
1281
1281
  return {
1282
1282
  sku: skuItem,
1283
1283
  num: addCardNum,
1284
1284
  };
1285
1285
  });
1286
1286
  const lowVersionAppListStr = JSON.stringify(lowVersionAppList);
1287
1287
  const url = `openApp.jdMobile://virtual?params={"category":"jump","des":"updateCartNum","sourceValue":"店铺批量加购","sourceType":"h5","businessName":"storeModel","wareList":${lowVersionAppListStr}}`;
1288
1288
  jump.side.jdJumpToWeb(url, {});
1289
1289
  resolve({
1290
1290
  ok: true,
1291
1291
  isOpenApp: true,
1292
1292
  msg: '加购完成, 加购结果请点击购物车确认',
1293
1293
  });
1294
1294
  } else {
1295
1295
  callRouterIOS({
1296
1296
  routerURL: 'router://JDCartModule/batchAddService',
1297
1297
  routerParam: {
1298
1298
  sourceType: 'h5',
1299
1299
  sourceValue: '店铺批量加购',
1300
1300
  businessName: 'storeModel',
1301
1301
  isNewMode: '1',
1302
1302
  NotPassWebview: '1',
1303
1303
  products: productsAddCartList,
1304
1304
  },
1305
1305
  callBackName: nativeAddCartCallBack,
1306
1306
  });
1307
1307
  }
1308
1308
  }
1309
1309
  } else if (isMin) {
1310
1310
  console.log('==========>', '京购批量加车!');
1311
1311
  const productsAddCartList = productIdsArr.map((skuItem) => {
1312
1312
  return {
1313
1313
  skuId: skuItem,
1314
1314
  buyNum: addCardNum,
1315
1315
  };
1316
1316
  });
1317
1317
  global.wxAppBizInstance
1318
1318
  .addCart({ commArr: productsAddCartList })
1319
1319
  .then((res) => {
1320
1320
  console.log('dealCartSuccess: ', res);
1321
1321
  resolve({
1322
1322
  ok: true,
1323
1323
  msg: '已加入购物车!',
1324
1324
  data: res,
1325
1325
  });
1326
1326
  })
1327
1327
  .catch((e) => {
1328
1328
  console.log('京购小程序批量加车失败dealCartFail: ', e);
1329
1329
  resolve({
1330
1330
  ok: false,
1331
1331
  msg: '加购失败,请稍后重试',
1332
1332
  });
1333
1333
  const data = {
1334
1334
  type: 'batchAddCart',
1335
1335
  subType: 'addCartFailByH5CallMini',
1336
1336
  errMsg: 'H5调用小程序加车失败',
1337
1337
  originData: e,
1338
1338
  };
1339
1339
  draBusinessCustomReport(data);
1340
1340
  });
1341
1341
  } else {
1342
1342
  console.log('==========>', 'M站批量加车!');
1343
1343
  return this.productAddToCart(skuIdsStr, addCardNum).then((res) => resolve(res));
1344
1344
  }
1345
1345
  } else {
1346
1346
  resolve({
1347
1347
  ok: false,
1348
1348
  msg: '加购sku不能为空',
1349
1349
  });
1350
1350
  const data = {
1351
1351
  type: 'batchAddCart',
1352
1352
  subType: 'addCartFailNoSku',
1353
1353
  errMsg: '加购sku不能为空',
1354
1354
  originData: skuIdsStr,
1355
1355
  };
1356
1356
  draBusinessCustomReport(data);
1357
1357
  }
1358
1358
  });
1359
1359
  }
1360
1360
 
1361
1361
  productAddToCart(skuIdsStr, num) {
1362
1362
  return new Promise((resolve) => {
1363
1363
  if (window.AddcartToolObj) {
1364
1364
  window.AddcartToolObj.addCart({
1365
1365
  skuId: skuIdsStr,
1366
1366
  num,
1367
1367
  source: 'sjdpyw',
1368
1368
  actId: '1',
1369
1369
  loginType: window?.isJingGouMiniViewState ? '11' : '2',
1370
1370
  sucFun(res) {
1371
1371
  console.log('ok,接口请求成功,不一定指的加车成功,加车成功需要看返回码errId为0', res);
1372
1372
  if (res.errId === '0' && res.cartJson) {
1373
1373
  resolve({
1374
1374
  ok: true,
1375
1375
  msg: '已加入购物车!',
1376
1376
  ...res,
1377
1377
  });
1378
1378
  } else {
1379
1379
  if (res.errId === '13') {
1380
1380
  global.doLogin();
1381
1381
  } else {
1382
1382
  resolve({
1383
1383
  ok: false,
1384
1384
  msg: '加购失败,请稍后重试',
1385
1385
  ...res,
1386
1386
  });
1387
1387
  const data = {
1388
1388
  type: 'batchAddCart',
1389
1389
  subType: 'addCartFailByH5CallM',
1390
1390
  errMsg: 'H5调用M站加车失败',
1391
1391
  originData: res,
1392
1392
  };
1393
1393
  draBusinessCustomReport(data);
1394
1394
  }
1395
1395
  }
1396
1396
  },
1397
1397
  failFun(err) {
1398
1398
  console.log('fail', err);
1399
1399
  resolve({
1400
1400
  ok: false,
1401
1401
  msg: '加购失败,请稍后重试',
1402
1402
  ...err,
1403
1403
  });
1404
1404
  const data = {
1405
1405
  type: 'batchAddCart',
1406
1406
  subType: 'addCartFailByH5CallM',
1407
1407
  errMsg: 'H5调用M站加车失败',
1408
1408
  originData: err,
1409
1409
  };
1410
1410
  draBusinessCustomReport(data);
1411
1411
  },
1412
1412
  });
1413
1413
  } else {
1414
1414
  resolve({
1415
1415
  ok: false,
1416
1416
  msg: '购物车方法检查异常',
1417
1417
  });
1418
1418
  const data = {
1419
1419
  type: 'batchAddCart',
1420
1420
  subType: 'addCartFailByH5CallM',
1421
1421
  errMsg: 'H5调用M站加车检查依赖方法异常',
1422
1422
  };
1423
1423
  draBusinessCustomReport(data);
1424
1424
  }
1425
1425
  });
1426
1426
  }
1427
1427
  bMallGetUserInfo = () => {
1428
1428
  return getBMallUserInfo();
1429
1429
  };
1430
1430
  async bMallAddCart(params = {}) {
1431
1431
  return await this.gatewayReq('bmall_cartAdd', params, {
1432
1432
  client: 'H5',
1433
1433
  clientVersion: '5.5.0',
1434
1434
  });
1435
1435
  }
1436
1436
  async bMallChangeCart(params = {}) {
1437
1437
  return await this.gatewayReq('bmall_cartChange', params, {
1438
1438
  client: 'H5',
1439
1439
  clientVersion: '5.5.0',
1440
1440
  });
1441
1441
  }
1442
1442
  async bMallRemoveCart(params = {}) {
1443
1443
  return await this.gatewayReq(
1444
1444
  'bmall_cartRemove',
1445
1445
  params,
1446
1446
  {
1447
1447
  client: 'H5',
1448
1448
  clientVersion: '5.5.0',
1449
1449
  },
1450
1450
  );
1451
1451
  }
1452
1452
  async bMallGetOmittedProduct(params = {}) {
1453
1453
  return await this.gatewayReq('bmall_getOmittedProduct', params, {
1454
1454
  client: 'H5',
1455
1455
  clientVersion: '5.5.0',
1456
1456
  });
1457
1457
  }
1458
1458
  async bMallAddCartService({ skuId, bMallTag, num, data = {} }) {
1459
1459
  const params = await getParams({ ...data, skuId, bMallTag, num });
1460
1460
  return await this.bMallAddCart(params);
1461
1461
  }
1462
1462
  async bMallGetSkuNumService(bMallTag = 1) {
1463
1463
  const params = await getParams({ bMallTag });
1464
1464
  delete params.operations;
1465
1465
  return await this.bMallGetOmittedProduct(params);
1466
1466
  }
1467
1467
  async bMallRemoveCartService({ skuId, bMallTag, num, skuUuid, data = {} }) {
1468
1468
  let params = await getParams({ ...data, skuId, bMallTag, num, skuUuid });
1469
1469
  return await this.bMallRemoveCart(params);
1470
1470
  }
1471
1471
  async bMallChangeCartService({ skuId, bMallTag, num, skuUuid, data = {} }) {
1472
1472
  let params = await getParams({ ...data, skuId, bMallTag, num, skuUuid });
1473
1473
  return await this.bMallChangeCart(params);
1474
1474
  }
1475
1475
 
1476
1476
  async bMallGetSkuNumApi(needRequest = false, bMallTag = 1) {
1477
1477
  if (!needRequest && isH5 && window?.bMallCartData) {
1478
1478
  return window?.bMallCartData;
1479
1479
  }
1480
1480
  const cartData = await this.bMallGetSkuNumService(bMallTag);
1481
1481
  let items;
1482
1482
  if (cartData.ok && cartData.result) {
1483
1483
  items = cartData?.result?.items || {};
1484
1484
  }
1485
1485
  if (isH5) {
1486
1486
  window.bMallCartData = items;
1487
1487
  }
1488
1488
  return items;
1489
1489
  }
1490
1490
 
1491
1491
  async bMallCartHandle(goodItem, num) {
1492
1492
  try {
1493
1493
  const {
1494
1494
  skuUuid,
1495
1495
  limitUpper,
1496
1496
  limitLower,
1497
1497
  } = goodItem;
1498
1498
  let cartNum = Number(num);
1499
1499
  if (limitLower && cartNum && cartNum < limitLower) {
1500
1500
  cartNum = limitLower;
1501
1501
  } else if (limitUpper && cartNum && cartNum > limitUpper) {
1502
1502
  cartNum = limitUpper;
1503
1503
  }
1504
1504
  if (skuUuid && cartNum > 0) {
1505
1505
  return this.bMallChangeCartService({
1506
1506
  ...goodItem,
1507
1507
  skuUuid,
1508
1508
  num: cartNum,
1509
1509
  });
1510
1510
  } else if (!skuUuid && cartNum > 0) {
1511
1511
  return this.bMallAddCartService({ ...goodItem, num: cartNum });
1512
1512
  } else if (skuUuid && !cartNum) {
1513
1513
  return this.bMallRemoveCartService({
1514
1514
  ...goodItem,
1515
1515
  skuUuid,
1516
1516
  num: cartNum,
1517
1517
  });
1518
1518
  } else {
1519
1519
  console.error('参数不全');
1520
1520
  return false;
1521
1521
  }
1522
1522
  } catch (e) {
1523
1523
  console.error(e);
1524
1524
  }
1525
1525
  }