@smart100/spu-web-plugin 1.0.5 → 1.0.6
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/dist/spu-web-plugin.mjs +8044 -388
- package/package.json +4 -1
- package/src/apaasSpuTrack.ts +3 -4
- package/src/axios.ts +11 -0
- package/src/core.js +44 -91
- package/src/crypt.ts +62 -0
- package/src/envService.ts +124 -0
- package/src/index.ts +7 -6
- package/src/login.ts +87 -74
- package/src/storageCache.ts +25 -0
- package/src/utils.ts +38 -1
- package/src/nativeApi.ts +0 -57
package/src/login.ts
CHANGED
|
@@ -1,78 +1,29 @@
|
|
|
1
|
-
import { cloneDeep } from 'lodash-es'
|
|
1
|
+
import { get, cloneDeep } from 'lodash-es'
|
|
2
2
|
import jwtDecode from 'jwt-decode'
|
|
3
3
|
import { lsProxy } from './storageProxy'
|
|
4
4
|
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
|
+
import { getData, setData, removeData } from './storageCache'
|
|
10
|
+
import {
|
|
11
|
+
setQueryEnvname,
|
|
12
|
+
removeQueryEnvname,
|
|
13
|
+
requestEnvdata,
|
|
14
|
+
getEnvname,
|
|
15
|
+
removeEnvdata,
|
|
16
|
+
saveEnvdata,
|
|
17
|
+
setTecode,
|
|
18
|
+
removeTecode,
|
|
19
|
+
getTecode
|
|
20
|
+
} from './envService'
|
|
8
21
|
|
|
9
22
|
type JwtResult = {
|
|
10
23
|
LoginUser: IAny
|
|
11
24
|
exp: number
|
|
12
25
|
} | null
|
|
13
26
|
|
|
14
|
-
const cache: IAny = {}
|
|
15
|
-
|
|
16
|
-
function getData(key: string) {
|
|
17
|
-
if (cache[key]) {
|
|
18
|
-
return cache[key]
|
|
19
|
-
} else {
|
|
20
|
-
const data = lsProxy.getItem(key)
|
|
21
|
-
cache[key] = data
|
|
22
|
-
return data
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
function setData(key: string, value: any) {
|
|
27
|
-
cache[key] = value
|
|
28
|
-
lsProxy.setItem(key, value)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function removeData(key: string) {
|
|
32
|
-
delete cache[key]
|
|
33
|
-
lsProxy.removeItem(key)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
async function getEnvname(): Promise<string> {
|
|
37
|
-
let envname = ''
|
|
38
|
-
|
|
39
|
-
// web 查 context 的 envname
|
|
40
|
-
let context: any = lsProxy.getItem('context')
|
|
41
|
-
context && (context = JSON.parse(context))
|
|
42
|
-
const contextEnvname = context?.envname || ''
|
|
43
|
-
|
|
44
|
-
// 链接有些spu可能会传 envname
|
|
45
|
-
const queryEnvname = getQueryEnvname()
|
|
46
|
-
|
|
47
|
-
if (contextEnvname) {
|
|
48
|
-
envname = contextEnvname
|
|
49
|
-
} else if (queryEnvname) {
|
|
50
|
-
envname = queryEnvname
|
|
51
|
-
} else if (window?.aPaaS?.getWebInitParams && window?.Native?.setNavigationBarReturnButton) {
|
|
52
|
-
// 手机端 查 envname
|
|
53
|
-
// 只有手机端有 setNavigationBarReturnButton 方法
|
|
54
|
-
envname = await new Promise((resolve, reject) => {
|
|
55
|
-
window.aPaaS.getWebInitParams((params: any) => {
|
|
56
|
-
resolve(params?.envname || '')
|
|
57
|
-
})
|
|
58
|
-
})
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return envname
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function setQueryEnvname(value: string) {
|
|
65
|
-
setData('envname', value)
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function getQueryEnvname() {
|
|
69
|
-
return getData('envname') || ''
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function removeQueryEnvname() {
|
|
73
|
-
removeData('envname')
|
|
74
|
-
}
|
|
75
|
-
|
|
76
27
|
function getToken() {
|
|
77
28
|
return getData('token')
|
|
78
29
|
// return lsProxy.getItem('token') as string
|
|
@@ -136,9 +87,9 @@ function updateToken() {
|
|
|
136
87
|
isShowLoading: false,
|
|
137
88
|
isShowErrorMessage: false,
|
|
138
89
|
isSendToken: false,
|
|
90
|
+
isSendTecode: true,
|
|
139
91
|
headers: {
|
|
140
|
-
token: sendToken
|
|
141
|
-
tecode: getUser('tenantcode')
|
|
92
|
+
token: sendToken
|
|
142
93
|
}
|
|
143
94
|
})
|
|
144
95
|
.then((res: any) => {
|
|
@@ -260,7 +211,7 @@ function parseToken(token?: string) {
|
|
|
260
211
|
}
|
|
261
212
|
|
|
262
213
|
// // 产品运营中心token
|
|
263
|
-
// {
|
|
214
|
+
// const token = {
|
|
264
215
|
// "LoginUser": {
|
|
265
216
|
// "appId": "100",
|
|
266
217
|
// "tenantCode": "1656652",
|
|
@@ -283,7 +234,7 @@ function parseToken(token?: string) {
|
|
|
283
234
|
// }
|
|
284
235
|
|
|
285
236
|
// // 租户token
|
|
286
|
-
// {
|
|
237
|
+
// const token = {
|
|
287
238
|
// "exp": 1720161305,
|
|
288
239
|
// "LoginUser": {
|
|
289
240
|
// "accountInfoCode": "1803686723986010112",
|
|
@@ -326,6 +277,7 @@ function parseToken(token?: string) {
|
|
|
326
277
|
// "isSmsLogin": false
|
|
327
278
|
// }
|
|
328
279
|
// }
|
|
280
|
+
|
|
329
281
|
// 查询token所属登录角色
|
|
330
282
|
// tenant: 普通租户登录 默认
|
|
331
283
|
// center: 产品运营中心登录 单点登录时只带 token 没带 refreshtoken 和 tokenexpires
|
|
@@ -493,6 +445,40 @@ async function getAndSetTenant(tenantcode?: string) {
|
|
|
493
445
|
}
|
|
494
446
|
}
|
|
495
447
|
|
|
448
|
+
async function requestAndSetTenantSetting(tenantcode?: string) {
|
|
449
|
+
try {
|
|
450
|
+
const res = await axios.post(
|
|
451
|
+
'/api/auth/tenantsettings',
|
|
452
|
+
{
|
|
453
|
+
tenantcode: tenantcode || ''
|
|
454
|
+
},
|
|
455
|
+
{
|
|
456
|
+
isSendToken: false,
|
|
457
|
+
isSendTecode: true,
|
|
458
|
+
isShowErrorMessage: false
|
|
459
|
+
}
|
|
460
|
+
)
|
|
461
|
+
|
|
462
|
+
let tenantSetting = res?.data?.resp_data?.econfig || ''
|
|
463
|
+
if (tenantSetting) {
|
|
464
|
+
tenantSetting = decrypt(tenantSetting)
|
|
465
|
+
}
|
|
466
|
+
// console.log(tenantSetting)
|
|
467
|
+
if (tenantSetting) {
|
|
468
|
+
lsProxy.setItem('tenantsetting', tenantSetting)
|
|
469
|
+
} else {
|
|
470
|
+
removeTenantSetting()
|
|
471
|
+
}
|
|
472
|
+
} catch (err) {
|
|
473
|
+
console.error(err)
|
|
474
|
+
removeTenantSetting()
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
function removeTenantSetting() {
|
|
479
|
+
lsProxy.removeItem('tenantsetting')
|
|
480
|
+
}
|
|
481
|
+
|
|
496
482
|
// 单点登录
|
|
497
483
|
async function singleLogin(query: IAny) {
|
|
498
484
|
query = cloneDeep(query)
|
|
@@ -527,6 +513,8 @@ async function singleLogin(query: IAny) {
|
|
|
527
513
|
flag = true
|
|
528
514
|
}
|
|
529
515
|
}
|
|
516
|
+
// isneedlogin = true
|
|
517
|
+
// debugger
|
|
530
518
|
|
|
531
519
|
if (isneedlogin) {
|
|
532
520
|
setToken(token)
|
|
@@ -543,8 +531,9 @@ async function singleLogin(query: IAny) {
|
|
|
543
531
|
// 单点登录写入 token 之后 换取完整的 refreshtoken
|
|
544
532
|
try {
|
|
545
533
|
if (checkLogin()) {
|
|
546
|
-
const
|
|
547
|
-
|
|
534
|
+
const refreshTokenUser = getUserByToken(getRefreshToken())
|
|
535
|
+
const tokenUser = getUserByToken(getToken())
|
|
536
|
+
if (!refreshTokenUser?.tokenId && tokenUser?.tokenId) {
|
|
548
537
|
updateToken()
|
|
549
538
|
}
|
|
550
539
|
}
|
|
@@ -552,7 +541,30 @@ async function singleLogin(query: IAny) {
|
|
|
552
541
|
console.error(err)
|
|
553
542
|
}
|
|
554
543
|
|
|
555
|
-
//
|
|
544
|
+
// 获取环境信息和租户配置信息
|
|
545
|
+
const nowEnvname = await getEnvname()
|
|
546
|
+
if (nowEnvname) {
|
|
547
|
+
const envData = await requestEnvdata(nowEnvname)
|
|
548
|
+
// debugger
|
|
549
|
+
if (envData) {
|
|
550
|
+
saveEnvdata(envData)
|
|
551
|
+
if (envData.tenantcode) {
|
|
552
|
+
setTecode(envData.tenantcode)
|
|
553
|
+
// 租户配置
|
|
554
|
+
await requestAndSetTenantSetting(envData.tenantcode)
|
|
555
|
+
} else {
|
|
556
|
+
removeTenantSetting()
|
|
557
|
+
removeTecode()
|
|
558
|
+
}
|
|
559
|
+
} else {
|
|
560
|
+
removeEnvdata()
|
|
561
|
+
removeTecode()
|
|
562
|
+
}
|
|
563
|
+
} else {
|
|
564
|
+
removeEnvdata()
|
|
565
|
+
removeTecode()
|
|
566
|
+
}
|
|
567
|
+
|
|
556
568
|
await getAndSetTenant()
|
|
557
569
|
await getAndSetUserInfo()
|
|
558
570
|
|
|
@@ -566,6 +578,11 @@ async function singleLogin(query: IAny) {
|
|
|
566
578
|
console.error('没传 token 或所传 token 已过期,无法单点登录。')
|
|
567
579
|
}
|
|
568
580
|
|
|
581
|
+
// 登录成功之后 获取spu信息
|
|
582
|
+
if (flag) {
|
|
583
|
+
await core.initGetData()
|
|
584
|
+
}
|
|
585
|
+
|
|
569
586
|
// 单点登录后 无论是否成功 都需要删除 query 中相关参数
|
|
570
587
|
token && delete query.token
|
|
571
588
|
refreshtoken && delete query.refreshtoken
|
|
@@ -594,9 +611,6 @@ function installAuth(options: any) {
|
|
|
594
611
|
if (to.query.token) {
|
|
595
612
|
const singleLoginRes = await singleLogin(to.query)
|
|
596
613
|
if (singleLoginRes.flag) {
|
|
597
|
-
// debugger
|
|
598
|
-
// next()
|
|
599
|
-
await core.initGetData()
|
|
600
614
|
next({
|
|
601
615
|
path: to.path,
|
|
602
616
|
params: to.params,
|
|
@@ -623,7 +637,6 @@ function installAuth(options: any) {
|
|
|
623
637
|
|
|
624
638
|
export {
|
|
625
639
|
installAuth,
|
|
626
|
-
getEnvname,
|
|
627
640
|
getToken,
|
|
628
641
|
// setToken,
|
|
629
642
|
// removeToken,
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { lsProxy } from './storageProxy'
|
|
2
|
+
|
|
3
|
+
const cache: IAny = {}
|
|
4
|
+
|
|
5
|
+
function getData(key: string) {
|
|
6
|
+
if (cache[key]) {
|
|
7
|
+
return cache[key]
|
|
8
|
+
} else {
|
|
9
|
+
const data = lsProxy.getItem(key)
|
|
10
|
+
cache[key] = data
|
|
11
|
+
return data
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function setData(key: string, value: any) {
|
|
16
|
+
cache[key] = value
|
|
17
|
+
lsProxy.setItem(key, value)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function removeData(key: string) {
|
|
21
|
+
delete cache[key]
|
|
22
|
+
lsProxy.removeItem(key)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { getData, setData, removeData }
|
package/src/utils.ts
CHANGED
|
@@ -63,4 +63,41 @@ function isvirtuallocation(): boolean {
|
|
|
63
63
|
return urlquery.isvirtuallocation
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
|
|
66
|
+
function urlIsIp(url: string) {
|
|
67
|
+
const hostname = url.split('://')[1].split(':')[0].split('/')[0]
|
|
68
|
+
const arr = hostname.split('.')
|
|
69
|
+
if (arr.length !== 4) return false
|
|
70
|
+
let flag = true
|
|
71
|
+
for (let i = 0, len = arr.length; i < len; i++) {
|
|
72
|
+
if (!Number.isInteger(Number(arr[i]))) {
|
|
73
|
+
flag = false
|
|
74
|
+
break
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return flag
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 如果是非ip地址 则切换为与主页面一样的 location.protocol 前缀
|
|
81
|
+
function toggleHttpOrHttps(url: string) {
|
|
82
|
+
let res = url
|
|
83
|
+
if (!urlIsIp(res)) {
|
|
84
|
+
if (!res.startsWith(location.protocol)) {
|
|
85
|
+
const arr = res.split('//')
|
|
86
|
+
arr[0] = location.protocol
|
|
87
|
+
res = arr.join('//')
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return res
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export {
|
|
94
|
+
isIOS,
|
|
95
|
+
isMobile,
|
|
96
|
+
getUniqueid,
|
|
97
|
+
functionCheck,
|
|
98
|
+
setTitle,
|
|
99
|
+
isInApp,
|
|
100
|
+
isdebugger,
|
|
101
|
+
isvirtuallocation,
|
|
102
|
+
toggleHttpOrHttps
|
|
103
|
+
}
|
package/src/nativeApi.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { getUser } from './login'
|
|
2
|
-
import core from './core'
|
|
3
|
-
|
|
4
|
-
class NativeApi {
|
|
5
|
-
// 已经注入api的或者不同域的就不再注入
|
|
6
|
-
checkIsCanInject(iframe: any) {
|
|
7
|
-
try {
|
|
8
|
-
return !iframe?.contentWindow?.Module || !!iframe?.contentWindow?.Module
|
|
9
|
-
} catch (err) {
|
|
10
|
-
console.error(err)
|
|
11
|
-
console.error(`SPU 容器无法注入 Native-API,url: ${iframe.src}。`)
|
|
12
|
-
return false
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
injectApi(iframe: any, options: any) {
|
|
17
|
-
const modulekey = options.modulekey
|
|
18
|
-
// const modulekey = 'demospu'
|
|
19
|
-
|
|
20
|
-
const Module = {
|
|
21
|
-
spuContainerType: '',
|
|
22
|
-
getContextSync() {
|
|
23
|
-
return core.getContextSync(modulekey)
|
|
24
|
-
},
|
|
25
|
-
getIndextagSync: core.getIndextagSync.bind(core),
|
|
26
|
-
checkPermission: core.checkPermission.bind(core),
|
|
27
|
-
linkToPage: window?.Module?.linkToPage,
|
|
28
|
-
linkToModule: window?.Module?.linkToModule,
|
|
29
|
-
apiRequest: window?.Module?.apiRequest
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const Native = {
|
|
33
|
-
// exitPage: window?.Native?.exitPage,
|
|
34
|
-
getLocation: window?.Native?.getLocation,
|
|
35
|
-
getSystemInfoSync: window?.Native?.getSystemInfoSync
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const aPaaS = {
|
|
39
|
-
getUserInfoSync: getUser,
|
|
40
|
-
getToken: window?.aPaaS?.getToken
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
iframe.contentWindow.Module = Module
|
|
44
|
-
iframe.contentWindow.Native = Native
|
|
45
|
-
iframe.contentWindow.aPaaS = aPaaS
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
inject(iframe: any, options: any) {
|
|
49
|
-
if (this.checkIsCanInject(iframe) && options?.modulekey) {
|
|
50
|
-
this.injectApi(iframe, options)
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const nativeApi = new NativeApi()
|
|
56
|
-
|
|
57
|
-
export default nativeApi
|