@smart100/spu-web-plugin 0.0.24 → 0.0.26

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
@@ -1,8 +1,7 @@
1
1
  import { cloneDeep } from 'lodash-es'
2
2
  import jwtDecode from 'jwt-decode'
3
- import { axios } from './axios'
4
- import tenantInfo from './tenantInfo'
5
- import { lsProxy } from './storageProxy'
3
+ import { lsProxy, axios } from './index'
4
+ import cloudServ from './cloudServ'
6
5
  // import { functionCheck } from './utils'
7
6
 
8
7
 
@@ -45,11 +44,19 @@ class Login {
45
44
 
46
45
  async getEnvname (): Promise<string> {
47
46
  let envname = ''
48
- // web 查 envname
47
+
48
+ // web 查 context 的 envname
49
49
  let context: any = lsProxy.getItem('context')
50
50
  context && (context = JSON.parse(context))
51
- if (context?.envname) {
52
- envname = context.envname
51
+ const contextEnvname = context?.envname || ''
52
+
53
+ // 链接有些spu可能会传 envname
54
+ const queryEnvname = this.getQueryEnvname()
55
+
56
+ if (contextEnvname) {
57
+ envname = contextEnvname
58
+ } else if (queryEnvname) {
59
+ envname = queryEnvname
53
60
  } else if (window?.aPaaS?.getWebInitParams && window?.Native?.setNavigationBarReturnButton) {
54
61
  // 手机端 查 envname
55
62
  // 只有手机端有 setNavigationBarReturnButton 方法
@@ -59,9 +66,22 @@ class Login {
59
66
  })
60
67
  })
61
68
  }
69
+
62
70
  return envname
63
71
  }
64
72
 
73
+ setQueryEnvname (value: string) {
74
+ this.setData('envname', value)
75
+ }
76
+
77
+ getQueryEnvname () {
78
+ return this.getData('envname') || ''
79
+ }
80
+
81
+ removeQueryEnvname () {
82
+ this.removeData('envname')
83
+ }
84
+
65
85
  getToken () {
66
86
  return this.getData('token')
67
87
  }
@@ -190,15 +210,112 @@ class Login {
190
210
  }
191
211
  }
192
212
 
193
- // 检测用户是否登录状态
213
+ // // 产品运营中心token
214
+ // {
215
+ // "LoginUser": {
216
+ // "appId": "100",
217
+ // "tenantCode": "1656652",
218
+ // "productCode": "100000000000000000",
219
+ // "productVersionCode": null,
220
+ // "clientTypeCode": null,
221
+ // "userCode": "6",
222
+ // "accountCode": "6",
223
+ // "username": "庄焕滨",
224
+ // "tokenId": "bd69b4a4-5376-47cd-91c3-f1e1576440e5",
225
+ // "appCodes": null,
226
+ // "appCode": null,
227
+ // "platRoleCodes": ["1637696814759153664"],
228
+ // "metamodeltype": 2,
229
+ // "orgCode": "1751852081616130048",
230
+ // "centerRole": true
231
+ // },
232
+ // "TwoFactorAuthCode": "6f56da978dffe31a3b03a56c446f9467",
233
+ // "exp": 1751694745
234
+ // }
235
+
236
+ // // 租户token
237
+ // {
238
+ // "exp": 1720161305,
239
+ // "LoginUser": {
240
+ // "accountInfoCode": "1803686723986010112",
241
+ // "accountCode": "1803686724107644928",
242
+ // "tenantCode": "3000911",
243
+ // "productCode": "100000000000000000",
244
+ // "productVersionCode": "30000000000000911",
245
+ // "clientTypeCode": 1,
246
+ // "tokenId": "8614059e-69a5-4e1e-a948-f2ef680d0dd5",
247
+ // "orgCode": "1803686397149065216",
248
+ // "userInfoId": "1806591894588108800",
249
+ // "userInfoName": "woOUQJEAAAn4r5-7jffaxad6yotbEZ5A",
250
+ // "positionCode": "1803686397304254473",
251
+ // "positionName": "系统管理员-勿删",
252
+ // "memberCode": "1806591894659411968",
253
+ // "refPositionCode": "1300728614534385664",
254
+ // "categoryCode": "",
255
+ // "orgStructTypeId": "1",
256
+ // "userName": null,
257
+ // "userName1": "woOUQJEAAAn4r5-7jffaxad6yotbEZ5A",
258
+ // "userName2": null,
259
+ // "userName3": null,
260
+ // "tenantName": "智慧100-企微版-V9.1.1开发租户",
261
+ // "appCode": "sales",
262
+ // "appCodes": [
263
+ // "promotion",
264
+ // "distribution",
265
+ // "sales"
266
+ // ],
267
+ // "subPdCodes": [
268
+ // "sfa",
269
+ // "dms",
270
+ // "pmm",
271
+ // "tpm",
272
+ // "ai"
273
+ // ],
274
+ // "codepath": "1.1803686395634921472.1803686397149065216.",
275
+ // "isleaforg": "true",
276
+ // "metamodeltype": 1,
277
+ // "isSmsLogin": false
278
+ // }
279
+ // }
280
+ // 查询token所属登录角色
281
+ // tenant: 普通租户登录 默认
282
+ // center: 产品运营中心登录 单点登录时只带 token 没带 refreshtoken 和 tokenexpires
283
+ getLoginRole (token?: string) {
284
+ let loginRole: 'center' | 'tenant' = 'tenant' // center | tenant
285
+ if (token) {
286
+ const jwtInfo = this.jwtDecode(token)
287
+ if (jwtInfo?.LoginUser?.centerRole || jwtInfo?.LoginUser?.appId === '100') {
288
+ // 产品运营中心登录
289
+ loginRole = 'center'
290
+ }
291
+ }
292
+ return loginRole
293
+ }
294
+
295
+ // 检测当前用户是否登录状态
194
296
  checkLogin () {
195
297
  let haslogged = false
196
298
  const token = this.getToken()
197
- const refreshtoken = this.getRefreshToken()
198
- const tokenexpires = this.getTokenExpires()
199
- const now = Date.now()
200
- if (token && refreshtoken && tokenexpires && Number(tokenexpires) > now) {
201
- // 优化成用 jwt 解析token 获取过期时间 不需要请求接口
299
+ if (token) {
300
+ if (this.getLoginRole(token) === 'center') {
301
+ haslogged = this.checkTokenLogin(token)
302
+ } else {
303
+ const refreshtoken = this.getRefreshToken()
304
+ const tokenexpires = this.getTokenExpires()
305
+ const now = Date.now()
306
+ if (token && refreshtoken && tokenexpires && Number(tokenexpires) > now) {
307
+ haslogged = this.checkTokenLogin(token)
308
+ }
309
+ }
310
+ }
311
+ return haslogged
312
+ }
313
+
314
+ // 检测token是否过期
315
+ checkTokenLogin (token?: string) {
316
+ let haslogged = false
317
+ if (token) {
318
+ const now = Date.now()
202
319
  const jwtInfo = this.jwtDecode(token)
203
320
  if (jwtInfo?.exp) {
204
321
  haslogged = Number(jwtInfo.exp + '000') > now
@@ -210,26 +327,11 @@ class Login {
210
327
  }
211
328
 
212
329
  // 接口请求回来的 userInfo 有 functioncodes 以便做权限校验
330
+ // 有可能是中心角色请求失败 兼容不报错
213
331
  async getAndSetUserInfo () {
214
- // return axios.post('/api/teapi/rolepermission/account/getaccountinfo', {
215
- // positionid: this.getUser('positioncode'),
216
- // deviceinfo: '',
217
- // sysversion: '',
218
- // clientversion: ''
219
- // }).then((res: any) => {
220
- // // console.log(res)
221
- // // debugger
222
- // if (res.code === 200 && res.data) {
223
- // this.setUser(res.data)
224
- // }
225
- // }).catch((err: Error) => {
226
- // console.error(22)
227
- // console.error(err)
228
- // })
229
-
230
332
  try {
231
- const accountinfo: null | any = await axios.post('/api/teapi/rolepermission/account/getaccountinfo', {
232
- positionid: this.getUser('positioncode'),
333
+ const accountinfo = await axios.post('/api/teapi/rolepermission/account/getaccountinfo', {
334
+ positionid: this.getUser('positioncode') || '',
233
335
  deviceinfo: '',
234
336
  sysversion: '',
235
337
  clientversion: ''
@@ -245,57 +347,135 @@ class Login {
245
347
  }
246
348
  } catch (e) {
247
349
  console.error(e)
350
+ console.warn('获取用户信息失败,当前您登录的帐号可能为非标准租户帐号。')
351
+ }
352
+ }
353
+
354
+ formatTenant (tenant: ITenantInfo) {
355
+ if (!tenant) {
356
+ return null
357
+ }
358
+ const cloundTagMap = ['storage', 'storage-1d', 'storage-1y', 'storage-3m']
359
+ const result: NormalizedCloudServ = {}
360
+ for (const keyItem of cloundTagMap) {
361
+ const cloudServ = tenant.cloudserv[keyItem as StorageEnum]
362
+ if (cloudServ) {
363
+ result[keyItem as StorageEnum] = {
364
+ cloudserv_storage_provider: cloudServ.provider,
365
+ cloudserv_storage_storagebucket: cloudServ.storagebucket,
366
+ cloudserv_storage_storageendpoint: cloudServ.storageendpoint,
367
+ cloudserv_storage_storageurl: cloudServ.storageurl,
368
+ cloudserv_storage_accesskeyid: cloudServ.accesskeyid,
369
+ cloudserv_storage_region: cloudServ.region
370
+ }
371
+ }
372
+ }
373
+ if (Object.keys(result).length === 0) {
374
+ return null
375
+ }
376
+ return result
377
+ }
378
+
379
+ async getAndSetTenant (tenantcode?: string) {
380
+ try {
381
+ const tenantsRes: null | ITenantInfo[] = await axios.get('/api/auth/tenantlist', {}).then((res: any) => {
382
+ return res?.data?.tenants
383
+ })
384
+
385
+ let tenant: ITenantInfo | null = null
386
+ if (tenantsRes?.length) {
387
+ if (!tenantcode) {
388
+ tenant = tenantsRes[0]
389
+ } else {
390
+ tenant = tenantsRes.find((item) => item.code === tenantcode) || null
391
+ }
392
+ }
393
+
394
+ if (!tenant) {
395
+ lsProxy.removeItem('tenant')
396
+ cloudServ.remove()
397
+ } else {
398
+ lsProxy.setItem('tenant', JSON.stringify(tenant))
399
+ const normalizedTenant = this.formatTenant(tenant)
400
+ if (normalizedTenant) {
401
+ cloudServ.set(normalizedTenant)
402
+ }
403
+ }
404
+ } catch (e) {
405
+ console.error(e)
406
+ console.warn('获取租户信息失败,当前您登录的帐号可能为非标准租户帐号。')
248
407
  }
249
408
  }
250
409
 
251
410
  // 单点登录
252
411
  async singleLogin (query: IAny) {
253
- // 自动登录
254
412
  query = cloneDeep(query)
255
- const token = query.token
256
- // 之所以不强制校验 refreshtoken tokenexpires 是因为安装卸载配置页面有可能放在产品运营中心 没有这两字段
257
- if (token) {
258
- this.setToken(token)
259
- this.setUserByToken(token) // 解析token为用户信息存入
260
413
 
261
- const refreshtoken = query.refreshtoken
262
- const tokenexpires = query.tokenexpires
263
- if (refreshtoken) {
264
- this.setRefreshToken(refreshtoken)
265
- } else {
266
- this.removeRefreshToken()
267
- }
268
- if (tokenexpires) {
269
- this.setTokenExpires(tokenexpires)
414
+ let flag = false // 是否登录成功
415
+ const token = query.token
416
+ const refreshtoken = query.refreshtoken
417
+ const tokenexpires = query.tokenexpires
418
+ const envname = query.envname
419
+ const context = query.context
420
+
421
+ if (this.checkTokenLogin(token)) {
422
+ let isneedlogin = true // 是否需要走单点登录流程
423
+ const loginRole = this.getLoginRole(token)
424
+
425
+ if (loginRole === 'center') {
426
+ // 如果本地已经登录 且 query 登录参数与本地一致 说明是刚登录没多久【token也没刷新过】 视为已经登录 不需再走单点登录流程
427
+ // 之所以不强制校验 refreshtoken tokenexpires 是因为安装卸载配置页面有可能放在产品运营中心 没有这两字段
428
+ if (this.checkLogin() && token === this.getToken()) {
429
+ isneedlogin = false
430
+ flag = true
431
+ }
270
432
  } else {
271
- this.removeTokenExpires()
433
+ // 如果本地已经登录 且 query 登录参数与本地一致 说明是刚登录没多久【token也没刷新过】 视为已经登录 不需再走单点登录流程
434
+ if (this.checkLogin() && token === this.getToken() && refreshtoken === this.getRefreshToken() && tokenexpires === this.getTokenExpires()) {
435
+ isneedlogin = false
436
+ flag = true
437
+ }
272
438
  }
273
439
 
274
- // context 上下文字段 产品运营中心安装 卸载 配置 和 产品配置中心业务配置 页面需要用到
275
- let context = query.context
276
- if (context) {
277
- context = decodeURIComponent(context)
278
- lsProxy.setItem('context', context)
279
- delete query.context
280
- }
440
+ if (isneedlogin) {
441
+ this.setToken(token)
442
+ this.setUserByToken(token) // 解析token为用户信息存入
443
+
444
+ refreshtoken ? this.setRefreshToken(refreshtoken) : this.removeRefreshToken()
445
+ tokenexpires ? this.setTokenExpires(tokenexpires) : this.removeTokenExpires()
446
+ envname ? this.setQueryEnvname(envname) : this.removeQueryEnvname()
281
447
 
282
- await tenantInfo.getAndSave()
283
- await this.getAndSetUserInfo()
448
+ // context 上下文字段 产品运营中心安装 卸载 配置 和 产品配置中心业务配置 页面需要用到
449
+ // web 端有传 app没传 需要做兼容
450
+ context && lsProxy.setItem('context', decodeURIComponent(context))
284
451
 
285
- // const ischeck = functionCheck('1429379220684842752')
286
- // console.log(ischeck)
452
+ // 这里兼容报错
453
+ await this.getAndSetTenant()
454
+ await this.getAndSetUserInfo()
455
+
456
+ flag = true
457
+ }
287
458
  } else {
288
- console.error('query 中没有 token,无法单点登录。')
459
+ flag = false
460
+ console.error('没传 token 或所传 token 已过期,无法单点登录。')
289
461
  }
462
+
290
463
  // 单点登录后 无论是否成功 都需要删除 query 中相关参数
291
- delete query.token
292
- delete query.refreshtoken
293
- delete query.tokenexpires
464
+ token && delete query.token
465
+ refreshtoken && delete query.refreshtoken
466
+ tokenexpires && delete query.tokenexpires
467
+ envname && delete query.envname
468
+ context && delete query.context
469
+
294
470
  // debugger
471
+
295
472
  return {
473
+ flag,
296
474
  query
297
475
  }
298
476
  }
299
477
  }
300
478
 
301
- export default new Login()
479
+ const login = new Login()
480
+
481
+ export default login
@@ -1,14 +1,14 @@
1
- import CloudServ from '../cloudServ'
2
- import { initServToken } from './servtoken'
3
1
  import OSS from '../package/ali-oss/aliyun-oss-sdk.apaas.min.js'
4
2
  // import * as OSS from '../package/ali-oss/aliyun-oss-sdk.apaas.min.js'
5
3
  // const OSS = require('../package/ali-oss/aliyun-oss-sdk.apaas.min.js')
6
4
  import ObsClient from '../package/huaweicloud-obs/esdk-obs-browserjs.3.22.3.min.js'
7
5
  // import * as ObsClient from '../package/huaweicloud-obs/esdk-obs-browserjs.3.22.3.min.js'
8
6
  // const ObsClient = require('../package/huaweicloud-obs/esdk-obs-browserjs.3.22.3.min.js')
7
+
9
8
  import dayjs from 'dayjs'
10
- import login from '../login'
11
- import { axios } from '../axios'
9
+ import CloudServ from '../cloudServ'
10
+ import { initServToken } from './servtoken'
11
+ import { axios, getUser } from '../index'
12
12
  // import { get } from 'lodash-es'
13
13
  // import qs from 'qs'
14
14
 
@@ -108,7 +108,7 @@ const getUrl = async ({
108
108
 
109
109
  const isAliYun = CloudServ.isAliyun(storagetype)
110
110
  const isHuawei = CloudServ.isHuawei(storagetype)
111
- const tenantCode = login.getUser('tenantcode')
111
+ const tenantCode = getUser('tenantcode')
112
112
 
113
113
  if (!filename) {
114
114
  filename = source
@@ -1,5 +1,5 @@
1
1
  // import { noBaseRequest } from '@/service/http'
2
- import { axios } from '../axios'
2
+ import { axios } from '../index'
3
3
 
4
4
  export type ServToken = {
5
5
  accesskeyid: string
@@ -1,15 +1,16 @@
1
- import CloudServ from '../cloudServ'
2
- import { initServToken } from './servtoken'
3
- import { v4 as uuidv4 } from 'uuid'
4
1
  import OSS from '../package/ali-oss/aliyun-oss-sdk.apaas.min.js'
5
2
  // const OSS = require('../package/ali-oss/aliyun-oss-sdk.apaas.min.js')
6
3
  import ObsClient from '../package/huaweicloud-obs/esdk-obs-browserjs.3.22.3.min.js'
7
4
  // const ObsClient = require('../package/huaweicloud-obs/esdk-obs-browserjs.3.22.3.min.js')
5
+
6
+ import { v4 as uuidv4 } from 'uuid'
8
7
  import dayjs from 'dayjs'
9
8
  import co from 'co'
10
- import login from '../login'
11
- import { obsMultiUpload } from './multiUpload'
12
9
 
10
+ import { getUser } from '../index'
11
+ import CloudServ from '../cloudServ'
12
+ import { initServToken } from './servtoken'
13
+ import { obsMultiUpload } from './multiUpload'
13
14
 
14
15
 
15
16
  interface IUpload {
@@ -37,7 +38,7 @@ const upload = async ({
37
38
 
38
39
  const isAliYun = CloudServ.isAliyun(storagetype)
39
40
  const isHuawei = CloudServ.isHuawei(storagetype)
40
- const tenantCode = login.getUser('tenantcode')
41
+ const tenantCode = getUser('tenantcode')
41
42
  const suffix = '.' + file.name.substring(file.name.lastIndexOf('.') + 1)
42
43
  source = source ? source : (uuidv4() + suffix)
43
44
  datetime = datetime ? datetime : Date.now()