@conecli/cone-render 0.10.1-shop-beta.35 → 0.10.1-shop-beta.36

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