@smart100/spu-web-plugin 0.0.34 → 0.0.36

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/src/login.ts CHANGED
@@ -84,6 +84,7 @@ class Login {
84
84
 
85
85
  getToken () {
86
86
  return this.getData('token')
87
+ // return lsProxy.getItem('token') as string
87
88
  }
88
89
 
89
90
  setToken (value: string) {
@@ -108,6 +109,7 @@ class Login {
108
109
 
109
110
  getRefreshToken () {
110
111
  return this.getData('refreshtoken')
112
+ // return lsProxy.getItem('refreshtoken') as string
111
113
  }
112
114
 
113
115
  setRefreshToken (value: string) {
@@ -118,13 +120,25 @@ class Login {
118
120
  this.removeData('refreshtoken')
119
121
  }
120
122
 
121
- private updateToken () {
123
+ updateToken () {
124
+ // 如果是产品运营中心 则不走刷新token流程
125
+ if (this.checkLogin() && this.getRole() === 'center') {
126
+ console.warn('当前登录为产品运营中心用户,不支持自动刷新token。')
127
+ return false
128
+ }
129
+ const token = this.getToken()
130
+ const refreshtoken = this.getRefreshToken()
131
+ const sendToken = this.checkLoginByToken(token) ? token : refreshtoken
122
132
  return axios.get('/api/auth/refreshtoken', {
123
133
  params: {
124
- refreshtoken: this.getRefreshToken()
134
+ refreshtoken: sendToken
125
135
  },
126
136
  isShowLoadding: false,
127
- isShowErrorMessage: false
137
+ isShowErrorMessage: false,
138
+ isSendToken: false,
139
+ headers: {
140
+ token: sendToken
141
+ }
128
142
  }).then((res: any) => {
129
143
  // console.log(res)
130
144
  const data = res?.data
@@ -139,19 +153,41 @@ class Login {
139
153
  private refreshtokenTimer: number | null = null
140
154
 
141
155
  startRefreshtoken () {
142
- // 如果有登录 则过期前15秒更新token
143
- // 如果没登录 每隔1分钟走token更新逻辑(如果刚开始没登录 后面才登录【不需要再在登陆后写刷新token逻辑】)
156
+ // 如果是产品运营中心 则不走刷新token流程
157
+ if (this.checkLogin() && this.getRole() === 'center') {
158
+ console.warn('当前登录为产品运营中心用户,不支持自动刷新token。')
159
+ return false
160
+ }
161
+
144
162
  this.stopRefreshtoken()
145
- const time = this.checkLogin() ? (Number(this.getTokenExpires()) - Date.now() - 1000 * 15) : (1000 * 60)
146
- // const time = 5000
147
- if (time > 0) {
148
- this.refreshtokenTimer = window.setTimeout(async () => {
149
- if (this.checkLogin()) {
150
- await this.updateToken()
163
+
164
+ // 如果有登录 refreshtoken 不是完整 token 则10秒后【需要等单点登录走完后才刷新不然会被覆盖】刷新一次取到完整 token
165
+ // 如果有登录 refreshtoken 是完整 token 如果剩余时间大于10分钟 则每隔10分钟刷一次 否则过期前15秒更新 token
166
+ // 如果没登录 每隔1分钟走token更新逻辑(如果刚开始没登录 后面才登录【不需要再在登陆后写刷新token逻辑】)
167
+ let time = 0
168
+ if (this.checkLogin()) {
169
+ const user = this.getUserByToken(this.getRefreshToken())
170
+ if (user?.tokenId) {
171
+ time = Number(this.getTokenExpires()) - Date.now() - 1000 * 15
172
+ // 如果剩余时间大于10分钟 则每隔10分钟刷一次
173
+ if (time > 600000) {
174
+ time = 600000
175
+ } else if (time < 0) {
176
+ time = 0
151
177
  }
152
- this.startRefreshtoken()
153
- }, time)
178
+ } else {
179
+ time = 10000
180
+ }
181
+ } else {
182
+ time = 60000
154
183
  }
184
+ // time = 5000
185
+ this.refreshtokenTimer = window.setTimeout(async () => {
186
+ if (this.checkLogin()) {
187
+ await this.updateToken()
188
+ }
189
+ this.startRefreshtoken()
190
+ }, time)
155
191
  }
156
192
 
157
193
  private stopRefreshtoken () {
@@ -185,11 +221,20 @@ class Login {
185
221
  }
186
222
 
187
223
  setUserByToken (token: string) {
224
+ const user = this.getUserByToken(token)
225
+ if (user) {
226
+ this.setUser(user)
227
+ } else {
228
+ this.removeUser()
229
+ }
230
+ }
231
+
232
+ getUserByToken (token: string) {
188
233
  const jwtInfo = this.jwtDecode(token)
189
234
  if (jwtInfo && jwtInfo.LoginUser) {
190
- this.setUser(jwtInfo.LoginUser)
235
+ return jwtInfo.LoginUser
191
236
  } else {
192
- this.removeUser()
237
+ return null
193
238
  }
194
239
  }
195
240
 
@@ -280,7 +325,7 @@ class Login {
280
325
  // 查询token所属登录角色
281
326
  // tenant: 普通租户登录 默认
282
327
  // center: 产品运营中心登录 单点登录时只带 token 没带 refreshtoken 和 tokenexpires
283
- getLoginRole (token?: string) {
328
+ getRoleByToken (token?: string) {
284
329
  let loginRole: 'center' | 'tenant' = 'tenant' // center | tenant
285
330
  if (token) {
286
331
  const jwtInfo = this.jwtDecode(token)
@@ -292,19 +337,23 @@ class Login {
292
337
  return loginRole
293
338
  }
294
339
 
340
+ getRole () {
341
+ return this.getRoleByToken(this.getToken())
342
+ }
343
+
295
344
  // 检测当前用户是否登录状态
296
345
  checkLogin () {
297
346
  let haslogged = false
298
347
  const token = this.getToken()
299
348
  if (token) {
300
- if (this.getLoginRole(token) === 'center') {
301
- haslogged = this.checkTokenLogin(token)
349
+ if (this.getRole() === 'center') {
350
+ haslogged = this.checkLoginByToken(token)
302
351
  } else {
303
352
  const refreshtoken = this.getRefreshToken()
304
353
  const tokenexpires = this.getTokenExpires()
305
354
  const now = Date.now()
306
355
  if (token && refreshtoken && tokenexpires && Number(tokenexpires) > now) {
307
- haslogged = this.checkTokenLogin(token)
356
+ haslogged = this.checkLoginByToken(token)
308
357
  }
309
358
  }
310
359
  }
@@ -312,7 +361,7 @@ class Login {
312
361
  }
313
362
 
314
363
  // 检测token是否过期
315
- checkTokenLogin (token?: string) {
364
+ checkLoginByToken (token?: string) {
316
365
  let haslogged = false
317
366
  if (token) {
318
367
  const now = Date.now()
@@ -418,9 +467,9 @@ class Login {
418
467
  const envname = query.envname
419
468
  const context = query.context
420
469
 
421
- if (this.checkTokenLogin(token)) {
470
+ if (this.checkLoginByToken(token)) {
422
471
  let isneedlogin = true // 是否需要走单点登录流程
423
- const loginRole = this.getLoginRole(token)
472
+ const loginRole = this.getRoleByToken(token)
424
473
 
425
474
  if (loginRole === 'center') {
426
475
  // 如果本地已经登录 且 query 登录参数与本地一致 说明是刚登录没多久【token也没刷新过】 视为已经登录 不需再走单点登录流程
@@ -449,6 +498,18 @@ class Login {
449
498
  // web 端有传 app没传 需要做兼容
450
499
  context && lsProxy.setItem('context', decodeURIComponent(context))
451
500
 
501
+ // 单点登录写入 token 之后 换取完整的 refreshtoken
502
+ try {
503
+ if (this.checkLogin()) {
504
+ const user = this.getUserByToken(this.getRefreshToken())
505
+ if (!user?.tokenId) {
506
+ this.updateToken()
507
+ }
508
+ }
509
+ } catch (err) {
510
+ console.error(err)
511
+ }
512
+
452
513
  // 这里兼容报错
453
514
  await this.getAndSetTenant()
454
515
  await this.getAndSetUserInfo()
@@ -0,0 +1,60 @@
1
+ import { globalOptions, axios, getUser, Module } from './index'
2
+ import { get, cloneDeep } from 'lodash-es'
3
+ import login from './login'
4
+ import { getIndextagSync } from './apaasSpuTrack'
5
+ import core from './core'
6
+
7
+ class NativeApi {
8
+ // 已经注入api的或者不同域的就不再注入
9
+ checkIsCanInject (iframe: any) {
10
+ try {
11
+ return !iframe?.contentWindow?.Module || !!iframe?.contentWindow?.Module
12
+ } catch (err) {
13
+ console.error(err)
14
+ console.error(`SPU 容器无法注入 Native-API,url: ${iframe.src}。`)
15
+ return false
16
+ }
17
+ }
18
+
19
+ injectApi (iframe: any, options: any) {
20
+ const modulekey = options.modulekey
21
+ // const modulekey = 'demospu'
22
+
23
+ const Module = {
24
+ spuContainerType: '',
25
+ getContextSync () {
26
+ return core.getContextSync(modulekey)
27
+ },
28
+ getIndextagSync: getIndextagSync,
29
+ checkPermission: core.checkPermission.bind(core),
30
+ linkToPage: window?.Module?.linkToPage,
31
+ linkToModule: window?.Module?.linkToModule,
32
+ apiRequest: window?.Module?.apiRequest
33
+ }
34
+
35
+ const Native = {
36
+ // exitPage: window?.Native?.exitPage,
37
+ getLocation: window?.Native?.getLocation,
38
+ getSystemInfoSync: window?.Native?.getSystemInfoSync
39
+ }
40
+
41
+ const aPaaS = {
42
+ getUserInfoSync: login.getUser.bind(login),
43
+ getToken: window?.aPaaS?.getToken
44
+ }
45
+
46
+ iframe.contentWindow.Module = Module
47
+ iframe.contentWindow.Native = Native
48
+ iframe.contentWindow.aPaaS = aPaaS
49
+ }
50
+
51
+ inject (iframe: any, options: any) {
52
+ if (this.checkIsCanInject(iframe) && options?.modulekey) {
53
+ this.injectApi(iframe, options)
54
+ }
55
+ }
56
+ }
57
+
58
+ const nativeApi = new NativeApi()
59
+
60
+ export default nativeApi