@conecli/cone-render 0.10.1-shop3.61 → 0.10.1-shop3.62

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