@smart100/spu-web-plugin 1.0.7 → 1.0.9

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.7",
3
+ "version": "1.0.9",
4
4
  "description": "",
5
5
  "scripts": {
6
6
  "dev": "npm run build:types && rollup -c -w",
package/src/index.ts CHANGED
@@ -2,14 +2,15 @@ import { version } from '../package.json'
2
2
  import { merge } from 'lodash-es'
3
3
  import { v4 as getUuid } from 'uuid'
4
4
  import { installStorageProxy, lsProxy, ssProxy } from './storageProxy'
5
- import { getLocation, getDistance } from './location'
5
+ import { getLocation, getDistance } from './map/index'
6
6
  import { installAxios, spuAxios, axios } from './axios'
7
7
  import { installSpuConfig, spuConfig } from './spuConfig'
8
8
  import { globalConfig } from './globalConfig'
9
9
  import { downloadService, uploadService } from './oss'
10
10
  import { getUniqueid, functionCheck, setTitle, isInApp, isdebugger, isvirtuallocation } from './utils'
11
11
  import { installUrlquery } from './urlquery'
12
- import AMapLoader from './AMapLoader'
12
+ import { mapService } from './map/MapService'
13
+ import AMapLoader from './map/AMapLoader'
13
14
  import {
14
15
  getToken,
15
16
  checkLogin,
@@ -113,6 +114,7 @@ export {
113
114
  setTitle,
114
115
  isInApp,
115
116
  AMapLoader,
117
+ mapService,
116
118
  singleLogin,
117
119
  updateToken,
118
120
  getToken,
package/src/login.ts CHANGED
@@ -5,7 +5,6 @@ import { axios } from './axios'
5
5
  import cloudServ from './cloudServ'
6
6
  import core from './core'
7
7
  import { urlquery } from './urlquery'
8
- import { decrypt } from './crypt'
9
8
  import { getData, setData, removeData } from './storageCache'
10
9
  import {
11
10
  setQueryEnvname,
@@ -15,9 +14,9 @@ import {
15
14
  removeEnvdata,
16
15
  saveEnvdata,
17
16
  setTecode,
18
- removeTecode,
19
- getTecode
17
+ removeTecode
20
18
  } from './envService'
19
+ import { tenantSetting } from './tenantSetting'
21
20
 
22
21
  type JwtResult = {
23
22
  LoginUser: IAny
@@ -63,93 +62,6 @@ function removeRefreshToken() {
63
62
  removeData('refreshtoken')
64
63
  }
65
64
 
66
- function updateToken() {
67
- const loginState = getLoginState()
68
-
69
- if (!loginState.islogin && loginState.type <= 1) {
70
- console.warn('当前未登录/token过期,不支持自动刷新token。')
71
- return false
72
- }
73
-
74
- if (loginState.role === 'center') {
75
- console.warn('当前登录为产品运营中心用户,不支持自动刷新token。')
76
- return false
77
- }
78
-
79
- const token = getToken()
80
- const refreshtoken = getRefreshToken()
81
- const sendToken = checkLoginByToken(token) ? token : refreshtoken
82
- return axios
83
- .get('/api/auth/refreshtoken', {
84
- params: {
85
- refreshtoken: sendToken
86
- },
87
- isShowLoading: false,
88
- isShowErrorMessage: false,
89
- isSendToken: false,
90
- isSendTecode: true,
91
- headers: {
92
- token: sendToken
93
- }
94
- })
95
- .then((res: any) => {
96
- // console.log(res)
97
- const data = res?.data
98
- if (data) {
99
- setToken(data.token)
100
- setRefreshToken(data.refreshtoken)
101
- setTokenExpires(data.tokenexpires)
102
- }
103
- })
104
- }
105
-
106
- let refreshtokenTimer: number | null = null
107
-
108
- function startRefreshtoken() {
109
- const loginState = getLoginState()
110
- // 如果是产品运营中心 则不走刷新token流程
111
- if (loginState.role === 'center') {
112
- console.warn('当前登录为产品运营中心用户,不支持自动刷新token。')
113
- return false
114
- }
115
-
116
- // stopRefreshtoken()
117
- clearTimeout(refreshtokenTimer as number)
118
- refreshtokenTimer = null
119
-
120
- // 如果有登录 但 refreshtoken 不是完整 token 则10秒后【需要等单点登录走完后才刷新不然会被覆盖】刷新一次取到完整 token
121
- // 如果有登录 且 refreshtoken 是完整 token 如果剩余时间大于10分钟 则每隔10分钟刷一次 否则过期前15秒更新 token
122
- // 如果没登录 每隔10秒走token更新逻辑(如果刚开始没登录 后面才登录【不需要再在登陆后写刷新token逻辑】)
123
- let time = 0
124
- if (loginState.islogin) {
125
- const user = getUserByToken(getRefreshToken())
126
- if (user?.tokenId) {
127
- time = Number(getTokenExpires()) - Date.now() - 1000 * 15
128
- // 如果剩余时间大于10分钟 则每隔10分钟刷一次
129
- if (time > 600000) {
130
- time = 600000
131
- } else if (time < 0) {
132
- time = 0
133
- }
134
- } else {
135
- time = 10000
136
- }
137
- } else {
138
- if (loginState.type === 2) {
139
- time = 0
140
- } else {
141
- time = 10000
142
- }
143
- }
144
- // time = 5000
145
- refreshtokenTimer = window.setTimeout(async () => {
146
- if (getLoginState().type >= 2) {
147
- await updateToken()
148
- }
149
- startRefreshtoken()
150
- }, time)
151
- }
152
-
153
65
  function getUser(key?: string): any {
154
66
  const user = getData('user')
155
67
  const userObj = user ? JSON.parse(user) : null
@@ -445,38 +357,92 @@ async function getAndSetTenant(tenantcode?: string) {
445
357
  }
446
358
  }
447
359
 
448
- async function requestAndSetTenantSetting(tenantcode?: string) {
449
- try {
450
- const res = await axios.post(
451
- '/api/auth/tenantsettings',
452
- {
453
- tenantcode: tenantcode || ''
360
+ function updateToken() {
361
+ const loginState = getLoginState()
362
+
363
+ if (!loginState.islogin && loginState.type <= 1) {
364
+ console.warn('当前未登录/token过期,不支持自动刷新token。')
365
+ return false
366
+ }
367
+
368
+ if (loginState.role === 'center') {
369
+ console.warn('当前登录为产品运营中心用户,不支持自动刷新token。')
370
+ return false
371
+ }
372
+
373
+ const token = getToken()
374
+ const refreshtoken = getRefreshToken()
375
+ const sendToken = checkLoginByToken(token) ? token : refreshtoken
376
+ return axios
377
+ .get('/api/auth/refreshtoken', {
378
+ params: {
379
+ refreshtoken: sendToken
454
380
  },
455
- {
456
- isSendToken: false,
457
- isSendTecode: true,
458
- isShowErrorMessage: false
381
+ isShowLoading: false,
382
+ isShowErrorMessage: false,
383
+ isSendToken: false,
384
+ isSendTecode: true,
385
+ headers: {
386
+ token: sendToken
459
387
  }
460
- )
388
+ })
389
+ .then((res: any) => {
390
+ // console.log(res)
391
+ const data = res?.data
392
+ if (data) {
393
+ setToken(data.token)
394
+ setRefreshToken(data.refreshtoken)
395
+ setTokenExpires(data.tokenexpires)
396
+ }
397
+ })
398
+ }
461
399
 
462
- let tenantSetting = res?.data?.resp_data?.econfig || ''
463
- if (tenantSetting) {
464
- tenantSetting = decrypt(tenantSetting)
400
+ let refreshtokenTimer: number | null = null
401
+
402
+ function startRefreshtoken() {
403
+ const loginState = getLoginState()
404
+ // 如果是产品运营中心 则不走刷新token流程
405
+ if (loginState.role === 'center') {
406
+ console.warn('当前登录为产品运营中心用户,不支持自动刷新token。')
407
+ return false
408
+ }
409
+
410
+ // stopRefreshtoken()
411
+ clearTimeout(refreshtokenTimer as number)
412
+ refreshtokenTimer = null
413
+
414
+ // 如果有登录 但 refreshtoken 不是完整 token 则10秒后【需要等单点登录走完后才刷新不然会被覆盖】刷新一次取到完整 token
415
+ // 如果有登录 且 refreshtoken 是完整 token 如果剩余时间大于10分钟 则每隔10分钟刷一次 否则过期前15秒更新 token
416
+ // 如果没登录 每隔10秒走token更新逻辑(如果刚开始没登录 后面才登录【不需要再在登陆后写刷新token逻辑】)
417
+ let time = 0
418
+ if (loginState.islogin) {
419
+ const user = getUserByToken(getRefreshToken())
420
+ if (user?.tokenId) {
421
+ time = Number(getTokenExpires()) - Date.now() - 1000 * 15
422
+ // 如果剩余时间大于10分钟 则每隔10分钟刷一次
423
+ if (time > 600000) {
424
+ time = 600000
425
+ } else if (time < 0) {
426
+ time = 0
427
+ }
428
+ } else {
429
+ time = 10000
465
430
  }
466
- // console.log(tenantSetting)
467
- if (tenantSetting) {
468
- lsProxy.setItem('tenantsetting', tenantSetting)
431
+ } else {
432
+ if (loginState.type === 2) {
433
+ time = 0
469
434
  } else {
470
- removeTenantSetting()
435
+ // console.error('未登录,10秒后尝试更新token')
436
+ time = 30000
471
437
  }
472
- } catch (err) {
473
- console.error(err)
474
- removeTenantSetting()
475
438
  }
476
- }
477
-
478
- function removeTenantSetting() {
479
- lsProxy.removeItem('tenantsetting')
439
+ // time = 5000
440
+ refreshtokenTimer = window.setTimeout(async () => {
441
+ if (getLoginState().type >= 2) {
442
+ await updateToken()
443
+ }
444
+ startRefreshtoken()
445
+ }, time)
480
446
  }
481
447
 
482
448
  // 单点登录
@@ -530,18 +496,21 @@ async function singleLogin(query: IAny) {
530
496
  if (isneedlogin) {
531
497
  setBaseInfo()
532
498
 
533
- // 单点登录写入 token 之后 换取完整的 refreshtoken
499
+ // 单点登录写入 token 之后
500
+ // 1、如果 refreshtoken 不完整 则换取完整的 refreshtoken
534
501
  try {
535
502
  if (checkLogin()) {
536
503
  const refreshTokenUser = getUserByToken(getRefreshToken())
537
504
  const tokenUser = getUserByToken(getToken())
538
505
  if (!refreshTokenUser?.tokenId && tokenUser?.tokenId) {
539
- updateToken()
506
+ await updateToken()
540
507
  }
541
508
  }
542
509
  } catch (err) {
543
510
  console.error(err)
544
511
  }
512
+ // 2、重新计算刷新 token 时间 因为刚开始进入就 startRefreshtoken 检测到没有 token 会过10秒才执行刷新 token 操作
513
+ startRefreshtoken()
545
514
 
546
515
  // 获取环境信息和租户配置信息
547
516
  const nowEnvname = await getEnvname()
@@ -553,9 +522,9 @@ async function singleLogin(query: IAny) {
553
522
  if (envData.tenantcode) {
554
523
  setTecode(envData.tenantcode)
555
524
  // 租户配置
556
- await requestAndSetTenantSetting(envData.tenantcode)
525
+ await tenantSetting.requestAndSetTenantSetting(envData.tenantcode)
557
526
  } else {
558
- removeTenantSetting()
527
+ tenantSetting.removeTenantSetting()
559
528
  removeTecode()
560
529
  }
561
530
  } else {
@@ -0,0 +1,31 @@
1
+ import { mapService } from './MapService'
2
+
3
+ // 高德地图 web端key 和 安全密钥
4
+ const AMapKey = {
5
+ // 测试开发环境
6
+ dev: {
7
+ key: '38fa4702d240e1e6ee5cc8ca059b254f',
8
+ securityJsCode: '96f2af5670b7a41a56dcd2e8b63c1e06'
9
+ },
10
+ // 生产环境
11
+ production: {
12
+ key: '1993ac213d2f4675ac1bffb1b03ef1f0',
13
+ securityJsCode: '816fe46b7b7bce145940b93c1e4818fa'
14
+ }
15
+ }
16
+
17
+ const getAMapKey = () => {
18
+ if (mapService.isLbssettingEnable && mapService.type === 'amap') {
19
+ return {
20
+ key: mapService.key,
21
+ securityJsCode: mapService.secretkey
22
+ }
23
+ } else {
24
+ return {
25
+ key: AMapKey.production.key,
26
+ securityJsCode: AMapKey.production.securityJsCode
27
+ }
28
+ }
29
+ }
30
+
31
+ export { AMapKey, getAMapKey }
@@ -0,0 +1,43 @@
1
+ import AMapLoader from '@amap/amap-jsapi-loader'
2
+ import { AMapKey } from './AMapKey'
3
+ import { getAMapKey } from './AMapKey'
4
+
5
+ let isinit = false
6
+
7
+ const load = (options?: {
8
+ plugins?: Array<string>
9
+ AMapUI?: {
10
+ plugins?: Array<string>
11
+ }
12
+ }) => {
13
+ const AMapKey = getAMapKey()
14
+ if (!isinit) {
15
+ isinit = true
16
+ if (AMapKey.securityJsCode) {
17
+ // 本来想改成取消安全秘钥 但考虑到其他spu有可能用了需要安全秘钥的功能 因此不取消
18
+ // 如果不设置安全秘钥的话 js-api的逆地址查询不成功 返回 INVALID_USER_SCODE 改成用ipaas服务查询
19
+ window._AMapSecurityConfig = {
20
+ securityJsCode: AMapKey.securityJsCode
21
+ }
22
+ }
23
+ }
24
+
25
+ // 不开放地址查询 改用ipaas
26
+ const plugin = ['AMap.Geolocation']
27
+ // plugin.push('AMap.Geocoder') // 不开放地址查询 改用ipaas
28
+
29
+ return AMapLoader.load({
30
+ key: AMapKey.key,
31
+ version: '2.0',
32
+ plugins: options?.plugins || plugin,
33
+ AMapUI: {
34
+ version: '1.1',
35
+ plugins: options?.AMapUI?.plugins || []
36
+ }
37
+ })
38
+ }
39
+
40
+ export default {
41
+ AMapKey,
42
+ load
43
+ }
@@ -0,0 +1,167 @@
1
+ import { importJS, delay } from '../utils'
2
+ import { tenantSetting } from '../tenantSetting'
3
+ import { AMapKey } from './AMapKey'
4
+ import AMapLoader from '@amap/amap-jsapi-loader'
5
+
6
+ type MapType = 'amap' | 'tencent' | 'baidu'
7
+
8
+ type MapSupportType = 'android' | 'harmony' | 'ios' | 'web' | 'server'
9
+
10
+ interface ILbsSetting {
11
+ type: MapType
12
+ key: Record<MapSupportType, string>
13
+ secretkey: Record<MapSupportType, string>
14
+ }
15
+
16
+ interface ILbsSettingData {
17
+ enable: string
18
+ setting: ILbsSetting
19
+ }
20
+
21
+ class MapService {
22
+ isInit = false
23
+
24
+ get isLbssettingEnable() {
25
+ return tenantSetting.get('lbssetting')?.enable === '1'
26
+ }
27
+
28
+ private get setting(): ILbsSetting | undefined {
29
+ return tenantSetting.get('lbssetting')?.setting
30
+ }
31
+
32
+ get key() {
33
+ if (this.isLbssettingEnable) {
34
+ return this.setting?.key?.web || ''
35
+ } else {
36
+ return AMapKey.production.key
37
+ }
38
+ }
39
+
40
+ get secretkey() {
41
+ if (this.isLbssettingEnable) {
42
+ return this.setting?.secretkey?.web || ''
43
+ } else {
44
+ return AMapKey.production.securityJsCode
45
+ }
46
+ }
47
+
48
+ get type(): MapType {
49
+ if (this.isLbssettingEnable) {
50
+ return this.setting?.type || 'amap'
51
+ } else {
52
+ return 'amap'
53
+ }
54
+ }
55
+
56
+ get AMap() {
57
+ if (!this.isInit || this.type !== 'amap') {
58
+ return null
59
+ }
60
+ return window.AMap
61
+ }
62
+
63
+ get TMap() {
64
+ if (!this.isInit || this.type !== 'tencent') {
65
+ return null
66
+ }
67
+ return window.TMap
68
+ }
69
+
70
+ get BMap() {
71
+ if (!this.isInit || this.type !== 'baidu') {
72
+ return null
73
+ }
74
+ return window.BMap
75
+ }
76
+
77
+ async init() {
78
+ if (this.isInit) return
79
+
80
+ this.isInit = true
81
+
82
+ const type = this.type
83
+
84
+ // if (process.env.NODE_ENV === 'development') {
85
+ // type = 'baidu'
86
+ // }
87
+ if (type === 'tencent') {
88
+ await this.initTecent()
89
+ } else if (type === 'amap') {
90
+ await this.initAmap()
91
+ } else if (type === 'baidu') {
92
+ await this.initBaidu()
93
+ }
94
+ }
95
+
96
+ // reset() {
97
+ // this.isInit = false
98
+ // }
99
+
100
+ private async initTecent() {
101
+ await importJS(
102
+ `https://map.qq.com/api/gljs?v=1.exp&libraries=service&key=${this.key}`,
103
+ // `https://map.qq.com/api/gljs?v=1.exp&key=${this.key}`,
104
+ 'TMap'
105
+ )
106
+ await delay(300)
107
+ }
108
+
109
+ private async initAmap() {
110
+ // if (this.secretkey) {
111
+ // window._AMapSecurityConfig = {
112
+ // securityJsCode: this.secretkey
113
+ // }
114
+ // }
115
+
116
+ const plugin = ['AMap.Geolocation']
117
+ // plugin.push('AMap.Geocoder') // 不开放地址查询 改用ipaas
118
+
119
+ const AMap = await AMapLoader.load({
120
+ key: this.key,
121
+ version: '2.0',
122
+ plugins: plugin,
123
+ AMapUI: {
124
+ version: '1.1',
125
+ plugins: []
126
+ }
127
+ })
128
+ window.AMap = AMap
129
+ // console.log(window)
130
+ // console.log(window.AMap)
131
+ // console.log(window.AMapUI)
132
+ // console.log(window.AMapLoader)
133
+ // console.log(window.AMap === aaaa)
134
+ }
135
+
136
+ private async initBaidu() {
137
+ // await importJS(
138
+ // `https://api.map.baidu.com/api?v=1.0&&type=webgl&ak=${this.key}`,
139
+ // 'BMap'
140
+ // )
141
+ // await delay(300)
142
+ // function initialize() {
143
+ // const mp = new BMap.Map('map')
144
+ // mp.centerAndZoom(new BMap.Point(121.491, 31.233), 11)
145
+ // }
146
+ return new Promise((resolve, reject) => {
147
+ window.__baiduMapInitial = function () {
148
+ // 启用google标准坐标体系
149
+ // coordsType 指定输入输出的坐标类型,BMAP_COORD_GCJ02为gcj02坐标,BMAP_COORD_BD09为bd0ll坐标,默认为BMAP_COORD_BD09。
150
+ window.BMap.coordType = 'BMAP_COORD_GCJ02'
151
+ resolve(null)
152
+ }
153
+ const script = document.createElement('script')
154
+ // 使用最新 3.0 api https://lbsyun.baidu.com/index.php?title=jspopular3.0
155
+ script.src = `https://api.map.baidu.com/api?v=3.0&ak=${this.key}&callback=__baiduMapInitial`
156
+ // script.src = `https://api.map.baidu.com/api?v=1.0&&type=webgl&ak=${this.key}&callback=__baiduMapInitial`
157
+ script.onerror = (err) => {
158
+ reject(err)
159
+ }
160
+ document.body.appendChild(script)
161
+ })
162
+ }
163
+ }
164
+
165
+ const mapService = new MapService()
166
+
167
+ export { mapService }