@smart100/spu-web-plugin 1.0.19 → 1.0.21

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smart100/spu-web-plugin",
3
- "version": "1.0.19",
3
+ "version": "1.0.21",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "dev": "npm run build:types && rollup -c -w",
package/src/axios.ts CHANGED
@@ -6,6 +6,7 @@ import { getToken, updateToken, getLoginState } from './login'
6
6
  import core from './core'
7
7
  import { urlquery } from './urlquery'
8
8
  import { getTecode } from './envService'
9
+ import { decrypt } from './crypt'
9
10
 
10
11
  interface Response {
11
12
  code: number | string
@@ -14,6 +15,34 @@ interface Response {
14
15
  message: string
15
16
  }
16
17
 
18
+ // _encrydata
19
+ const normalizeEncryData = (response: any) => {
20
+ if (response.data && response.data._encrydata && typeof response.data._encrydata === 'string') {
21
+ let res = decrypt(response.data._encrydata)
22
+ try {
23
+ let resJson = JSON.parse(res)
24
+ response.data = {
25
+ ...resJson
26
+ }
27
+ } catch (e) {
28
+ response.data = res
29
+ }
30
+ }
31
+ if (response.body && response.body._encrydata && typeof response.body._encrydata === 'string') {
32
+ let res = decrypt(response.body._encrydata)
33
+ try {
34
+ let resJson = JSON.parse(res)
35
+ response.body = {
36
+ ...resJson
37
+ }
38
+ } catch (e) {
39
+ response.body = res
40
+ }
41
+ }
42
+
43
+ return response
44
+ }
45
+
17
46
  const createAxiosInstance = (type: 'spu' | 'normal' = 'spu', options: any) => {
18
47
  const axiosInstance: AxiosInstance = axios.create({
19
48
  baseURL: type === 'spu' ? `/api/${options.modulekey}/${options.moduleversion}` : ''
@@ -97,6 +126,7 @@ const createAxiosInstance = (type: 'spu' | 'normal' = 'spu', options: any) => {
97
126
  // debugger
98
127
  const isShowLoading = get(res, 'config.isShowLoading', true)
99
128
  isShowLoading && loadding.close()
129
+ normalizeEncryData(res)
100
130
 
101
131
  let realRes: Response = {
102
132
  code: 404,
package/src/core.js CHANGED
@@ -40,7 +40,11 @@ class Core {
40
40
  const nowEnvname = await getEnvname()
41
41
  const nowTenantCode = getUser('tenantcode') || ''
42
42
  // console.log(tenantCode)
43
- if (this.cache.envName === nowEnvname && this.cache.tenantCode === nowTenantCode && this.loadStatus === 2) {
43
+ if (
44
+ this.cache.envName === nowEnvname &&
45
+ this.cache.tenantCode === nowTenantCode &&
46
+ this.loadStatus === 2
47
+ ) {
44
48
  return this.cache
45
49
  }
46
50
 
@@ -332,7 +336,9 @@ class Core {
332
336
  if (!indextag) {
333
337
  errorMsg = '缺少 indextag,请检查。'
334
338
  } else {
335
- indextagData = (moduleData.data.protocol.indexs || []).find((item) => item.indextag === indextag)
339
+ indextagData = (moduleData.data.protocol.indexs || []).find(
340
+ (item) => item.indextag === indextag
341
+ )
336
342
  if (indextagData) {
337
343
  const queryUrl = this.getQueryUrl(indextagData.query || [], queryvalue)
338
344
  const context = await this.getContext(modulekey)
@@ -348,9 +354,13 @@ class Core {
348
354
  }
349
355
 
350
356
  if (url.indexOf('?') === -1) {
351
- url += `?${queryUrl}indextag=${indextag}&context=${encodeURIComponent(JSON.stringify(context))}`
357
+ url += `?${queryUrl}indextag=${indextag}&context=${encodeURIComponent(
358
+ JSON.stringify(context)
359
+ )}`
352
360
  } else {
353
- url += `&${queryUrl}indextag=${indextag}&context=${encodeURIComponent(JSON.stringify(context))}`
361
+ url += `&${queryUrl}indextag=${indextag}&context=${encodeURIComponent(
362
+ JSON.stringify(context)
363
+ )}`
354
364
  }
355
365
  } else {
356
366
  errorMsg = `找不到 indextag = ${indextag} 的页面信息。`
@@ -405,6 +415,10 @@ class Core {
405
415
  return ''
406
416
  }
407
417
  }
418
+
419
+ checkInAppSpuWebview() {
420
+ return !!window?.aPaaS?.getPhoto
421
+ }
408
422
  }
409
423
 
410
424
  const core = new Core()
package/src/index.ts CHANGED
@@ -23,10 +23,13 @@ import { mapService } from './map/MapService'
23
23
  import AMapLoader from './map/AMapLoader'
24
24
  import {
25
25
  getToken,
26
- checkLogin,
27
- getUser,
26
+ setToken,
28
27
  getRefreshToken,
28
+ setRefreshToken,
29
29
  getTokenExpires,
30
+ setTokenExpires,
31
+ checkLogin,
32
+ getUser,
30
33
  updateToken,
31
34
  singleLogin,
32
35
  installAuth
@@ -135,6 +138,9 @@ export {
135
138
  getToken,
136
139
  getTokenExpires,
137
140
  getRefreshToken,
141
+ setToken,
142
+ setTokenExpires,
143
+ setRefreshToken,
138
144
  getUser,
139
145
  checkLogin,
140
146
  Module,
package/src/login.ts CHANGED
@@ -455,7 +455,9 @@ function startRefreshtoken() {
455
455
  // 获取 spu 容器 token
456
456
  const getSPUContainerToken = (): Promise<any> => {
457
457
  return new Promise((resolve, reject) => {
458
- if (window.aPaaS?.getToken) {
458
+ // 在spuwebview中
459
+ if (core.checkInAppSpuWebview() && window.aPaaS?.getToken) {
460
+ console.log('in App spuwebview')
459
461
  window.aPaaS.getToken((res: any) => {
460
462
  console.log('window.aPaaS.getToken success', res)
461
463
  const token = res?.token
@@ -473,6 +475,50 @@ const getSPUContainerToken = (): Promise<any> => {
473
475
  resolve(null)
474
476
  }
475
477
  })
478
+ } else if (window.aPaaS?.getToken && globalOptions.modulekey === 'portalEngine') {
479
+ console.log('in App interactivewebview')
480
+ // 兼容门户引擎在 interactivewebview 的情况
481
+ const u = navigator.userAgent
482
+ const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1
483
+ if (isAndroid) {
484
+ console.log('in Android, use window.aPaaS.getToken and window.getTokenComplete')
485
+ window.getTokenComplete = (res: any) => {
486
+ console.log('window.getTokenComplete success', res)
487
+ const token = res?.token
488
+ const tokenexpires = res?.tokenExpires
489
+ const refreshtoken = res?.refreshToken
490
+ if (token && tokenexpires && refreshtoken) {
491
+ resolve({
492
+ token,
493
+ tokenexpires,
494
+ refreshtoken
495
+ })
496
+ } else {
497
+ console.error('window.getTokenComplete fail')
498
+ resolve(null)
499
+ }
500
+ }
501
+ window.aPaaS.getToken(window.getTokenComplete)
502
+ } else {
503
+ console.log('in IOS, use window.aPaaS.getToken')
504
+ window.aPaaS.getToken((res: any) => {
505
+ console.log('window.aPaaS.getToken success', res)
506
+ const token = res?.token
507
+ const tokenexpires = res?.tokenExpires
508
+ const refreshtoken = res?.refreshToken
509
+
510
+ if (token && tokenexpires && refreshtoken) {
511
+ resolve({
512
+ token,
513
+ tokenexpires,
514
+ refreshtoken
515
+ })
516
+ } else {
517
+ console.error('window.aPaaS.getToken fail')
518
+ resolve(null)
519
+ }
520
+ })
521
+ }
476
522
  } else {
477
523
  console.warn('window.aPaaS.getToken fail: not in SPU container')
478
524
  resolve(null)
@@ -487,44 +533,70 @@ const getSPUContainerToken = (): Promise<any> => {
487
533
  // 因此 在APP端 单点登录不使用url上的token 而是直接拿App端的 App端能保证拿到的token一直不过期
488
534
  const fixLoginQuery = async (query: IAny) => {
489
535
  const newQuery = cloneDeep(query)
490
- const type = await Module.getSpuContainerType()
491
- if (type === 'app' && window.aPaaS?.getToken) {
492
- console.log('SPU is in App, singleLogin use App token.')
536
+ if (!window.aPaaS?.getToken) {
537
+ console.log('SPU is not in App, singleLogin use query token.')
538
+ } else {
539
+ if (core.checkInAppSpuWebview()) {
540
+ console.log('SPU is in App spuwebview, singleLogin use App token.')
541
+ } else if (globalOptions.modulekey === 'portalEngine') {
542
+ // 兼容门户引擎在 interactivewebview 的情况
543
+ console.log('SPU is in App interactivewebview, singleLogin use App token.')
544
+ }
493
545
  const tokenData = await getSPUContainerToken()
494
546
  if (tokenData) {
495
547
  newQuery.token = tokenData.token
496
548
  newQuery.tokenexpires = tokenData.tokenexpires
497
549
  newQuery.refreshtoken = tokenData.refreshtoken
498
550
  }
499
- } else {
500
- console.log('SPU is not in App, singleLogin use query token.')
501
551
  }
552
+
502
553
  return newQuery
503
554
  }
504
555
 
556
+ const checkTokenExpiredAndUpdate = async () => {
557
+ const loginState = getLoginState()
558
+ if (!loginState.islogin && loginState.type === 2 && loginState.role !== 'center') {
559
+ const tokenData = await getSPUContainerToken()
560
+ if (tokenData) {
561
+ setToken(tokenData.token)
562
+ setRefreshToken(tokenData.refreshtoken)
563
+ setTokenExpires(tokenData.tokenexpires)
564
+ } else {
565
+ try {
566
+ await updateToken()
567
+ } catch (err) {
568
+ console.error(err)
569
+ }
570
+ }
571
+ }
572
+ }
573
+
505
574
  // 修复 App 切到后台时间过长导致 token 过期
506
575
  // 监听 App 切换到前台 判断token是否过期 如果过期就调用获取tokne方法更新token
507
- const fixAppTokenExpired = () => {
508
- if (globalOptions.isfixapptokenexpired && window.Native?.onHostEnterForceground) {
509
- console.log('listen App enter forceground')
510
- window.Native.onHostEnterForceground(async () => {
576
+ const fixAppTokenExpired = async () => {
577
+ if (
578
+ globalOptions.isfixapptokenexpired &&
579
+ core.checkInAppSpuWebview() &&
580
+ window.Native?.onHostEnterForceground
581
+ ) {
582
+ console.log('SPU is in App spuwebview, listen App enter forceground.')
583
+ window.Native.onHostEnterForceground(() => {
511
584
  console.log('App enter forceground')
512
- const loginState = getLoginState()
513
- if (!loginState.islogin && loginState.type === 2 && loginState.role !== 'center') {
514
- const tokenData = await getSPUContainerToken()
515
- if (tokenData) {
516
- setToken(tokenData.token)
517
- setRefreshToken(tokenData.refreshtoken)
518
- setTokenExpires(tokenData.tokenexpires)
519
- } else {
520
- try {
521
- await updateToken()
522
- } catch (err) {
523
- console.error(err)
524
- }
525
- }
526
- }
585
+ checkTokenExpiredAndUpdate()
527
586
  })
587
+ } else if (
588
+ globalOptions.isfixapptokenexpired &&
589
+ globalOptions.modulekey === 'portalEngine' &&
590
+ window.aPaaS?.getToken
591
+ ) {
592
+ console.log('SPU is in App interactivewebview, listen App enter forceground.')
593
+ setTimeout(() => {
594
+ window.Native = window.Native || {}
595
+ window.Native.onHostEnterForceground = () => {
596
+ console.log('App enter forceground')
597
+ checkTokenExpiredAndUpdate()
598
+ }
599
+ }, 1000)
528
600
  }
529
601
  }
530
602
 
@@ -700,13 +772,13 @@ function installAuth(options: any) {
700
772
  export {
701
773
  installAuth,
702
774
  getToken,
703
- // setToken,
775
+ setToken,
704
776
  // removeToken,
705
777
  getTokenExpires,
706
- // setTokenExpires,
778
+ setTokenExpires,
707
779
  // removeTokenExpires,
708
780
  getRefreshToken,
709
- // setRefreshToken,
781
+ setRefreshToken,
710
782
  // removeRefreshToken,
711
783
  updateToken,
712
784
  // startRefreshtoken,
@@ -137,6 +137,9 @@ export const updateToken: () => Promise<void>
137
137
  export const getToken: () => string
138
138
  export const getTokenExpires: () => string
139
139
  export const getRefreshToken: () => string
140
+ export const setToken: (value: string) => void
141
+ export const setTokenExpires: (value: string) => void
142
+ export const setRefreshToken: (value: string) => void
140
143
  export const getUser: (key?: string) => any
141
144
  export const checkLogin: () => boolean
142
145
  export const Module: any
@@ -35,6 +35,8 @@ interface Window {
35
35
  __baiduMapInitial: any
36
36
  // 腾讯地图
37
37
  TMap: any
38
+ // InteractiveWebView 中 getToken 回调函数
39
+ getTokenComplete: any
38
40
  // wx: IAny;
39
41
  // AMapUI: IAny;
40
42
  // lsProxy: any;