@smart100/spu-web-plugin 1.0.5 → 1.0.7
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 +8066 -404
- 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 +8 -6
- package/src/login.ts +109 -93
- 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)
|
|
@@ -504,6 +490,17 @@ async function singleLogin(query: IAny) {
|
|
|
504
490
|
const envname = query.envname
|
|
505
491
|
const context = query.context
|
|
506
492
|
|
|
493
|
+
function setBaseInfo() {
|
|
494
|
+
setToken(token)
|
|
495
|
+
setUserByToken(token) // 解析token为用户信息存入
|
|
496
|
+
refreshtoken ? setRefreshToken(refreshtoken) : removeRefreshToken()
|
|
497
|
+
tokenexpires ? setTokenExpires(tokenexpires) : removeTokenExpires()
|
|
498
|
+
envname ? setQueryEnvname(envname) : removeQueryEnvname()
|
|
499
|
+
// context 上下文字段 产品运营中心安装 卸载 配置 和 产品配置中心业务配置 页面需要用到
|
|
500
|
+
// web 端有传 app没传 需要做兼容
|
|
501
|
+
context && lsProxy.setItem('context', decodeURIComponent(context))
|
|
502
|
+
}
|
|
503
|
+
|
|
507
504
|
if (checkLoginByToken(token)) {
|
|
508
505
|
let isneedlogin = true // 是否需要走单点登录流程
|
|
509
506
|
const loginRole = getRoleByToken(token)
|
|
@@ -527,24 +524,18 @@ async function singleLogin(query: IAny) {
|
|
|
527
524
|
flag = true
|
|
528
525
|
}
|
|
529
526
|
}
|
|
527
|
+
// isneedlogin = true
|
|
528
|
+
// debugger
|
|
530
529
|
|
|
531
530
|
if (isneedlogin) {
|
|
532
|
-
|
|
533
|
-
setUserByToken(token) // 解析token为用户信息存入
|
|
534
|
-
|
|
535
|
-
refreshtoken ? setRefreshToken(refreshtoken) : removeRefreshToken()
|
|
536
|
-
tokenexpires ? setTokenExpires(tokenexpires) : removeTokenExpires()
|
|
537
|
-
envname ? setQueryEnvname(envname) : removeQueryEnvname()
|
|
538
|
-
|
|
539
|
-
// context 上下文字段 产品运营中心安装 卸载 配置 和 产品配置中心业务配置 页面需要用到
|
|
540
|
-
// web 端有传 app没传 需要做兼容
|
|
541
|
-
context && lsProxy.setItem('context', decodeURIComponent(context))
|
|
531
|
+
setBaseInfo()
|
|
542
532
|
|
|
543
533
|
// 单点登录写入 token 之后 换取完整的 refreshtoken
|
|
544
534
|
try {
|
|
545
535
|
if (checkLogin()) {
|
|
546
|
-
const
|
|
547
|
-
|
|
536
|
+
const refreshTokenUser = getUserByToken(getRefreshToken())
|
|
537
|
+
const tokenUser = getUserByToken(getToken())
|
|
538
|
+
if (!refreshTokenUser?.tokenId && tokenUser?.tokenId) {
|
|
548
539
|
updateToken()
|
|
549
540
|
}
|
|
550
541
|
}
|
|
@@ -552,7 +543,30 @@ async function singleLogin(query: IAny) {
|
|
|
552
543
|
console.error(err)
|
|
553
544
|
}
|
|
554
545
|
|
|
555
|
-
//
|
|
546
|
+
// 获取环境信息和租户配置信息
|
|
547
|
+
const nowEnvname = await getEnvname()
|
|
548
|
+
if (nowEnvname) {
|
|
549
|
+
const envData = await requestEnvdata(nowEnvname)
|
|
550
|
+
// debugger
|
|
551
|
+
if (envData) {
|
|
552
|
+
saveEnvdata(envData)
|
|
553
|
+
if (envData.tenantcode) {
|
|
554
|
+
setTecode(envData.tenantcode)
|
|
555
|
+
// 租户配置
|
|
556
|
+
await requestAndSetTenantSetting(envData.tenantcode)
|
|
557
|
+
} else {
|
|
558
|
+
removeTenantSetting()
|
|
559
|
+
removeTecode()
|
|
560
|
+
}
|
|
561
|
+
} else {
|
|
562
|
+
removeEnvdata()
|
|
563
|
+
removeTecode()
|
|
564
|
+
}
|
|
565
|
+
} else {
|
|
566
|
+
removeEnvdata()
|
|
567
|
+
removeTecode()
|
|
568
|
+
}
|
|
569
|
+
|
|
556
570
|
await getAndSetTenant()
|
|
557
571
|
await getAndSetUserInfo()
|
|
558
572
|
|
|
@@ -563,17 +577,23 @@ async function singleLogin(query: IAny) {
|
|
|
563
577
|
}
|
|
564
578
|
} else {
|
|
565
579
|
flag = false
|
|
566
|
-
|
|
580
|
+
setBaseInfo() // 传递的token过期依然写入 如果不写入的话 有可能之前的token本来没过期 页面依然可用
|
|
581
|
+
console.error('单点登录失败,请检查链接所传 token/refreshtoken/tokenexpires 是否非法或过期。')
|
|
567
582
|
}
|
|
568
583
|
|
|
569
|
-
//
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
envname && delete query.envname
|
|
574
|
-
context && delete query.context
|
|
584
|
+
// 登录成功之后 获取spu信息
|
|
585
|
+
if (flag) {
|
|
586
|
+
await core.initGetData()
|
|
587
|
+
}
|
|
575
588
|
|
|
576
|
-
|
|
589
|
+
if (flag) {
|
|
590
|
+
// 单点登录成功 需要删除 query 中相关参数
|
|
591
|
+
token && delete query.token
|
|
592
|
+
refreshtoken && delete query.refreshtoken
|
|
593
|
+
tokenexpires && delete query.tokenexpires
|
|
594
|
+
envname && delete query.envname
|
|
595
|
+
context && delete query.context
|
|
596
|
+
}
|
|
577
597
|
|
|
578
598
|
return {
|
|
579
599
|
flag,
|
|
@@ -594,18 +614,15 @@ function installAuth(options: any) {
|
|
|
594
614
|
if (to.query.token) {
|
|
595
615
|
const singleLoginRes = await singleLogin(to.query)
|
|
596
616
|
if (singleLoginRes.flag) {
|
|
597
|
-
// debugger
|
|
598
|
-
// next()
|
|
599
|
-
await core.initGetData()
|
|
600
617
|
next({
|
|
601
618
|
path: to.path,
|
|
602
619
|
params: to.params,
|
|
603
620
|
query: singleLoginRes.query
|
|
604
621
|
})
|
|
605
622
|
} else {
|
|
606
|
-
console.error('单点登录失败,请检查链接所传 token 是否非法或过期。')
|
|
607
623
|
next()
|
|
608
624
|
}
|
|
625
|
+
options.singleLoginCallback && options.singleLoginCallback(singleLoginRes)
|
|
609
626
|
} else {
|
|
610
627
|
next()
|
|
611
628
|
}
|
|
@@ -623,7 +640,6 @@ function installAuth(options: any) {
|
|
|
623
640
|
|
|
624
641
|
export {
|
|
625
642
|
installAuth,
|
|
626
|
-
getEnvname,
|
|
627
643
|
getToken,
|
|
628
644
|
// setToken,
|
|
629
645
|
// 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
|