@conecli/cone-render 0.10.1-shop3.24 → 0.10.1-shop3.25

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