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

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