@conecli/cone-render 0.9.1-shop2.19 → 0.9.1-shop2.20

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
  isIosDevice,
3
2
  isAndroidDevice,
4
3
  isJdAndHarmonyDevice,
5
4
  isJdAndAndroidDevice,
6
5
  jdAppVersion,
7
6
  jdAppVersionStr,
8
7
  isString,
9
8
  isObject,
10
9
  serialize,
11
10
  dealNativePixelToCssPixel,
12
11
  isPc,
13
12
  BUSINESS_TYPE,
14
13
  JSSDK_APP_WEBVIEW_CODE,
15
14
  SECTION_HOME_TAB_NAME_TYPE,
16
15
  SECTION_HOME_TAB_TYPE,
17
16
  WXAPP_BIZ_KEY,
18
17
  WX_BUSINESS_TYPE,
19
18
  MPAAS_CONFIG_APP_VERSION,
20
19
  MPAAS_CONFIG_APP_LOW_VERSION,
21
20
  WXAPP_BIZ_SHOP_LIGHT_KEY,
22
21
  LoadJsInitLoadEnvType,
23
22
  LoadJsInitLoadType,
24
23
  LoadJsInitTriggerType
25
24
  loginState: false,
26
25
  cookiesStr: '',
27
26
  userInfo: userPinKey,
28
27
  isJingGouMiniViewState: false,
29
28
  isJingxiMiniViewState: false,
30
29
  pageInfo: {
31
30
  wxBusinessType: WX_BUSINESS_TYPE.NO,
32
31
  address: '',
33
32
  addressCommaStr: '',
34
33
  un_area: '',
35
34
  vapptype: '1',
36
35
  pageType: 'home',
37
36
  isExposureState: false,
38
37
  moduleId: '',
39
38
  entrance: '',
40
39
  dataType: BUSINESS_TYPE.ONLINE,
41
40
  floorExposureInfo: {},
42
41
  floorVideInfo: {},
43
42
  productVideInfo: {},
44
43
  tabsLoadAllDataInfo: {
45
44
  [SECTION_HOME_TAB_NAME_TYPE[
46
45
  SECTION_HOME_TAB_TYPE.HOME_WELL_CHOSEN
47
46
  ]]: false,
48
47
  },
49
48
  updateShopInfosAllState: false,
50
49
  isVipShop: false,
51
50
  isJdShowNativeImmersivePlayer: false,
52
51
  ...shopConfig,
53
52
  pageScrollTop: 0,
54
53
  pageIdxHeightInfo: {
55
54
  list: [],
56
55
  },
57
56
  shopNavBarHeight: 0,
58
57
  },
59
58
  defaultQueryLogInfo: {
60
59
  sourceType: 'JDshop',
61
60
  sourceValue: '',
62
61
  moduleId: 'none',
63
62
  entrance: 'none',
64
63
  },
65
64
  sysInfo: {
66
65
  windowWidth: isPc ? 375 : 0,
67
66
  containerWidth: isPc ? 375 : 0,
68
67
  windowHeight: 0,
69
68
  netWorkType: '4g',
70
69
  jdBottomBarHeight: 0,
71
70
  jdNativeHeaderHeight: 0,
72
71
  },
73
72
  queryInfo: {},
74
73
  shopInfo: {},
75
74
  openAppData: {},
76
75
  public info: CommonInterFace.BaseConfigInfo
77
76
  public config: {
78
77
  [key: string]: any
79
78
  }
80
79
  public lazyContainer: CommonInterFace.lazyContainer
81
80
  public renderedIsvComponents: CommonInterFace.renderedIsvComponents
82
81
  public rootEleNode: HTMLElement | null
83
82
  public checkStatusAndLoginPromise: object | null
84
83
  private jmfeRegisterStatePromise: Promise<any> | null
85
84
  private jmfeRegisterState: boolean
86
85
  public loadJsSdkList: Array<any>
87
86
  constructor(opt) {
88
87
  this.info = this._getConfig(opt)
89
88
  this.config = {}
90
89
  this.loadJsSdkList = []
91
90
  this.lazyContainer = {
92
91
  [SECTION_HOME_TAB_NAME_TYPE[SECTION_HOME_TAB_TYPE.HOME_WELL_CHOSEN]]: {
93
92
  appLazyContainerList: [],
94
93
  appLazyFinishContainerList: [],
95
94
  },
96
95
  [SECTION_HOME_TAB_NAME_TYPE[SECTION_HOME_TAB_TYPE.HOME_PROMOTION]]: {
97
96
  appLazyContainerList: [],
98
97
  appLazyFinishContainerList: [],
99
98
  },
100
99
  }
101
100
  this.renderedIsvComponents = {}
102
101
  this.rootEleNode = document.querySelector('body')
103
102
  this.checkStatusAndLoginPromise = null
104
103
  this.jmfeRegisterStatePromise = null
105
104
  this.loadOtherSdk()
106
105
  isJdApp && this.jmfeReayPromise()
107
106
  }
108
107
  _getConfig(opt) {
109
108
  return Object.assign({}, DefaultConfig, opt)
110
109
  }
111
110
 
112
111
  jmfeReayPromise(): Promise<any> {
113
112
  if (isJdApp) {
114
113
  if (this.jmfeRegisterState) {
115
114
  return Promise.resolve(true)
116
115
  } else {
117
116
  !this.jmfeRegisterStatePromise &&
118
117
  (this.jmfeRegisterStatePromise = new Promise((resolve, reject) => {
119
118
  ready('jmfe', 3000)
120
119
  .then(() => {
121
120
  window?.jmfe && window.jmfe.registerCode(JSSDK_APP_WEBVIEW_CODE)
122
121
  this.jmfeRegisterState = true
123
122
  resolve(true)
124
123
  console.log('松果app内初始化注册jmfe认证完成')
125
124
  })
126
125
  .catch(() => {
127
126
  reject(false)
128
127
  })
129
128
  }))
130
129
  return this.jmfeRegisterStatePromise
131
130
  }
132
131
  } else {
133
132
  return Promise.reject(false)
134
133
  }
135
134
  }
136
135
 
137
136
  updateBusinessDomainAndApi(domain, api) {
138
137
  }
139
138
 
140
139
  formatNativeScreenPageData(action) {
141
140
  let getChangePageInfo: any = null
142
141
  try {
143
142
  const getNativeScreenPageInfoStr = window.XWebView?._callNative(
144
143
  JSON.stringify({
145
144
  plugin: 'JDHybridScreenPlugin',
146
145
  action,
147
146
  sync: '1',
148
147
  }),
149
148
  )
150
149
  const getChangePageInfoData =
151
150
  typeof getNativeScreenPageInfoStr === 'string'
152
151
  ? JSON.parse(getNativeScreenPageInfoStr)
153
152
  : null
154
153
  if (getChangePageInfoData && typeof getChangePageInfoData === 'object') {
155
154
  const { code, data } = getChangePageInfoData
156
155
  getChangePageInfo = code && code === '0' ? data : null
157
156
  }
158
157
  } catch (e) {
159
158
  console.log('JDHybridScreenPlugin转换异常', e)
160
159
  }
161
160
  return getChangePageInfo
162
161
  }
163
162
 
164
163
  isAndroidFoldScreen() {
165
164
  return this.formatNativeScreenPageData('isFoldScreen') === '1'
166
165
  }
167
166
 
168
167
  getJdAndroidPageChangeScreenInfo() {
169
168
  const getPageScreenInfo = this.formatNativeScreenPageData('getScreenSize')
170
169
  if (
171
170
  getPageScreenInfo &&
172
171
  getPageScreenInfo?.pageWidth &&
173
172
  getPageScreenInfo?.pageHeight
174
173
  ) {
175
174
  const { pageWidth, pageHeight } = dealNativePixelToCssPixel({
176
175
  pageWidth: getPageScreenInfo.pageWidth,
177
176
  pageHeight: getPageScreenInfo.pageHeight,
178
177
  })
179
178
  getPageScreenInfo.pageWidth = pageWidth
180
179
  getPageScreenInfo.pageHeight = pageHeight
181
180
  }
182
181
  return getPageScreenInfo
183
182
  }
184
183
 
185
184
  getSystemInfo(params) {
186
185
  let info: UtilsInterFace.taroGetSystemInfoSyncRes | {} = isPc
187
186
  ? {}
188
187
  : getSystemInfos(params)
189
188
  if (isJdAndAndroidDevice && window.innerWidth <= 0) {
190
189
  const isfoldScreen = this.isAndroidFoldScreen()
191
190
  if (isfoldScreen) {
192
191
  const getJdAndroidPageInfo = this.getJdAndroidPageChangeScreenInfo()
193
192
  getJdAndroidPageInfo && (info = getSystemInfos(getJdAndroidPageInfo))
194
193
  console.info(
195
194
  '安卓松果app折叠屏获取webview宽度为0,那么通过内部方法重置获取',
196
195
  getJdAndroidPageInfo,
197
196
  )
198
197
  }
199
198
  }
200
199
  this.info.sysInfo = {
201
200
  actualNavBarHeight: 0,
202
201
  ...this.info.sysInfo,
203
202
  ...info,
204
203
  safeContentHeight: info?.screenHeight,
205
204
  headerHeight: 0,
206
205
  tabBarHeight: 0,
207
206
  }
208
207
  if (isJdApp) {
209
208
  this.info.sysInfo['hostVersionName'] = jdAppVersionStr
210
209
  this.info.sysInfo['hostAppVersion'] = jdAppVersion
211
210
  this.getAddressCachePromise()
212
211
  this.getElderModePromise()
213
212
  this.getJDAppearanceStatePromise()
214
213
  this.createJdAndroidRquestEventForTouchStart()
215
214
  }
216
215
  this.getWifiVideoAutoPlayAsync()
217
216
  this.getMPaasConfigAsync()
218
217
  this.getNetWorkType()
219
218
  }
220
219
 
221
220
  taskTimeoutPromise(callBack, timeout = 2000) {
222
221
  return new Promise(resolve => {
223
222
  setTimeout(() => {
224
223
  const getCallBackRes = typeof callBack === 'function' && callBack()
225
224
  return resolve(getCallBackRes || false)
226
225
  }, timeout)
227
226
  })
228
227
  }
229
228
 
230
229
  getElderModePromise() {
231
230
  if (this.info.sysInfo.hasOwnProperty('jdAppModeType')) {
232
231
  return Promise.resolve(this.info.sysInfo.jdAppModeType)
233
232
  } else {
234
233
  if (isJdAndAndroidDevice) {
235
234
  this.info.sysInfo.jdAppModeType = '0'
236
235
  return Promise.resolve(this.info.sysInfo.jdAppModeType)
237
236
  } else {
238
237
  return Promise.race([
239
238
  this.taskTimeoutPromise(() => {
240
239
  this.info.sysInfo.jdAppModeType = '0'
241
240
  return this.info.sysInfo.jdAppModeType
242
241
  }),
243
242
  new Promise(resolve => {
244
243
  const getCallBackName = `getJdCurrentModeType${Date.now()}`
245
244
  if (!window[getCallBackName]) {
246
245
  window[getCallBackName] = res => {
247
246
  try {
248
247
  const getResJson =
249
248
  typeof res === 'string' ? JSON.parse(res) : res
250
249
  const { status, data, msg } = getResJson
251
250
  console.log(`获取松果app展示模式成功,返回结果${data}`)
252
251
  if (status === '0') {
253
252
  this.info.sysInfo.jdAppModeType = data
254
253
  resolve(data)
255
254
  } else {
256
255
  resolve('0')
257
256
  }
258
257
  } catch (e) {
259
258
  resolve('0')
260
259
  }
261
260
  window[getCallBackName] = null
262
261
  }
263
262
  }
264
263
  window?.webkit?.messageHandlers?.JDAppUnite?.postMessage({
265
264
  method: 'callSyncRouterModuleWithParams',
266
265
  params: JSON.stringify({
267
266
  routerURL: 'router://JDBModeModule/getCurrentMode',
268
267
  routerParam: {},
269
268
  callBackName: `window.${getCallBackName}`,
270
269
  callBackId: `${getCallBackName}Ios`,
271
270
  }),
272
271
  })
273
272
  }),
274
273
  ])
275
274
  }
276
275
  }
277
276
  }
278
277
 
279
278
  getJDAppearanceStatePromise() {
280
279
  if (this.info.sysInfo.hasOwnProperty('jdAppearanceState')) {
281
280
  return Promise.resolve(this.info.sysInfo.jdAppearanceState)
282
281
  } else {
283
282
  return Promise.race([
284
283
  this.taskTimeoutPromise(() => {
285
284
  this.info.sysInfo.jdAppearanceState = '0'
286
285
  return this.info.sysInfo.jdAppearanceState
287
286
  }),
288
287
  new Promise(resolve => {
289
288
  const getCallBackName = `getJdCurrentAppearanceState${Date.now()}`
290
289
  if (!window[getCallBackName]) {
291
290
  window[getCallBackName] = res => {
292
291
  try {
293
292
  console.log('getJDAppearanceStatePromise', res)
294
293
  const getResJson =
295
294
  typeof res === 'string' ? JSON.parse(res) : res
296
295
  const { status, data, msg } = getResJson
297
296
  console.log(`获取松果app是否开启黑暗模式成功,返回结果${data}`)
298
297
  if (status === '0') {
299
298
  this.info.sysInfo.jdAppearanceState = data
300
299
  resolve(data)
301
300
  } else {
302
301
  resolve('0')
303
302
  }
304
303
  } catch (e) {
305
304
  resolve('0')
306
305
  }
307
306
  window[getCallBackName] = null
308
307
  }
309
308
  }
310
309
  if (isAndroidDevice) {
311
310
  const jsonString = JSON.stringify({
312
311
  callBackName: `window.${getCallBackName}`,
313
312
  })
314
313
  console.log('window.JDAppearance', window.JDAppearance)
315
314
  window.JDAppearance && window.JDAppearance.getUiState(jsonString)
316
315
  } else {
317
316
  window?.webkit?.messageHandlers?.JDAppUnite?.postMessage({
318
317
  method: 'callSyncRouterModuleWithParams',
319
318
  params: JSON.stringify({
320
319
  routerURL:
321
320
  'router://JDWebViewBusinessModule/getJDAppearanceState',
322
321
  routerParam: {},
323
322
  callBackName: `window.${getCallBackName}`,
324
323
  callBackId: `${getCallBackName}Ios`,
325
324
  }),
326
325
  })
327
326
  }
328
327
  }),
329
328
  ])
330
329
  }
331
330
  }
332
331
 
333
332
  createJdAndroidRquestEventForTouchStart() {
334
333
  if (isJdAndAndroidDevice && window.JdAndroid) {
335
334
  const rootEleNode = document.querySelector('body')
336
335
  if (rootEleNode) {
337
336
  rootEleNode.addEventListener(
338
337
  'touchstart',
339
338
  this.jdAndroidAddEventListenerTouchStart,
340
339
  false,
341
340
  )
342
341
  }
343
342
  }
344
343
  }
345
344
  jdAndroidAddEventListenerTouchStart(e) {
346
345
  const isH5SwiperCustomEle = e?.target?.closest('.J_h5SwiperCustom')
347
346
  if (!isH5SwiperCustomEle) {
348
347
  const hasCustomEle = e
349
348
  ? e?.target?.closest('.J_customScroll') ||
350
349
  e?.target?.closest('.J_customLayout')
351
350
  : false
352
351
  if (!hasCustomEle) {
353
352
  window.JdAndroid.requestEvent(false)
354
353
  console.log(
355
354
  'createJdAndroidRquestEvent 所有松果安卓APP内的document touch start事件执行检测requestEvent并重置为false',
356
355
  )
357
356
  }
358
357
  }
359
358
  }
360
359
  removeJdAndroidRquestEventForTouchStart() {
361
360
  if (isJdAndAndroidDevice && window.JdAndroid) {
362
361
  const rootEleNode = document.querySelector('body')
363
362
  if (rootEleNode) {
364
363
  rootEleNode.removeEventListener(
365
364
  'touchstart',
366
365
  this.jdAndroidAddEventListenerTouchStart,
367
366
  false,
368
367
  )
369
368
  }
370
369
  }
371
370
  }
372
371
 
373
372
  getNetWorkType() {
374
373
  if (isJdApp) {
375
374
  this.jmfeReayPromise().then(() => {
376
375
  window.jmfe
377
376
  .getNetworkStatus()
378
377
  .then(({ status, data }) => {
379
378
  console.log(
380
379
  '在app内初始化通过jmfe对象获取网络状态完成,当前网络状态====',
381
380
  data,
382
381
  )
383
382
  if (status === '0') {
384
383
  this.info.sysInfo['netWorkType'] = data
385
384
  } else {
386
385
  this._taroGetNetworkType()
387
386
  }
388
387
  })
389
388
  .catch(err => {
390
389
  console.log('在app内初始化通过jmfe对象获取网络状态异常====', err)
391
390
  this._taroGetNetworkType()
392
391
  })
393
392
  })
394
393
  } else {
395
394
  this._taroGetNetworkType()
396
395
  }
397
396
  }
398
397
  _taroGetNetworkType() {
399
398
  Taro.getNetworkType().then(getRes => {
400
399
  if (getRes && getRes.networkType) {
401
400
  this.info.sysInfo['netWorkType'] = getRes.networkType
402
401
  console.log(
403
402
  '在app内通过taro对象获取网络状态完成,当前网络状态',
404
403
  this.info.sysInfo['netWorkType'],
405
404
  )
406
405
  }
407
406
  })
408
407
  }
409
408
  getCacheAddressRouter() {
410
409
  if (!window.jmfe) return
411
410
  if (isAndroidDevice) {
412
411
  return window.jmfe.callRouter({
413
412
  jdRouter: '1',
414
413
  routerURL: 'router://JDAddressModule/getCacheAddress',
415
414
  routerParam: { sceneId: 'basicShoppingProcess' },
416
415
  })
417
416
  } else if (isIosDevice) {
418
417
  return window.jmfe.callRouter({
419
418
  jdRouter: '1',
420
419
  routerURL: 'router://JDBAddressCacheManagerModule/getCacheAddress',
421
420
  routerParam: { sceneId: 'basicShoppingProcess' },
422
421
  })
423
422
  } else if (isJdAndHarmonyDevice) {
424
423
  return new Promise(resolve => {
425
424
  resolve({})
426
425
  })
427
426
  } else {
428
427
  throw Error(
429
428
  'Unknown device type cannot obtain the address information through the route!',
430
429
  )
431
430
  }
432
431
  }
433
432
 
434
433
  getAddressCachePromise() {
435
434
  return new Promise(resolve => {
436
435
  if (
437
436
  this?.info?.sysInfo?.lat &&
438
437
  this?.info?.sysInfo?.lng &&
439
438
  this?.info?.sysInfo?.area
440
439
  ) {
441
440
  resolve({
442
441
  lat: this.info.sysInfo.lat,
443
442
  lng: this.info.sysInfo.lng,
444
443
  area: this?.info?.sysInfo?.area,
445
444
  })
446
445
  } else {
447
446
  this.jmfeReayPromise()
448
447
  .then(() => {
449
448
  this.getCacheAddressRouter()
450
449
  .then(({ status, data }) => {
451
450
  if (status === '0' && data) {
452
451
  const {
453
452
  lat,
454
453
  latitude,
455
454
  lng,
456
455
  longitude,
457
456
  provinceId,
458
457
  cityId,
459
458
  countyId,
460
459
  townId,
461
460
  } = data || {}
462
461
  let area = ''
463
462
  this.info.sysInfo['lat'] = lat || latitude || ''
464
463
  this.info.sysInfo['lng'] = lng || longitude || ''
465
464
  if (provinceId) {
466
465
  area = `${provinceId}_${cityId || 0}_${countyId ||
467
466
  0}_${townId || 0}`
468
467
  this.info.pageInfo['address'] = area
469
468
  this.info.pageInfo['addressCommaStr'] = area.replace(
470
469
  /_/g,
471
470
  ',',
472
471
  )
473
472
  }
474
473
  resolve({
475
474
  lat: lat,
476
475
  lng: lng,
477
476
  area: area,
478
477
  })
479
478
  } else {
480
479
  resolve({ lat: '', lng: '', area: '' })
481
480
  }
482
481
  })
483
482
  .catch(e => {
484
483
  console.log(' ~~ file: index.h5.ts:514 ~~ .catch ~~ e:', e)
485
484
  resolve({ lat: '', lng: '', area: '' })
486
485
  })
487
486
  })
488
487
  .catch(e => {
489
488
  console.log(' ~~ file: index.h5.ts:518 ~~ .catch ~~ e:', e)
490
489
  resolve({ lat: '', lng: '', area: '' })
491
490
  console.log('判断jmfe不存在,获取经纬度信息异常')
492
491
  })
493
492
  }
494
493
  })
495
494
  }
496
495
 
497
496
  async updateMPaasConfigAsync(isBeforePageReady: boolean) {
498
497
  console.log("updateMPaasConfigAsync isBeforePageReady:", isBeforePageReady)
499
498
  if (!isJdApp) {
500
499
  return
501
500
  }
502
501
  const avifSwitch = await getMPaasConfigByBussinessKey(
503
502
  'avifSwitch',
504
503
  isBeforePageReady,
505
504
  )
506
505
  this.info.sysInfo.dynamicConfig['avifSwitch'] = avifSwitch
507
506
  const isFollowAppVideoPlayStatus = await getMPaasConfigByBussinessKey(
508
507
  'isFollowAppVideoPlayStatus',
509
508
  isBeforePageReady,
510
509
  )
511
510
  console.log(
512
511
  'isBeforePageReady:',
513
512
  isBeforePageReady,
514
513
  'isFollowAppVideoPlayStatus:',
515
514
  isFollowAppVideoPlayStatus,
516
515
  )
517
516
  if (
518
517
  isFollowAppVideoPlayStatus === true ||
519
518
  isFollowAppVideoPlayStatus === 'true'
520
519
  ) {
521
520
  this.info.sysInfo.dynamicConfig['isFollowAppVideoPlayStatus'] = true
522
521
  }
523
522
  }
524
523
 
525
524
  async getWifiVideoAutoPlayAsync() {
526
525
  this.info.sysInfo['wifiVideoAutoPlay'] = false
527
526
  if (!isJdApp) {
528
527
  return
529
528
  }
530
529
  const videoPlayStatus = await getWifiVideoAutoPlay().catch(e => {
531
530
  return 0
532
531
  })
533
532
  if (Number(videoPlayStatus) === 1) {
534
533
  this.info.sysInfo['wifiVideoAutoPlay'] = true
535
534
  }
536
535
  }
537
536
 
538
537
  async getMPaasConfigAsync() {
539
538
  this.info.sysInfo.dynamicConfig = {}
540
539
  this.info.sysInfo.dynamicConfig['avifSwitch'] = {}
541
540
  this.info.sysInfo.dynamicConfig['isFollowAppVideoPlayStatus'] = false
542
541
  return this.updateMPaasConfigAsync(true)
543
542
  }
544
543
 
545
544
  getDynamicConfig(key: string){
546
545
  return this.info.sysInfo.dynamicConfig[key]
547
546
  }
548
547
  async updateMPaasConfig() {
549
548
  console.log('updateMPaasConfig')
550
549
  if (
551
550
  isIosDevice &&
552
551
  versionCompare(jdAppVersionStr, MPAAS_CONFIG_APP_VERSION) < 0 &&
553
552
  versionCompare(jdAppVersionStr, MPAAS_CONFIG_APP_LOW_VERSION) >= 0
554
553
  ) {
555
554
  try {
556
555
  await this.updateMPaasConfigAsync(false)
557
556
  } catch (e) {
558
557
  console.log('updateMPaasConfigAsync:', e)
559
558
  }
560
559
  }
561
560
  }
562
561
 
563
562
  toLogin(options) {
564
563
  return this.info.isJingGouMiniViewState || this.info.isJingxiMiniViewState
565
564
  ? this.toWxAppLogin(options)
566
565
  : this.toWebLogin(options)
567
566
  }
568
567
 
569
568
  doLogin(options) {
570
569
  return this.toLogin(options)
571
570
  }
572
571
 
573
572
  doLoginForJdPin(options = {}) {
574
573
  return this.doLogin({
575
574
  loginColor: {
576
575
  biz: WXAPP_BIZ_SHOP_LIGHT_KEY,
577
576
  dpin: 0,
578
577
  },
579
578
  ...options,
580
579
  })
581
580
  }
582
581
 
583
582
  toWebLogin(options) {
584
583
  let params: {
585
584
  returnurl: string
586
585
  } = {
587
586
  returnurl: '',
588
587
  }
589
588
  const loginUrl = isPc
590
589
  ? `//passport.jd.com/new/login.aspx`
591
590
  : `${domain.mobileLogin}/user/login.action`
592
591
  const defaultParams = {
593
592
  appid: '100',
594
593
  returnurl: window.location.href,
595
594
  }
596
595
  if (isString(options)) {
597
596
  params = Object.assign({}, defaultParams, {
598
597
  returnurl: options,
599
598
  })
600
599
  } else if (isObject(options)) {
601
600
  const { loginColor, ...otherOptions } = options
602
601
  params = Object.assign({}, defaultParams, otherOptions)
603
602
  } else {
604
603
  params = defaultParams
605
604
  }
606
605
  params.returnurl = encodeURIComponent(params.returnurl)
607
606
  let getFullUrl = loginUrl + '?' + serialize(params)
608
607
  if (isPc) {
609
608
  getFullUrl = getFullUrl.replace(/returnurl/, 'ReturnUrl')
610
609
  }
611
610
  return Promise.resolve({
612
611
  h5ToUrl: true,
613
612
  url: getFullUrl,
614
613
  }).then(() => {
615
614
  window.location.href = getFullUrl
616
615
  })
617
616
  }
618
617
 
619
618
  toWxAppLogin(options = {}) {
620
619
  console.log('微信京购小程序中h5登录跳转', options)
621
620
  return Promise.resolve(true).then(() => {
622
621
  const { loginColor } = Object.assign(
623
622
  {},
624
623
  {
625
624
  loginColor: {
626
625
  biz: WXAPP_BIZ_KEY,
627
626
  dpin: 1,
628
627
  },
629
628
  },
630
629
  options,
631
630
  )
632
631
  window.location.href = `${domain.wq}/pinbind/pintokenredirect?biz=${
633
632
  loginColor.biz
634
633
  }&url=${encodeURIComponent(window.location.href)}`
635
634
  })
636
635
  }
637
636
 
638
637
  getLoginCookie() {
639
638
  return Promise.resolve({
640
639
  pin: cookie.get('pin') || '',
641
640
  })
642
641
  }
643
642
 
644
643
  clearLoginCookie() {
645
644
  cookie.remove('pin')
646
645
  }
647
646
 
648
647
  checkStatusAndLogin(options = {}) {
649
648
  if (!this.checkStatusAndLoginPromise) {
650
649
  this.checkStatusAndLoginPromise = new Promise(async (resolve, reject) => {
651
650
  try {
652
651
  const getLoginState = await this.doCheckLoginStateAndForApiCheck(
653
652
  options,
654
653
  )
655
654
  if (getLoginState) {
656
655
  resolve(true)
657
656
  } else {
658
657
  this.toLogin(options)
659
658
  reject(false)
660
659
  }
661
660
  } catch (e) {
662
661
  this.toLogin(options)
663
662
  reject(false)
664
663
  }
665
664
  })
666
665
  return this.checkStatusAndLoginPromise
667
666
  } else {
668
667
  return this.checkStatusAndLoginPromise
669
668
  .then(() => {
670
669
  return Promise.resolve(true)
671
670
  })
672
671
  .catch(() => {
673
672
  this.toLogin(options)
674
673
  return Promise.reject(true)
675
674
  })
676
675
  }
677
676
  }
678
677
 
679
678
  checkJdStatusAndLogin(
680
679
  options = {
681
680
  loginColor: {
682
681
  biz: WXAPP_BIZ_SHOP_LIGHT_KEY,
683
682
  dpin: 0,
684
683
  },
685
684
  },
686
685
  ) {
687
686
  return this.checkStatusAndLogin(options)
688
687
  }
689
688
 
690
689
  doCheckLoginStateAndForApiCheck(options) {
691
690
  if (this.info.loginState) {
692
691
  return Promise.resolve(true)
693
692
  } else {
694
693
  return new Promise((resolve, reject) => {
695
694
  if (
696
695
  this.info.isJingGouMiniViewState ||
697
696
  this.info.isJingxiMiniViewState
698
697
  ) {
699
698
  const getWqAuthToken = cookie.get('wq_auth_token')
700
699
  const getWqSkey = cookie.get('wq_skey')
701
700
  const getWqUin = cookie.get('wq_uin')
702
701
  const isLoginState =
703
702
  options?.loginColor?.dpin === 0
704
703
  ? getWqAuthToken
705
704
  : getWqSkey && getWqUin
706
705
  if (isLoginState) {
707
706
  this.info.loginState = true
708
707
  resolve(true)
709
708
  } else {
710
709
  reject(false)
711
710
  }
712
711
  } else {
713
712
  Taro.request({
714
713
  url: api.isLogin,
715
714
  jsonp: true,
716
715
  timeout: 3000,
717
716
  success: res => {
718
717
  const { statusCode, data } = res
719
718
  if (
720
719
  statusCode === 200 &&
721
720
  data?.islogin &&
722
721
  Number(data.islogin) === 1
723
722
  ) {
724
723
  this.info.loginState = true
725
724
  resolve(true)
726
725
  } else {
727
726
  reject(false)
728
727
  }
729
728
  },
730
729
  fail: err => {
731
730
  console.log('登录检查异常', err)
732
731
  reject(false)
733
732
  },
734
733
  })
735
734
  }
736
735
  })
737
736
  }
738
737
  }
739
738
 
740
739
  checkLoginStatus(options) {
741
740
  return new Promise(async (resolve, reject) => {
742
741
  try {
743
742
  const getLoginState = await this.doCheckLoginStateAndForApiCheck(
744
743
  options,
745
744
  )
746
745
  if (getLoginState) {
747
746
  const { pin } = await this.getLoginCookie()
748
747
  this.info.userInfo = {
749
748
  pin,
750
749
  encodePin: encodeURIComponent(pin),
751
750
  ptkey: '',
752
751
  }
753
752
  resolve(true)
754
753
  } else {
755
754
  reject(false)
756
755
  }
757
756
  } catch (e) {
758
757
  reject(false)
759
758
  }
760
759
  })
761
760
  }
762
761
 
763
762
  updatePageAndLogInfo(updateQuery = {}) {
764
763
  const createUpdateQueryInfo: {
765
764
  query: {
766
765
  shopId?: string | number
767
766
  venderId?: string | number
768
767
  }
769
768
  updateShopInfoState: boolean
770
769
  } = Object.assign(
771
770
  {},
772
771
  {
773
772
  query: {},
774
773
  updateShopInfoState: false,
775
774
  },
776
775
  updateQuery,
777
776
  )
778
777
  console.log(
779
778
  '获取当前下发的店铺查询参数',
780
779
  updateQuery,
781
780
  '获取之前保存的shopInfo店铺查询参数',
782
781
  this.info?.shopInfo,
783
782
  )
784
783
  const { query, updateShopInfoState } = createUpdateQueryInfo
785
784
  const { shopId, venderId, un_area } = query
786
785
  if (updateShopInfoState) {
787
786
  this.info.queryInfo = {
788
787
  ...this.info.queryInfo,
789
788
  ...query,
790
789
  }
791
790
  if (shopId && venderId) {
792
791
  this.info.shopInfo = {
793
792
  shopId: `${shopId}`,
794
793
  venderId: `${venderId}`,
795
794
  }
796
795
  }
797
796
  } else {
798
797
  this.info.queryInfo = {
799
798
  ...query,
800
799
  }
801
800
  if (
802
801
  this.info.shopInfo?.shopId &&
803
802
  this.info.shopInfo?.venderId &&
804
803
  (this.info.shopInfo.shopId == shopId ||
805
804
  this.info.shopInfo.venderId == venderId)
806
805
  ) {
807
806
  this.info.queryInfo.shopId = this.info.shopInfo.shopId
808
807
  this.info.queryInfo.venderId = this.info.shopInfo.venderId
809
808
  console.log(
810
809
  '当前存储的店铺shopId和venderId与下发的店铺信息shopId或者venderId为同一个,补充shopId或者venderId查询参数',
811
810
  this.info.queryInfo,
812
811
  )
813
812
  }
814
813
  }
815
814
  this.info.queryInfo['shopId'] &&
816
815
  (this.info.queryInfo['shopId'] = `${this.info.queryInfo['shopId']}`)
817
816
  this.info.queryInfo['venderId'] &&
818
817
  (this.info.queryInfo['venderId'] = `${this.info.queryInfo['venderId']}`)
819
818
  console.log(
820
819
  'h5==获取店铺下发查询参数\n',
821
820
  query,
822
821
  '\n获取店铺最后查询参数\n',
823
822
  this.info.queryInfo,
824
823
  '\n是否为更新店铺状态\n',
825
824
  updateShopInfoState,
826
825
  )
827
826
  const changeArea =
828
827
  un_area && un_area.length > 0 ? un_area : ipLoc_djd ? ipLoc_djd : ''
829
828
  if (changeArea) {
830
829
  this.info.pageInfo.address = changeArea
831
830
  this.info.pageInfo.un_area = changeArea
832
831
  this.info.pageInfo.addressCommaStr = changeArea.replace(/_/g, ',')
833
832
  }
834
833
  }
835
834
 
836
835
  dealLoadSdkList(){
837
836
  const globalLoadJsList = window?.shopGlobalSwitch?.asyncLoadJsList ?? [];
838
837
  const businessLoadJsList = window?.PAGE_DATA?.businessData?.asyncLoadJsList ?? []
839
838
  const concatLoadJsList = [].concat(globalLoadJsList,businessLoadJsList)
840
839
  let mergeLoadJsList = globalLoadJsList
841
840
  try {
842
841
  mergeLoadJsList = concatLoadJsList.reduce((accArr: any[], current: any) => {
843
842
  const getFindIndex = accArr.findIndex(item => item?.fileName === current?.fileName);
844
843
  getFindIndex !== -1 ? (accArr[getFindIndex] = {...accArr[getFindIndex],...current }) : accArr.push(current);
845
844
  return accArr;
846
845
  }, []);
847
846
  }catch (e) {
848
847
  console.log("LoadJsList合并错误", e)
849
848
  }
850
849
  console.log("globalLoadJsList",globalLoadJsList,"businessLoadJsList",businessLoadJsList,"两个加载jsList集合合并完成", mergeLoadJsList)
851
850
  this.loadJsSdkList = mergeLoadJsList
852
851
  return this.loadJsSdkList
853
852
  }
854
853
 
855
854
  renderNextTickLoadSdk(){
856
855
  Taro.nextTick(() => {
857
856
  console.log("页面渲染的下一帧执行的js加载方法,当前nextTick存在state的渲染问题,先延迟1s=======")
858
857
  setTimeout(() => {
859
858
  this.loadOtherSdk(LoadJsInitTriggerType.NRXT_TICK,this.loadJsSdkList)
860
859
  },1000)
861
860
  })
862
861
  }
863
862
 
864
863
  loadOtherSdk(triggerType = LoadJsInitTriggerType.NOW,loadJsList:any[] = [] ) {
865
864
  const getLoadJsList = Array.isArray(loadJsList) && loadJsList.length > 0 ? loadJsList : this.dealLoadSdkList()
866
865
  const getLoadFilterList = getLoadJsList.filter(item => {
867
866
  const getInitLoadEnvType = item?.initLoadEnvType || LoadJsInitLoadEnvType.ALL
868
867
  let getLoastLoadEventState = true
869
868
  if(getInitLoadEnvType === LoadJsInitLoadEnvType.JD_APP){
870
869
  getLoastLoadEventState = isJdApp
871
870
  }
872
871
  else if(getInitLoadEnvType === LoadJsInitLoadEnvType.M){
873
872
  getLoastLoadEventState = !isJdApp
874
873
  }
875
874
  const getInitTriggerType = isJdApp && item?.initJdAppTriggerType ? item?.initTriggerType : item?.initTriggerType || LoadJsInitTriggerType.NOW
876
875
  const getInitLinkTriggerWay = window?.PAGE_DATA[item?.initLinkTriggerWay] || false
877
876
  return getLoastLoadEventState && getInitTriggerType === triggerType && getInitLinkTriggerWay
878
877
  })
879
878
  console.log("获取当前触发方式",triggerType,"获取当前最后加载的js集合",getLoadFilterList,"过滤前的加载集合",getLoadJsList)
880
879
  getLoadFilterList.length > 0 && getLoadFilterList.forEach((item) => {
881
880
  const isLoadState = /sgm/.test(item?.fileName) ? window?.shopGlobalSwitch?.openSgm === "true" : true
882
881
  isLoadState && this.loadItemSdkPromise(item)
883
882
  .then((res) => {
884
883
  console.info('当前js地址' + item?.src, '加载状态', res)
885
884
  })
886
885
  .catch((err) => {
887
886
  console.info('当前js地址加载异常', item?.src)
888
887
  window?.fetchErrorData &&
889
888
  window.fetchErrorData({
890
889
  title: '公共js加载异常',
891
890
  type: "jsLoad",
892
891
  data: err,
893
892
  })
894
893
  })
895
894
  })
896
895
  }
897
896
  loadScriptEle(jsInfo,resolve,reject){
898
897
  const getFileName = jsInfo?.fileName;
899
898
  if(getFileName){
900
899
  const getEleId = `J_loadJs_${getFileName}`
901
900
  const getEle = document.getElementById(getEleId)
902
901
  if (!getEle) {
903
902
  const jsLoadErrorSgmCode = `jsLoadError_${
904
903
  jsInfo?.fileName || 'customJs'
905
904
  }`
906
905
  const _sgmEle = document.createElement('script')
907
906
  _sgmEle.id = getEleId
908
907
  _sgmEle.onload = function () {
909
908
  resolve({
910
909
  ...jsInfo,
911
910
  jsTip: "js加载成功",
912
911
  })
913
912
  }
914
913
  _sgmEle.onerror = function () {
915
914
  reject({
916
915
  ...jsInfo,
917
916
  env: getSgmCustomCode(jsLoadErrorSgmCode),
918
917
  jsReqError: '当前js创建标签触发onerror异常回调,请排查网络络错误或语法错误或运行时错误',
919
918
  })
920
919
  }
921
920
  const dataAttrList = ['timeout','fileName',"env"]
922
921
  const getJsInfoKeyList = Object.keys(jsInfo)
923
922
  getJsInfoKeyList.forEach(key => {
924
923
  if(key === "async"){
925
924
  _sgmEle.async = jsInfo[key]
926
925
  }else if(key === "crossOrigin"){
927
926
  _sgmEle.crossOrigin = jsInfo[key]
928
927
  }else if(key === "src"){
929
928
  _sgmEle.src = `${jsInfo[key]}`
930
929
  }else if(dataAttrList.includes(key) || /init/.test(key)) {
931
930
  _sgmEle.setAttribute(`data-${key}`, jsInfo[key])
932
931
  }else {
933
932
  _sgmEle.setAttribute(key, jsInfo[key])
934
933
  }
935
934
  })
936
935
  document.head.appendChild(_sgmEle)
937
936
  }else {
938
937
  console.log(`当前${jsInfo?.fileName || "js"}已经存在页面中,可以直接调用相关方法`,jsInfo)
939
938
  resolve({
940
939
  ...jsInfo,
941
940
  jsTip: "js本身已存在页面中",
942
941
  })
943
942
  }
944
943
  }else {
945
944
  console.warn("当前js资源信息缺少必要的参数fileName,请关注",jsInfo)
946
945
  }
947
946
  }
948
947
 
949
948
  loadItemSdkPromise(jsInfo = {}){
950
949
  if(jsInfo?.src){
951
950
  const getInitLoadType = isJdApp && jsInfo?.initJdAppLoadType ? jsInfo?.initJdAppLoadType : jsInfo?.initLoadType || LoadJsInitLoadType.ALL
952
951
  if(getInitLoadType !== LoadJsInitLoadType.NONE){
953
952
  if(getInitLoadType !== LoadJsInitLoadType.INSERT_ELE){
954
953
  return new Promise((resolve,reject) => {
955
954
  const jsLoadErrorSgmCode = `jsLoadError_${
956
955
  jsInfo?.fileName || 'customJs'
957
956
  }`
958
957
  try {
959
958
  const jsXhrRequest = new XMLHttpRequest();
960
959
  jsXhrRequest.timeout = jsInfo?.timeout ?? 2000
961
960
  const jsUrl = `${jsInfo?.src}`
962
961
  jsXhrRequest.open('GET', jsUrl, true);
963
962
  jsXhrRequest.onload = () => {
964
963
  if (jsXhrRequest.status === 200) {
965
964
  const getInsetHeadState = getInitLoadType === LoadJsInitLoadType.ALL
966
965
  if(getInsetHeadState){
967
966
  this.loadScriptEle(jsInfo,resolve,reject)
968
967
  }else {
969
968
  resolve({
970
969
  ...jsInfo,
971
970
  jsTip: "js请求成功,暂未插入head节点,业务自行单独插入",
972
971
  })
973
972
  }
974
973
  }else {
975
974
  const getRes = {
976
975
  ...jsInfo,
977
976
  env: getSgmCustomCode(jsLoadErrorSgmCode),
978
977
  jsReqError: `请求状态异常,状态为${jsXhrRequest?.status}`,
979
978
  jsReqState: jsXhrRequest?.status,
980
979
  }
981
980
  console.log("当前js请求状态异常", getRes)
982
981
  reject(getRes)
983
982
  }
984
983
  }
985
984
  jsXhrRequest.onerror = () => {
986
985
  const getRes = {
987
986
  ...jsInfo,
988
987
  env: getSgmCustomCode(jsLoadErrorSgmCode),
989
988
  jsReqError: '请求错误',
990
989
  }
991
990
  console.log("当前js请求错误", getRes)
992
991
  jsXhrRequest.abort()
993
992
  reject(getRes)
994
993
  }
995
994
  jsXhrRequest.ontimeout = () => {
996
995
  const getRes = {
997
996
  ...jsInfo,
998
997
  env: getSgmCustomCode(jsLoadErrorSgmCode),
999
998
  jsReqError: `请求${jsXhrRequest.timeout}ms超时异常`,
1000
999
  }
1001
1000
  console.log("当前js请求超时异常", getRes)
1002
1001
  jsXhrRequest.abort()
1003
1002
  reject(getRes)
1004
1003
  }
1005
1004
  jsXhrRequest.send();
1006
1005
  }catch (e) {
1007
1006
  console.log("执行js请求异常",e)
1008
1007
  reject({
1009
1008
  ...jsInfo,
1010
1009
  env: getSgmCustomCode(jsLoadErrorSgmCode),
1011
1010
  jsReqError: '未知异常',
1012
1011
  error: e,
1013
1012
  })
1014
1013
  }
1015
1014
  })
1016
1015
  }else {
1017
1016
  return new Promise((resolve,reject) => {
1018
1017
  return this.loadScriptEle(jsInfo,resolve,reject)
1019
1018
  })
1020
1019
  }
1021
1020
  }else {
1022
1021
  return Promise.resolve({
1023
1022
  ...jsInfo,
1024
1023
  jsTip: "js加载方式设置为不加载,当前不做处理",
1025
1024
  })
1026
1025
  }
1027
1026
  }else {
1028
1027
  return Promise.reject(jsInfo)
1029
1028
  }
1030
1029
  }
1030
+ import Taro from '@tarojs/taro'
1031
1031
  isIosDevice,
1032
1032
  isAndroidDevice,
1033
1033
  isJdAndHarmonyDevice,
1034
1034
  isJdAndAndroidDevice,
1035
1035
  jdAppVersion,
1036
1036
  jdAppVersionStr,
1037
1037
  isString,
1038
1038
  isObject,
1039
1039
  serialize,
1040
1040
  dealNativePixelToCssPixel,
1041
1041
  isPc,
1042
1042
  BUSINESS_TYPE,
1043
1043
  JSSDK_APP_WEBVIEW_CODE,
1044
1044
  SECTION_HOME_TAB_NAME_TYPE,
1045
1045
  SECTION_HOME_TAB_TYPE,
1046
1046
  WXAPP_BIZ_KEY,
1047
1047
  WX_BUSINESS_TYPE,
1048
1048
  MPAAS_CONFIG_APP_VERSION,
1049
1049
  MPAAS_CONFIG_APP_LOW_VERSION,
1050
1050
  WXAPP_BIZ_SHOP_LIGHT_KEY,
1051
1051
  LoadJsInitLoadEnvType,
1052
1052
  LoadJsInitLoadType,
1053
1053
  LoadJsInitTriggerType
1054
1054
  loginState: false,
1055
1055
  cookiesStr: '',
1056
1056
  userInfo: userPinKey,
1057
1057
  isJingGouMiniViewState: false,
1058
1058
  isJingxiMiniViewState: false,
1059
1059
  pageInfo: {
1060
1060
  wxBusinessType: WX_BUSINESS_TYPE.NO,
1061
1061
  address: '',
1062
1062
  addressCommaStr: '',
1063
1063
  un_area: '',
1064
1064
  vapptype: '1',
1065
1065
  pageType: 'home',
1066
1066
  isExposureState: false,
1067
1067
  moduleId: '',
1068
1068
  entrance: '',
1069
1069
  dataType: BUSINESS_TYPE.ONLINE,
1070
1070
  floorExposureInfo: {},
1071
1071
  floorVideInfo: {},
1072
1072
  productVideInfo: {},
1073
1073
  tabsLoadAllDataInfo: {
1074
1074
  [SECTION_HOME_TAB_NAME_TYPE[
1075
1075
  SECTION_HOME_TAB_TYPE.HOME_WELL_CHOSEN
1076
1076
  ]]: false,
1077
1077
  },
1078
1078
  updateShopInfosAllState: false,
1079
1079
  isVipShop: false,
1080
1080
  isJdShowNativeImmersivePlayer: false,
1081
1081
  ...shopConfig,
1082
1082
  pageScrollTop: 0,
1083
1083
  pageIdxHeightInfo: {
1084
1084
  list: [],
1085
1085
  },
1086
1086
  shopNavBarHeight: 0,
1087
1087
  },
1088
1088
  defaultQueryLogInfo: {
1089
1089
  sourceType: 'JDshop',
1090
1090
  sourceValue: '',
1091
1091
  moduleId: 'none',
1092
1092
  entrance: 'none',
1093
1093
  },
1094
1094
  sysInfo: {
1095
1095
  windowWidth: isPc ? 375 : 0,
1096
1096
  containerWidth: isPc ? 375 : 0,
1097
1097
  windowHeight: 0,
1098
1098
  netWorkType: '4g',
1099
1099
  jdBottomBarHeight: 0,
1100
1100
  jdNativeHeaderHeight: 0,
1101
1101
  },
1102
1102
  queryInfo: {},
1103
1103
  shopInfo: {},
1104
1104
  openAppData: {},
1105
1105
  public info: CommonInterFace.BaseConfigInfo
1106
1106
  public config: {
1107
1107
  [key: string]: any
1108
1108
  }
1109
1109
  public lazyContainer: CommonInterFace.lazyContainer
1110
1110
  public renderedIsvComponents: CommonInterFace.renderedIsvComponents
1111
1111
  public rootEleNode: HTMLElement | null
1112
1112
  public checkStatusAndLoginPromise: object | null
1113
1113
  private jmfeRegisterStatePromise: Promise<any> | null
1114
1114
  private jmfeRegisterState: boolean
1115
1115
  public loadJsSdkList: Array<any>
1116
1116
  public loadJsSdkListCachePromise: any
1117
1117
  constructor(opt) {
1118
1118
  this.info = this._getConfig(opt)
1119
1119
  this.config = {}
1120
1120
  this.loadJsSdkList = []
1121
1121
  this.loadJsSdkListCachePromise = {}
1122
1122
  this.lazyContainer = {
1123
1123
  [SECTION_HOME_TAB_NAME_TYPE[SECTION_HOME_TAB_TYPE.HOME_WELL_CHOSEN]]: {
1124
1124
  appLazyContainerList: [],
1125
1125
  appLazyFinishContainerList: [],
1126
1126
  },
1127
1127
  [SECTION_HOME_TAB_NAME_TYPE[SECTION_HOME_TAB_TYPE.HOME_PROMOTION]]: {
1128
1128
  appLazyContainerList: [],
1129
1129
  appLazyFinishContainerList: [],
1130
1130
  },
1131
1131
  }
1132
1132
  this.renderedIsvComponents = {}
1133
1133
  this.rootEleNode = document.querySelector('body')
1134
1134
  this.checkStatusAndLoginPromise = null
1135
1135
  this.jmfeRegisterStatePromise = null
1136
1136
  this.loadOtherSdk()
1137
1137
  isJdApp && this.jmfeReayPromise()
1138
1138
  }
1139
1139
  _getConfig(opt) {
1140
1140
  return Object.assign({}, DefaultConfig, opt)
1141
1141
  }
1142
1142
 
1143
1143
  jmfeReayPromise(): Promise<any> {
1144
1144
  if (isJdApp) {
1145
1145
  if (this.jmfeRegisterState) {
1146
1146
  return Promise.resolve(true)
1147
1147
  } else {
1148
1148
  !this.jmfeRegisterStatePromise &&
1149
1149
  (this.jmfeRegisterStatePromise = new Promise((resolve, reject) => {
1150
1150
  ready('jmfe', 3000)
1151
1151
  .then(() => {
1152
1152
  window?.jmfe && window.jmfe.registerCode(JSSDK_APP_WEBVIEW_CODE)
1153
1153
  this.jmfeRegisterState = true
1154
1154
  resolve(true)
1155
1155
  console.log('松果app内初始化注册jmfe认证完成')
1156
1156
  })
1157
1157
  .catch(() => {
1158
1158
  reject(false)
1159
1159
  })
1160
1160
  }))
1161
1161
  return this.jmfeRegisterStatePromise
1162
1162
  }
1163
1163
  } else {
1164
1164
  return Promise.reject(false)
1165
1165
  }
1166
1166
  }
1167
1167
 
1168
1168
  updateBusinessDomainAndApi(domain, api) {
1169
1169
  }
1170
1170
 
1171
1171
  formatNativeScreenPageData(action) {
1172
1172
  let getChangePageInfo: any = null
1173
1173
  try {
1174
1174
  const getNativeScreenPageInfoStr = window.XWebView?._callNative(
1175
1175
  JSON.stringify({
1176
1176
  plugin: 'JDHybridScreenPlugin',
1177
1177
  action,
1178
1178
  sync: '1',
1179
1179
  }),
1180
1180
  )
1181
1181
  const getChangePageInfoData =
1182
1182
  typeof getNativeScreenPageInfoStr === 'string'
1183
1183
  ? JSON.parse(getNativeScreenPageInfoStr)
1184
1184
  : null
1185
1185
  if (getChangePageInfoData && typeof getChangePageInfoData === 'object') {
1186
1186
  const { code, data } = getChangePageInfoData
1187
1187
  getChangePageInfo = code && code === '0' ? data : null
1188
1188
  }
1189
1189
  } catch (e) {
1190
1190
  console.log('JDHybridScreenPlugin转换异常', e)
1191
1191
  }
1192
1192
  return getChangePageInfo
1193
1193
  }
1194
1194
 
1195
1195
  isAndroidFoldScreen() {
1196
1196
  return this.formatNativeScreenPageData('isFoldScreen') === '1'
1197
1197
  }
1198
1198
 
1199
1199
  getJdAndroidPageChangeScreenInfo() {
1200
1200
  const getPageScreenInfo = this.formatNativeScreenPageData('getScreenSize')
1201
1201
  if (
1202
1202
  getPageScreenInfo &&
1203
1203
  getPageScreenInfo?.pageWidth &&
1204
1204
  getPageScreenInfo?.pageHeight
1205
1205
  ) {
1206
1206
  const { pageWidth, pageHeight } = dealNativePixelToCssPixel({
1207
1207
  pageWidth: getPageScreenInfo.pageWidth,
1208
1208
  pageHeight: getPageScreenInfo.pageHeight,
1209
1209
  })
1210
1210
  getPageScreenInfo.pageWidth = pageWidth
1211
1211
  getPageScreenInfo.pageHeight = pageHeight
1212
1212
  }
1213
1213
  return getPageScreenInfo
1214
1214
  }
1215
1215
 
1216
1216
  getSystemInfo(params) {
1217
1217
  let info: UtilsInterFace.taroGetSystemInfoSyncRes | {} = isPc
1218
1218
  ? {}
1219
1219
  : getSystemInfos(params)
1220
1220
  if (isJdAndAndroidDevice && window.innerWidth <= 0) {
1221
1221
  const isfoldScreen = this.isAndroidFoldScreen()
1222
1222
  if (isfoldScreen) {
1223
1223
  const getJdAndroidPageInfo = this.getJdAndroidPageChangeScreenInfo()
1224
1224
  getJdAndroidPageInfo && (info = getSystemInfos(getJdAndroidPageInfo))
1225
1225
  console.info(
1226
1226
  '安卓松果app折叠屏获取webview宽度为0,那么通过内部方法重置获取',
1227
1227
  getJdAndroidPageInfo,
1228
1228
  )
1229
1229
  }
1230
1230
  }
1231
1231
  this.info.sysInfo = {
1232
1232
  actualNavBarHeight: 0,
1233
1233
  ...this.info.sysInfo,
1234
1234
  ...info,
1235
1235
  safeContentHeight: info?.screenHeight,
1236
1236
  headerHeight: 0,
1237
1237
  tabBarHeight: 0,
1238
1238
  }
1239
1239
  if (isJdApp) {
1240
1240
  this.info.sysInfo['hostVersionName'] = jdAppVersionStr
1241
1241
  this.info.sysInfo['hostAppVersion'] = jdAppVersion
1242
1242
  this.getAddressCachePromise()
1243
1243
  this.getElderModePromise()
1244
1244
  this.getJDAppearanceStatePromise()
1245
1245
  this.createJdAndroidRquestEventForTouchStart()
1246
1246
  }
1247
1247
  this.getWifiVideoAutoPlayAsync()
1248
1248
  this.getMPaasConfigAsync()
1249
1249
  this.getNetWorkType()
1250
1250
  }
1251
1251
 
1252
1252
  taskTimeoutPromise(callBack, timeout = 2000) {
1253
1253
  return new Promise(resolve => {
1254
1254
  setTimeout(() => {
1255
1255
  const getCallBackRes = typeof callBack === 'function' && callBack()
1256
1256
  return resolve(getCallBackRes || false)
1257
1257
  }, timeout)
1258
1258
  })
1259
1259
  }
1260
1260
 
1261
1261
  getElderModePromise() {
1262
1262
  if (this.info.sysInfo.hasOwnProperty('jdAppModeType')) {
1263
1263
  return Promise.resolve(this.info.sysInfo.jdAppModeType)
1264
1264
  } else {
1265
1265
  if (isJdAndAndroidDevice) {
1266
1266
  this.info.sysInfo.jdAppModeType = '0'
1267
1267
  return Promise.resolve(this.info.sysInfo.jdAppModeType)
1268
1268
  } else {
1269
1269
  return Promise.race([
1270
1270
  this.taskTimeoutPromise(() => {
1271
1271
  this.info.sysInfo.jdAppModeType = '0'
1272
1272
  return this.info.sysInfo.jdAppModeType
1273
1273
  }),
1274
1274
  new Promise(resolve => {
1275
1275
  const getCallBackName = `getJdCurrentModeType${Date.now()}`
1276
1276
  if (!window[getCallBackName]) {
1277
1277
  window[getCallBackName] = res => {
1278
1278
  try {
1279
1279
  const getResJson =
1280
1280
  typeof res === 'string' ? JSON.parse(res) : res
1281
1281
  const { status, data, msg } = getResJson
1282
1282
  console.log(`获取松果app展示模式成功,返回结果${data}`)
1283
1283
  if (status === '0') {
1284
1284
  this.info.sysInfo.jdAppModeType = data
1285
1285
  resolve(data)
1286
1286
  } else {
1287
1287
  resolve('0')
1288
1288
  }
1289
1289
  } catch (e) {
1290
1290
  resolve('0')
1291
1291
  }
1292
1292
  window[getCallBackName] = null
1293
1293
  }
1294
1294
  }
1295
1295
  window?.webkit?.messageHandlers?.JDAppUnite?.postMessage({
1296
1296
  method: 'callSyncRouterModuleWithParams',
1297
1297
  params: JSON.stringify({
1298
1298
  routerURL: 'router://JDBModeModule/getCurrentMode',
1299
1299
  routerParam: {},
1300
1300
  callBackName: `window.${getCallBackName}`,
1301
1301
  callBackId: `${getCallBackName}Ios`,
1302
1302
  }),
1303
1303
  })
1304
1304
  }),
1305
1305
  ])
1306
1306
  }
1307
1307
  }
1308
1308
  }
1309
1309
 
1310
1310
  getJDAppearanceStatePromise() {
1311
1311
  if (this.info.sysInfo.hasOwnProperty('jdAppearanceState')) {
1312
1312
  return Promise.resolve(this.info.sysInfo.jdAppearanceState)
1313
1313
  } else {
1314
1314
  return Promise.race([
1315
1315
  this.taskTimeoutPromise(() => {
1316
1316
  this.info.sysInfo.jdAppearanceState = '0'
1317
1317
  return this.info.sysInfo.jdAppearanceState
1318
1318
  }),
1319
1319
  new Promise(resolve => {
1320
1320
  const getCallBackName = `getJdCurrentAppearanceState${Date.now()}`
1321
1321
  if (!window[getCallBackName]) {
1322
1322
  window[getCallBackName] = res => {
1323
1323
  try {
1324
1324
  console.log('getJDAppearanceStatePromise', res)
1325
1325
  const getResJson =
1326
1326
  typeof res === 'string' ? JSON.parse(res) : res
1327
1327
  const { status, data, msg } = getResJson
1328
1328
  console.log(`获取松果app是否开启黑暗模式成功,返回结果${data}`)
1329
1329
  if (status === '0') {
1330
1330
  this.info.sysInfo.jdAppearanceState = data
1331
1331
  resolve(data)
1332
1332
  } else {
1333
1333
  resolve('0')
1334
1334
  }
1335
1335
  } catch (e) {
1336
1336
  resolve('0')
1337
1337
  }
1338
1338
  window[getCallBackName] = null
1339
1339
  }
1340
1340
  }
1341
1341
  if (isAndroidDevice) {
1342
1342
  const jsonString = JSON.stringify({
1343
1343
  callBackName: `window.${getCallBackName}`,
1344
1344
  })
1345
1345
  console.log('window.JDAppearance', window.JDAppearance)
1346
1346
  window.JDAppearance && window.JDAppearance.getUiState(jsonString)
1347
1347
  } else {
1348
1348
  window?.webkit?.messageHandlers?.JDAppUnite?.postMessage({
1349
1349
  method: 'callSyncRouterModuleWithParams',
1350
1350
  params: JSON.stringify({
1351
1351
  routerURL:
1352
1352
  'router://JDWebViewBusinessModule/getJDAppearanceState',
1353
1353
  routerParam: {},
1354
1354
  callBackName: `window.${getCallBackName}`,
1355
1355
  callBackId: `${getCallBackName}Ios`,
1356
1356
  }),
1357
1357
  })
1358
1358
  }
1359
1359
  }),
1360
1360
  ])
1361
1361
  }
1362
1362
  }
1363
1363
 
1364
1364
  createJdAndroidRquestEventForTouchStart() {
1365
1365
  if (isJdAndAndroidDevice && window.JdAndroid) {
1366
1366
  const rootEleNode = document.querySelector('body')
1367
1367
  if (rootEleNode) {
1368
1368
  rootEleNode.addEventListener(
1369
1369
  'touchstart',
1370
1370
  this.jdAndroidAddEventListenerTouchStart,
1371
1371
  false,
1372
1372
  )
1373
1373
  }
1374
1374
  }
1375
1375
  }
1376
1376
  jdAndroidAddEventListenerTouchStart(e) {
1377
1377
  const isH5SwiperCustomEle = e?.target?.closest('.J_h5SwiperCustom')
1378
1378
  if (!isH5SwiperCustomEle) {
1379
1379
  const hasCustomEle = e
1380
1380
  ? e?.target?.closest('.J_customScroll') ||
1381
1381
  e?.target?.closest('.J_customLayout')
1382
1382
  : false
1383
1383
  if (!hasCustomEle) {
1384
1384
  window.JdAndroid.requestEvent(false)
1385
1385
  console.log(
1386
1386
  'createJdAndroidRquestEvent 所有松果安卓APP内的document touch start事件执行检测requestEvent并重置为false',
1387
1387
  )
1388
1388
  }
1389
1389
  }
1390
1390
  }
1391
1391
  removeJdAndroidRquestEventForTouchStart() {
1392
1392
  if (isJdAndAndroidDevice && window.JdAndroid) {
1393
1393
  const rootEleNode = document.querySelector('body')
1394
1394
  if (rootEleNode) {
1395
1395
  rootEleNode.removeEventListener(
1396
1396
  'touchstart',
1397
1397
  this.jdAndroidAddEventListenerTouchStart,
1398
1398
  false,
1399
1399
  )
1400
1400
  }
1401
1401
  }
1402
1402
  }
1403
1403
 
1404
1404
  getNetWorkType() {
1405
1405
  if (isJdApp) {
1406
1406
  this.jmfeReayPromise().then(() => {
1407
1407
  window.jmfe
1408
1408
  .getNetworkStatus()
1409
1409
  .then(({ status, data }) => {
1410
1410
  console.log(
1411
1411
  '在app内初始化通过jmfe对象获取网络状态完成,当前网络状态====',
1412
1412
  data,
1413
1413
  )
1414
1414
  if (status === '0') {
1415
1415
  this.info.sysInfo['netWorkType'] = data
1416
1416
  } else {
1417
1417
  this._taroGetNetworkType()
1418
1418
  }
1419
1419
  })
1420
1420
  .catch(err => {
1421
1421
  console.log('在app内初始化通过jmfe对象获取网络状态异常====', err)
1422
1422
  this._taroGetNetworkType()
1423
1423
  })
1424
1424
  })
1425
1425
  } else {
1426
1426
  this._taroGetNetworkType()
1427
1427
  }
1428
1428
  }
1429
1429
  _taroGetNetworkType() {
1430
1430
  Taro.getNetworkType().then(getRes => {
1431
1431
  if (getRes && getRes.networkType) {
1432
1432
  this.info.sysInfo['netWorkType'] = getRes.networkType
1433
1433
  console.log(
1434
1434
  '在app内通过taro对象获取网络状态完成,当前网络状态',
1435
1435
  this.info.sysInfo['netWorkType'],
1436
1436
  )
1437
1437
  }
1438
1438
  })
1439
1439
  }
1440
1440
  getCacheAddressRouter() {
1441
1441
  if (!window.jmfe) return
1442
1442
  if (isAndroidDevice) {
1443
1443
  return window.jmfe.callRouter({
1444
1444
  jdRouter: '1',
1445
1445
  routerURL: 'router://JDAddressModule/getCacheAddress',
1446
1446
  routerParam: { sceneId: 'basicShoppingProcess' },
1447
1447
  })
1448
1448
  } else if (isIosDevice) {
1449
1449
  return window.jmfe.callRouter({
1450
1450
  jdRouter: '1',
1451
1451
  routerURL: 'router://JDBAddressCacheManagerModule/getCacheAddress',
1452
1452
  routerParam: { sceneId: 'basicShoppingProcess' },
1453
1453
  })
1454
1454
  } else if (isJdAndHarmonyDevice) {
1455
1455
  return new Promise(resolve => {
1456
1456
  resolve({})
1457
1457
  })
1458
1458
  } else {
1459
1459
  throw Error(
1460
1460
  'Unknown device type cannot obtain the address information through the route!',
1461
1461
  )
1462
1462
  }
1463
1463
  }
1464
1464
 
1465
1465
  getAddressCachePromise() {
1466
1466
  return new Promise(resolve => {
1467
1467
  if (
1468
1468
  this?.info?.sysInfo?.lat &&
1469
1469
  this?.info?.sysInfo?.lng &&
1470
1470
  this?.info?.sysInfo?.area
1471
1471
  ) {
1472
1472
  resolve({
1473
1473
  lat: this.info.sysInfo.lat,
1474
1474
  lng: this.info.sysInfo.lng,
1475
1475
  area: this?.info?.sysInfo?.area,
1476
1476
  })
1477
1477
  } else {
1478
1478
  this.jmfeReayPromise().then(() => {
1479
1479
  this.getCacheAddressRouter()
1480
1480
  .then(({ status, data }) => {
1481
1481
  console.log("原生端获取经纬度原始数据结果",status,data)
1482
1482
  if (status === '0' && data) {
1483
1483
  const {
1484
1484
  lat,
1485
1485
  latitude,
1486
1486
  lng,
1487
1487
  longitude,
1488
1488
  provinceId,
1489
1489
  cityId,
1490
1490
  countyId,
1491
1491
  townId,
1492
1492
  } = data || {}
1493
1493
  let area = ''
1494
1494
  this.info.sysInfo['lat'] = `${lat || latitude || ''}`
1495
1495
  this.info.sysInfo['lng'] = `${lng || longitude || ''}`
1496
1496
  if (provinceId) {
1497
1497
  area = `${provinceId}_${cityId || 0}_${countyId ||
1498
1498
  0}_${townId || 0}`
1499
1499
  this.info.pageInfo['address'] = area
1500
1500
  this.info.pageInfo['addressCommaStr'] = area.replace(
1501
1501
  /_/g,
1502
1502
  ',',
1503
1503
  )
1504
1504
  }
1505
1505
  })
1506
1506
  .catch(e => {
1507
1507
  console.log(' ~~ file: index.h5.ts:514 ~~ .catch ~~ e:', e)
1508
1508
  resolve({ lat: '', lng: '', area: '' })
1509
1509
  }
1510
1510
  })
1511
1511
  .catch(e => {
1512
1512
  console.log(' ~~ file: index.h5.ts:518 ~~ .catch ~~ e:', e)
1513
1513
  resolve({ lat: '', lng: '', area: '' })
1514
1514
  console.log('判断jmfe不存在,获取经纬度信息异常')
1515
1515
  })
1516
1516
  })
1517
1517
  }
1518
1518
  })
1519
1519
  }
1520
1520
 
1521
1521
  async updateMPaasConfigAsync(isBeforePageReady: boolean) {
1522
1522
  console.log("updateMPaasConfigAsync isBeforePageReady:", isBeforePageReady)
1523
1523
  if (!isJdApp) {
1524
1524
  return
1525
1525
  }
1526
1526
  const avifSwitch = await getMPaasConfigByBussinessKey(
1527
1527
  'avifSwitch',
1528
1528
  isBeforePageReady,
1529
1529
  )
1530
1530
  this.info.sysInfo.dynamicConfig['avifSwitch'] = avifSwitch
1531
1531
  const isFollowAppVideoPlayStatus = await getMPaasConfigByBussinessKey(
1532
1532
  'isFollowAppVideoPlayStatus',
1533
1533
  isBeforePageReady,
1534
1534
  )
1535
1535
  console.log(
1536
1536
  'isBeforePageReady:',
1537
1537
  isBeforePageReady,
1538
1538
  'isFollowAppVideoPlayStatus:',
1539
1539
  isFollowAppVideoPlayStatus,
1540
1540
  )
1541
1541
  if (
1542
1542
  isFollowAppVideoPlayStatus === true ||
1543
1543
  isFollowAppVideoPlayStatus === 'true'
1544
1544
  ) {
1545
1545
  this.info.sysInfo.dynamicConfig['isFollowAppVideoPlayStatus'] = true
1546
1546
  }
1547
1547
  }
1548
1548
 
1549
1549
  async getWifiVideoAutoPlayAsync() {
1550
1550
  this.info.sysInfo['wifiVideoAutoPlay'] = false
1551
1551
  if (!isJdApp) {
1552
1552
  return
1553
1553
  }
1554
1554
  const videoPlayStatus = await getWifiVideoAutoPlay().catch(e => {
1555
1555
  return 0
1556
1556
  })
1557
1557
  if (Number(videoPlayStatus) === 1) {
1558
1558
  this.info.sysInfo['wifiVideoAutoPlay'] = true
1559
1559
  }
1560
1560
  }
1561
1561
 
1562
1562
  async getMPaasConfigAsync() {
1563
1563
  this.info.sysInfo.dynamicConfig = {}
1564
1564
  this.info.sysInfo.dynamicConfig['avifSwitch'] = {}
1565
1565
  this.info.sysInfo.dynamicConfig['isFollowAppVideoPlayStatus'] = false
1566
1566
  return this.updateMPaasConfigAsync(true)
1567
1567
  }
1568
1568
 
1569
1569
  getDynamicConfig(key: string){
1570
1570
  return this.info.sysInfo.dynamicConfig[key]
1571
1571
  }
1572
1572
  async updateMPaasConfig() {
1573
1573
  console.log('updateMPaasConfig')
1574
1574
  if (
1575
1575
  isIosDevice &&
1576
1576
  versionCompare(jdAppVersionStr, MPAAS_CONFIG_APP_VERSION) < 0 &&
1577
1577
  versionCompare(jdAppVersionStr, MPAAS_CONFIG_APP_LOW_VERSION) >= 0
1578
1578
  ) {
1579
1579
  try {
1580
1580
  await this.updateMPaasConfigAsync(false)
1581
1581
  } catch (e) {
1582
1582
  console.log('updateMPaasConfigAsync:', e)
1583
1583
  }
1584
1584
  }
1585
1585
  }
1586
1586
 
1587
1587
  toLogin(options) {
1588
1588
  return this.info.isJingGouMiniViewState || this.info.isJingxiMiniViewState
1589
1589
  ? this.toWxAppLogin(options)
1590
1590
  : this.toWebLogin(options)
1591
1591
  }
1592
1592
 
1593
1593
  doLogin(options) {
1594
1594
  return this.toLogin(options)
1595
1595
  }
1596
1596
 
1597
1597
  doLoginForJdPin(options = {}) {
1598
1598
  return this.doLogin({
1599
1599
  loginColor: {
1600
1600
  biz: WXAPP_BIZ_SHOP_LIGHT_KEY,
1601
1601
  dpin: 0,
1602
1602
  },
1603
1603
  ...options,
1604
1604
  })
1605
1605
  }
1606
1606
 
1607
1607
  toWebLogin(options) {
1608
1608
  let params: {
1609
1609
  returnurl: string
1610
1610
  } = {
1611
1611
  returnurl: '',
1612
1612
  }
1613
1613
  const loginUrl = isPc
1614
1614
  ? `//passport.jd.com/new/login.aspx`
1615
1615
  : `${domain.mobileLogin}/user/login.action`
1616
1616
  const defaultParams = {
1617
1617
  appid: '100',
1618
1618
  returnurl: window.location.href,
1619
1619
  }
1620
1620
  if (isString(options)) {
1621
1621
  params = Object.assign({}, defaultParams, {
1622
1622
  returnurl: options,
1623
1623
  })
1624
1624
  } else if (isObject(options)) {
1625
1625
  const { loginColor, ...otherOptions } = options
1626
1626
  params = Object.assign({}, defaultParams, otherOptions)
1627
1627
  } else {
1628
1628
  params = defaultParams
1629
1629
  }
1630
1630
  params.returnurl = encodeURIComponent(params.returnurl)
1631
1631
  let getFullUrl = loginUrl + '?' + serialize(params)
1632
1632
  if (isPc) {
1633
1633
  getFullUrl = getFullUrl.replace(/returnurl/, 'ReturnUrl')
1634
1634
  }
1635
1635
  return Promise.resolve({
1636
1636
  h5ToUrl: true,
1637
1637
  url: getFullUrl,
1638
1638
  }).then(() => {
1639
1639
  window.location.href = getFullUrl
1640
1640
  })
1641
1641
  }
1642
1642
 
1643
1643
  toWxAppLogin(options = {}) {
1644
1644
  console.log('微信京购小程序中h5登录跳转', options)
1645
1645
  return Promise.resolve(true).then(() => {
1646
1646
  const { loginColor } = Object.assign(
1647
1647
  {},
1648
1648
  {
1649
1649
  loginColor: {
1650
1650
  biz: WXAPP_BIZ_KEY,
1651
1651
  dpin: 1,
1652
1652
  },
1653
1653
  },
1654
1654
  options,
1655
1655
  )
1656
1656
  window.location.href = `${domain.wq}/pinbind/pintokenredirect?biz=${
1657
1657
  loginColor.biz
1658
1658
  }&url=${encodeURIComponent(window.location.href)}`
1659
1659
  })
1660
1660
  }
1661
1661
 
1662
1662
  getLoginCookie() {
1663
1663
  return Promise.resolve({
1664
1664
  pin: cookie.get('pin') || '',
1665
1665
  })
1666
1666
  }
1667
1667
 
1668
1668
  clearLoginCookie() {
1669
1669
  cookie.remove('pin')
1670
1670
  }
1671
1671
 
1672
1672
  checkStatusAndLogin(options = {}) {
1673
1673
  if (!this.checkStatusAndLoginPromise) {
1674
1674
  this.checkStatusAndLoginPromise = new Promise(async (resolve, reject) => {
1675
1675
  try {
1676
1676
  const getLoginState = await this.doCheckLoginStateAndForApiCheck(
1677
1677
  options,
1678
1678
  )
1679
1679
  if (getLoginState) {
1680
1680
  resolve(true)
1681
1681
  } else {
1682
1682
  this.toLogin(options)
1683
1683
  reject(false)
1684
1684
  }
1685
1685
  } catch (e) {
1686
1686
  this.toLogin(options)
1687
1687
  reject(false)
1688
1688
  }
1689
1689
  })
1690
1690
  return this.checkStatusAndLoginPromise
1691
1691
  } else {
1692
1692
  return this.checkStatusAndLoginPromise
1693
1693
  .then(() => {
1694
1694
  return Promise.resolve(true)
1695
1695
  })
1696
1696
  .catch(() => {
1697
1697
  this.toLogin(options)
1698
1698
  return Promise.reject(true)
1699
1699
  })
1700
1700
  }
1701
1701
  }
1702
1702
 
1703
1703
  checkJdStatusAndLogin(
1704
1704
  options = {
1705
1705
  loginColor: {
1706
1706
  biz: WXAPP_BIZ_SHOP_LIGHT_KEY,
1707
1707
  dpin: 0,
1708
1708
  },
1709
1709
  },
1710
1710
  ) {
1711
1711
  return this.checkStatusAndLogin(options)
1712
1712
  }
1713
1713
 
1714
1714
  doCheckLoginStateAndForApiCheck(options) {
1715
1715
  if (this.info.loginState) {
1716
1716
  return Promise.resolve(true)
1717
1717
  } else {
1718
1718
  return new Promise((resolve, reject) => {
1719
1719
  if (
1720
1720
  this.info.isJingGouMiniViewState ||
1721
1721
  this.info.isJingxiMiniViewState
1722
1722
  ) {
1723
1723
  const getWqAuthToken = cookie.get('wq_auth_token')
1724
1724
  const getWqSkey = cookie.get('wq_skey')
1725
1725
  const getWqUin = cookie.get('wq_uin')
1726
1726
  const isLoginState =
1727
1727
  options?.loginColor?.dpin === 0
1728
1728
  ? getWqAuthToken
1729
1729
  : getWqSkey && getWqUin
1730
1730
  if (isLoginState) {
1731
1731
  this.info.loginState = true
1732
1732
  resolve(true)
1733
1733
  } else {
1734
1734
  reject(false)
1735
1735
  }
1736
1736
  } else {
1737
1737
  Taro.request({
1738
1738
  url: api.isLogin,
1739
1739
  jsonp: true,
1740
1740
  timeout: 3000,
1741
1741
  success: res => {
1742
1742
  const { statusCode, data } = res
1743
1743
  if (
1744
1744
  statusCode === 200 &&
1745
1745
  data?.islogin &&
1746
1746
  Number(data.islogin) === 1
1747
1747
  ) {
1748
1748
  this.info.loginState = true
1749
1749
  resolve(true)
1750
1750
  } else {
1751
1751
  reject(false)
1752
1752
  }
1753
1753
  },
1754
1754
  fail: err => {
1755
1755
  console.log('登录检查异常', err)
1756
1756
  reject(false)
1757
1757
  },
1758
1758
  })
1759
1759
  }
1760
1760
  })
1761
1761
  }
1762
1762
  }
1763
1763
 
1764
1764
  checkLoginStatus(options) {
1765
1765
  return new Promise(async (resolve, reject) => {
1766
1766
  try {
1767
1767
  const getLoginState = await this.doCheckLoginStateAndForApiCheck(
1768
1768
  options,
1769
1769
  )
1770
1770
  if (getLoginState) {
1771
1771
  const { pin } = await this.getLoginCookie()
1772
1772
  this.info.userInfo = {
1773
1773
  pin,
1774
1774
  encodePin: encodeURIComponent(pin),
1775
1775
  ptkey: '',
1776
1776
  }
1777
1777
  resolve(true)
1778
1778
  } else {
1779
1779
  reject(false)
1780
1780
  }
1781
1781
  } catch (e) {
1782
1782
  reject(false)
1783
1783
  }
1784
1784
  })
1785
1785
  }
1786
1786
 
1787
1787
  updatePageAndLogInfo(updateQuery = {}) {
1788
1788
  const createUpdateQueryInfo: {
1789
1789
  query: {
1790
1790
  shopId?: string | number
1791
1791
  venderId?: string | number
1792
1792
  }
1793
1793
  updateShopInfoState: boolean
1794
1794
  } = Object.assign(
1795
1795
  {},
1796
1796
  {
1797
1797
  query: {},
1798
1798
  updateShopInfoState: false,
1799
1799
  },
1800
1800
  updateQuery,
1801
1801
  )
1802
1802
  console.log(
1803
1803
  '获取当前下发的店铺查询参数',
1804
1804
  updateQuery,
1805
1805
  '获取之前保存的shopInfo店铺查询参数',
1806
1806
  this.info?.shopInfo,
1807
1807
  )
1808
1808
  const { query, updateShopInfoState } = createUpdateQueryInfo
1809
1809
  const { shopId, venderId, un_area } = query
1810
1810
  if (updateShopInfoState) {
1811
1811
  this.info.queryInfo = {
1812
1812
  ...this.info.queryInfo,
1813
1813
  ...query,
1814
1814
  }
1815
1815
  if (shopId && venderId) {
1816
1816
  this.info.shopInfo = {
1817
1817
  shopId: `${shopId}`,
1818
1818
  venderId: `${venderId}`,
1819
1819
  }
1820
1820
  }
1821
1821
  } else {
1822
1822
  this.info.queryInfo = {
1823
1823
  ...query,
1824
1824
  }
1825
1825
  if (
1826
1826
  this.info.shopInfo?.shopId &&
1827
1827
  this.info.shopInfo?.venderId &&
1828
1828
  (this.info.shopInfo.shopId == shopId ||
1829
1829
  this.info.shopInfo.venderId == venderId)
1830
1830
  ) {
1831
1831
  this.info.queryInfo.shopId = this.info.shopInfo.shopId
1832
1832
  this.info.queryInfo.venderId = this.info.shopInfo.venderId
1833
1833
  console.log(
1834
1834
  '当前存储的店铺shopId和venderId与下发的店铺信息shopId或者venderId为同一个,补充shopId或者venderId查询参数',
1835
1835
  this.info.queryInfo,
1836
1836
  )
1837
1837
  }
1838
1838
  }
1839
1839
  this.info.queryInfo['shopId'] &&
1840
1840
  (this.info.queryInfo['shopId'] = `${this.info.queryInfo['shopId']}`)
1841
1841
  this.info.queryInfo['venderId'] &&
1842
1842
  (this.info.queryInfo['venderId'] = `${this.info.queryInfo['venderId']}`)
1843
1843
  console.log(
1844
1844
  'h5==获取店铺下发查询参数\n',
1845
1845
  query,
1846
1846
  '\n获取店铺最后查询参数\n',
1847
1847
  this.info.queryInfo,
1848
1848
  '\n是否为更新店铺状态\n',
1849
1849
  updateShopInfoState,
1850
1850
  )
1851
1851
  const changeArea =
1852
1852
  un_area && un_area.length > 0 ? un_area : ipLoc_djd ? ipLoc_djd : ''
1853
1853
  if (changeArea) {
1854
1854
  this.info.pageInfo.address = changeArea
1855
1855
  this.info.pageInfo.un_area = changeArea
1856
1856
  this.info.pageInfo.addressCommaStr = changeArea.replace(/_/g, ',')
1857
1857
  }
1858
1858
  }
1859
1859
 
1860
1860
  dealLoadSdkList(){
1861
1861
  const globalLoadJsList = window?.shopGlobalSwitch?.asyncLoadJsList ?? [];
1862
1862
  const businessLoadJsList = window?.PAGE_DATA?.businessData?.asyncLoadJsList ?? []
1863
1863
  const concatLoadJsList = [].concat(globalLoadJsList,businessLoadJsList)
1864
1864
  let mergeLoadJsList = globalLoadJsList
1865
1865
  try {
1866
1866
  mergeLoadJsList = concatLoadJsList.reduce((accArr: any[], current: any) => {
1867
1867
  const getFindIndex = accArr.findIndex(item => item?.fileName === current?.fileName);
1868
1868
  getFindIndex !== -1 ? (accArr[getFindIndex] = {...accArr[getFindIndex],...current }) : accArr.push(current);
1869
1869
  return accArr;
1870
1870
  }, []);
1871
1871
  }catch (e) {
1872
1872
  console.log("LoadJsList合并错误", e)
1873
1873
  }
1874
1874
  console.log("globalLoadJsList",globalLoadJsList,"businessLoadJsList",businessLoadJsList,"两个加载jsList集合合并完成", mergeLoadJsList)
1875
1875
  this.loadJsSdkList = mergeLoadJsList
1876
1876
  return this.loadJsSdkList
1877
1877
  }
1878
1878
 
1879
1879
  renderNextTickLoadSdk(){
1880
1880
  Taro.nextTick(() => {
1881
1881
  console.log("页面渲染的下一帧执行的js加载方法,当前nextTick存在state的渲染问题,先延迟1s=======")
1882
1882
  setTimeout(() => {
1883
1883
  this.loadOtherSdk(LoadJsInitTriggerType.NRXT_TICK,this.loadJsSdkList)
1884
1884
  },1000)
1885
1885
  })
1886
1886
  }
1887
1887
 
1888
1888
  loadOtherSdk(triggerType = LoadJsInitTriggerType.NOW,loadJsList:any[] = [] ) {
1889
1889
  const getLoadJsList = Array.isArray(loadJsList) && loadJsList.length > 0 ? loadJsList : this.dealLoadSdkList()
1890
1890
  const getLoadFilterList = getLoadJsList.filter(item => {
1891
1891
  const getInitLoadEnvType = item?.initLoadEnvType || LoadJsInitLoadEnvType.ALL
1892
1892
  let getLoastLoadEventState = true
1893
1893
  if(getInitLoadEnvType === LoadJsInitLoadEnvType.JD_APP){
1894
1894
  getLoastLoadEventState = isJdApp
1895
1895
  }
1896
1896
  else if(getInitLoadEnvType === LoadJsInitLoadEnvType.M){
1897
1897
  getLoastLoadEventState = !isJdApp
1898
1898
  }
1899
1899
  const getInitTriggerType = isJdApp && item?.initJdAppTriggerType ? item?.initTriggerType : item?.initTriggerType || LoadJsInitTriggerType.NOW
1900
1900
  const getInitLinkTriggerWay = window?.PAGE_DATA[item?.initLinkTriggerWay] || false
1901
1901
  return getLoastLoadEventState && getInitTriggerType === triggerType && getInitLinkTriggerWay
1902
1902
  })
1903
1903
  console.log("获取当前触发方式",triggerType,"获取当前最后加载的js集合",getLoadFilterList,"过滤前的加载集合",getLoadJsList)
1904
1904
  getLoadFilterList.length > 0 && getLoadFilterList.forEach((item) => {
1905
1905
  const isLoadState = /sgm/.test(item?.fileName) ? window?.shopGlobalSwitch?.openSgm === "true" : true
1906
1906
  isLoadState && this.loadItemSdkPromise(item)
1907
1907
  .then((res) => {
1908
1908
  console.info('当前js地址' + item?.src, '加载状态', res)
1909
1909
  })
1910
1910
  .catch((err) => {
1911
1911
  console.info('当前js地址加载异常', item?.src)
1912
1912
  window?.fetchErrorData &&
1913
1913
  window.fetchErrorData({
1914
1914
  title: '公共js加载异常',
1915
1915
  type: "jsLoad",
1916
1916
  data: err,
1917
1917
  })
1918
1918
  })
1919
1919
  })
1920
1920
  }
1921
1921
  loadScriptEle(jsInfo,resolve,reject){
1922
1922
  const getFileName = jsInfo?.fileName;
1923
1923
  if(getFileName){
1924
1924
  const getEleId = `J_loadJs_${getFileName}`
1925
1925
  const getEle = document.getElementById(getEleId)
1926
1926
  if (!getEle) {
1927
1927
  const jsLoadErrorSgmCode = `jsLoadError_${
1928
1928
  jsInfo?.fileName || 'customJs'
1929
1929
  }`
1930
1930
  const _sgmEle = document.createElement('script')
1931
1931
  _sgmEle.id = getEleId
1932
1932
  _sgmEle.onload = function () {
1933
1933
  resolve({
1934
1934
  ...jsInfo,
1935
1935
  jsTip: "js加载成功",
1936
1936
  })
1937
1937
  }
1938
1938
  _sgmEle.onerror = function () {
1939
1939
  reject({
1940
1940
  ...jsInfo,
1941
1941
  env: getSgmCustomCode(jsLoadErrorSgmCode),
1942
1942
  jsReqError: '当前js创建标签触发onerror异常回调,请排查网络络错误或语法错误或运行时错误',
1943
1943
  })
1944
1944
  }
1945
1945
  const dataAttrList = ['timeout','fileName',"env"]
1946
1946
  const getJsInfoKeyList = Object.keys(jsInfo)
1947
1947
  getJsInfoKeyList.forEach(key => {
1948
1948
  if(key === "async"){
1949
1949
  _sgmEle.async = jsInfo[key]
1950
1950
  }else if(key === "crossOrigin"){
1951
1951
  _sgmEle.crossOrigin = jsInfo[key]
1952
1952
  }else if(key === "src"){
1953
1953
  _sgmEle.src = `${jsInfo[key]}`
1954
1954
  }else if(dataAttrList.includes(key) || /init/.test(key)) {
1955
1955
  _sgmEle.setAttribute(`data-${key}`, jsInfo[key])
1956
1956
  }else {
1957
1957
  _sgmEle.setAttribute(key, jsInfo[key])
1958
1958
  }
1959
1959
  })
1960
1960
  document.head.appendChild(_sgmEle)
1961
1961
  }else {
1962
1962
  console.log(`当前${jsInfo?.fileName || "js"}已经存在页面中,可以直接调用相关方法`,jsInfo)
1963
1963
  resolve({
1964
1964
  ...jsInfo,
1965
1965
  jsTip: "js本身已存在页面中",
1966
1966
  })
1967
1967
  }
1968
1968
  }else {
1969
1969
  console.warn("当前js资源信息缺少必要的参数fileName,请关注",jsInfo)
1970
1970
  }
1971
1971
  }
1972
1972
 
1973
1973
  loadItemSdkPromise(jsInfo = {}){
1974
1974
  if(jsInfo?.src){
1975
1975
  const getInitLoadType = isJdApp && jsInfo?.initJdAppLoadType ? jsInfo?.initJdAppLoadType : jsInfo?.initLoadType || LoadJsInitLoadType.ALL
1976
1976
  if(getInitLoadType !== LoadJsInitLoadType.NONE){
1977
1977
  const getFileKeyName = jsInfo?.fileName || jsInfo?.src
1978
1978
  if (!this.loadJsSdkListCachePromise[getFileKeyName]){
1979
1979
  if(getInitLoadType !== LoadJsInitLoadType.INSERT_ELE){
1980
1980
  this.loadJsSdkListCachePromise[getFileKeyName] = new Promise((resolve,reject) => {
1981
1981
  const jsLoadErrorSgmCode = `jsLoadError_${
1982
1982
  jsInfo?.fileName || 'customJs'
1983
1983
  }`
1984
1984
  try {
1985
1985
  const jsXhrRequest = new XMLHttpRequest();
1986
1986
  jsXhrRequest.timeout = jsInfo?.timeout ?? 2000
1987
1987
  const jsUrl = `${jsInfo?.src}`
1988
1988
  jsXhrRequest.open('GET', jsUrl, true);
1989
1989
  jsXhrRequest.onload = () => {
1990
1990
  if (jsXhrRequest.status === 200) {
1991
1991
  const getInsetHeadState = getInitLoadType === LoadJsInitLoadType.ALL
1992
1992
  if(getInsetHeadState){
1993
1993
  this.loadScriptEle(jsInfo,resolve,reject)
1994
1994
  }else {
1995
1995
  resolve({
1996
1996
  ...jsInfo,
1997
1997
  jsTip: "js请求成功,暂未插入head节点,业务自行单独插入",
1998
1998
  })
1999
1999
  }
2000
2000
  }else {
2001
2001
  const getRes = {
2002
2002
  ...jsInfo,
2003
2003
  env: getSgmCustomCode(jsLoadErrorSgmCode),
2004
2004
  jsReqError: `请求状态异常,状态为${jsXhrRequest?.status}`,
2005
2005
  jsReqState: jsXhrRequest?.status,
2006
2006
  }
2007
2007
  console.log("当前js请求状态异常", getRes)
2008
2008
  reject(getRes)
2009
2009
  }
2010
2010
  }
2011
2011
  jsXhrRequest.onerror = () => {
2012
2012
  const getRes = {
2013
2013
  ...jsInfo,
2014
2014
  env: getSgmCustomCode(jsLoadErrorSgmCode),
2015
2015
  jsReqError: '请求错误',
2016
2016
  }
2017
2017
  console.log("当前js请求错误", getRes)
2018
2018
  jsXhrRequest.abort()
2019
2019
  reject(getRes)
2020
2020
  }
2021
2021
  jsXhrRequest.ontimeout = () => {
2022
2022
  const getRes = {
2023
2023
  ...jsInfo,
2024
2024
  env: getSgmCustomCode(jsLoadErrorSgmCode),
2025
2025
  jsReqError: `请求${jsXhrRequest.timeout}ms超时异常`,
2026
2026
  }
2027
2027
  console.log("当前js请求超时异常", getRes)
2028
2028
  jsXhrRequest.abort()
2029
2029
  reject(getRes)
2030
2030
  }
2031
2031
  jsXhrRequest.send();
2032
2032
  }catch (e) {
2033
2033
  console.log("执行js请求异常",e)
2034
2034
  reject({
2035
2035
  ...jsInfo,
2036
2036
  env: getSgmCustomCode(jsLoadErrorSgmCode),
2037
2037
  jsReqError: '未知异常',
2038
2038
  error: e,
2039
2039
  })
2040
2040
  }
2041
2041
  })
2042
2042
  }else {
2043
2043
  this.loadJsSdkListCachePromise[getFileKeyName] = new Promise((resolve,reject) => {
2044
2044
  return this.loadScriptEle(jsInfo,resolve,reject)
2045
2045
  })
2046
2046
  }
2047
2047
  }
2048
2048
  return this.loadJsSdkListCachePromise[getFileKeyName]
2049
2049
  }else {
2050
2050
  return Promise.resolve({
2051
2051
  ...jsInfo,
2052
2052
  jsTip: "js加载方式设置为不加载,当前不做处理",
2053
2053
  })
2054
2054
  }
2055
2055
  }else {
2056
2056
  return Promise.reject(jsInfo)
2057
2057
  }
2058
2058
  }