adtec-core-package 3.2.8 → 3.2.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": "adtec-core-package",
3
- "version": "3.2.8",
3
+ "version": "3.2.9",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "exports": {
@@ -12,25 +12,28 @@ export default function useDictHooks(dictTypes?: string[], orgId?: string) {
12
12
  const dictStores = dictStore()
13
13
  const { clearDictCache } = dictStores
14
14
  const { dictMap, dictDefaultValueMap, dictDataMap } = storeToRefs(dictStores)
15
+
16
+ const resolveOrgId = (explicitOrgId?: string): string | undefined => {
17
+ if (explicitOrgId) return explicitOrgId
18
+ userInfo.hydrateFromRootSession()
19
+ return userInfo.user?.orgId
20
+ }
21
+
15
22
  const getDict = async (dictTypes?: string[], orgId?: string): Promise<void> => {
16
23
  try {
17
24
  if (!dictTypes || !dictTypes.length) return
18
- //判断是否存在缓存数据,如果存在则不请求接口
25
+ const tenantOrgId = resolveOrgId(orgId)
19
26
  const dictKeySet = new Set(Object.keys(dictMap.value))
20
- let uncachedDictTypes = []
21
- if (orgId) {
22
- uncachedDictTypes = dictTypes.filter((dictType) => !dictKeySet.has(`${orgId}:${dictType}`))
27
+ let uncachedDictTypes: string[] = []
28
+ if (orgId || tenantOrgId) {
29
+ const oid = orgId ?? tenantOrgId!
30
+ uncachedDictTypes = dictTypes.filter((dictType) => !dictKeySet.has(`${oid}:${dictType}`))
23
31
  } else {
24
- uncachedDictTypes = dictTypes.filter(
25
- (dictType) =>
26
- !dictKeySet.has(`${userInfo.getUserInfo.orgId}:${dictType}`) &&
27
- !dictKeySet.has(dictType)
28
- )
32
+ uncachedDictTypes = dictTypes.filter((dictType) => !dictKeySet.has(dictType))
29
33
  }
30
34
  if (!uncachedDictTypes.length) return
31
- const data = await SysDictCacheApi.batchGetSysDictDataCacheVo(uncachedDictTypes, orgId)
35
+ const data = await SysDictCacheApi.batchGetSysDictDataCacheVo(uncachedDictTypes, orgId ?? tenantOrgId)
32
36
  dictMap.value = { ...dictMap.value, ...data }
33
- // 获取默认值
34
37
  const promises = Object.keys(data).map(async (item: string) => {
35
38
  const dataMap: dictDataMapVoType = {}
36
39
  const defaultList: ISysDictDataCacheVo[] = []
@@ -72,11 +75,13 @@ export default function useDictHooks(dictTypes?: string[], orgId?: string) {
72
75
  }
73
76
 
74
77
  const getDictName = (value?: string, dictType?: string, orgId?: string): string | undefined => {
75
- //此处需要支持多租户
76
78
  let key = orgId ? `${orgId}:${dictType}` : dictType
77
79
  if (dictDataMap.value[key!]) return dictDataMap.value[key!][value!]?.label || value
78
- key = `${userInfo.getUserInfo.orgId}:${dictType}`
79
- if (dictDataMap.value[key!]) return dictDataMap.value[key!][value!]?.label || value
80
+ const tenantOrgId = resolveOrgId()
81
+ if (tenantOrgId) {
82
+ key = `${tenantOrgId}:${dictType}`
83
+ if (dictDataMap.value[key!]) return dictDataMap.value[key!][value!]?.label || value
84
+ }
80
85
  return value
81
86
  }
82
87
 
@@ -85,38 +90,43 @@ export default function useDictHooks(dictTypes?: string[], orgId?: string) {
85
90
  dictType: string,
86
91
  orgId?: string,
87
92
  ): ISysDictDataCacheVo | {} => {
88
- //此处需要支持多租户
89
93
  let key = orgId ? `${orgId}:${dictType}` : dictType
90
94
  if (dictDataMap.value[key!] && dictDataMap.value[key!][value!])
91
95
  return dictDataMap.value[key!][value!]
92
- key = `${userInfo.getUserInfo.orgId}:${dictType}`
93
- if (dictDataMap.value[key!] && dictDataMap.value[key!][value!])
94
- return dictDataMap.value[key!][value!]
96
+ const tenantOrgId = resolveOrgId()
97
+ if (tenantOrgId) {
98
+ key = `${tenantOrgId}:${dictType}`
99
+ if (dictDataMap.value[key!] && dictDataMap.value[key!][value!])
100
+ return dictDataMap.value[key!][value!]
101
+ }
95
102
  return {}
96
103
  }
97
104
  const getDictDefaultValue = (
98
105
  dictType: string,
99
106
  orgId?: string
100
107
  ): ISysDictDataCacheVo[] => {
101
- //此处需要支持多租户
102
108
  let key = orgId ? `${orgId}:${dictType}` : dictType
103
109
  if (dictDefaultValueMap.value[key!]) return dictDefaultValueMap.value[key!]
104
- key = `${userInfo.getUserInfo.orgId}:${dictType}`
105
- if (dictDefaultValueMap.value[key!]) return dictDefaultValueMap.value[key!]
110
+ const tenantOrgId = resolveOrgId()
111
+ if (tenantOrgId) {
112
+ key = `${tenantOrgId}:${dictType}`
113
+ if (dictDefaultValueMap.value[key!]) return dictDefaultValueMap.value[key!]
114
+ }
106
115
  return []
107
116
  }
108
117
  const getDictTypeData = (dictType: string, orgId?: string): ISysDictDataCacheVo[] => {
109
- //此处需要支持多租户
110
118
  let key = orgId ? `${orgId}:${dictType}` : dictType
111
119
  if (dictMap.value[key!]) return dictMap.value[key!]
112
- key = `${userInfo.getUserInfo.orgId}:${dictType}`
113
- if (dictMap.value[key!]) return dictMap.value[key!]
120
+ const tenantOrgId = resolveOrgId()
121
+ if (tenantOrgId) {
122
+ key = `${tenantOrgId}:${dictType}`
123
+ if (dictMap.value[key!]) return dictMap.value[key!]
124
+ }
114
125
  return []
115
126
  }
116
127
  const clearDict = (dictType?: string[], orgId?: string, all?: Boolean) => {
117
- clearDictCache(dictType, userInfo.getUserInfo.orgId, orgId, all)
128
+ clearDictCache(dictType, resolveOrgId(), orgId, all)
118
129
  }
119
- /** 构造函数传入的 dictTypes 是否已全部加载(缓存命中也会立即置 true) */
120
130
  const dictReady = ref(!dictTypes?.length)
121
131
  if (dictTypes?.length) {
122
132
  getDict(dictTypes, orgId).finally(() => {
@@ -6,19 +6,39 @@
6
6
  */
7
7
  import { defineStore } from 'pinia'
8
8
  import type { IUserPermissionVo } from '../interface/IUserPermissionVo'
9
+ import { readUserInfoFromSessionStorage } from '../utils/userSessionStorage'
9
10
 
10
11
  export const userInfoStore = defineStore({
11
12
  id: 'userInfoStore',
12
13
  state: () => ({
13
14
  //@ts-ignore
14
- user: null as IUserPermissionVo
15
+ user: null as IUserPermissionVo | null,
15
16
  }),
16
17
  getters: {
17
18
  getUserInfo(): IUserPermissionVo {
19
+ if (!this.user) {
20
+ const hydrated = readUserInfoFromSessionStorage()
21
+ if (hydrated) {
22
+ //@ts-ignore
23
+ this.user = hydrated
24
+ }
25
+ }
18
26
  return this.user as IUserPermissionVo
19
- }
27
+ },
20
28
  },
21
29
  actions: {
30
+ /** 无界子应用从宿主 sessionStorage 恢复登录态(与 dictStore 同源策略) */
31
+ hydrateFromRootSession(): IUserPermissionVo | null {
32
+ if (this.user) {
33
+ return this.user
34
+ }
35
+ const hydrated = readUserInfoFromSessionStorage()
36
+ if (hydrated) {
37
+ //@ts-ignore
38
+ this.user = hydrated
39
+ }
40
+ return this.user
41
+ },
22
42
  setUserInfo(userInfo: IUserPermissionVo) {
23
43
  //@ts-ignore
24
44
  this.user = userInfo
@@ -26,6 +46,6 @@ export const userInfoStore = defineStore({
26
46
  clear() {
27
47
  //@ts-ignore
28
48
  this.user = null
29
- }
30
- }
49
+ },
50
+ },
31
51
  })
@@ -0,0 +1,28 @@
1
+ import Base64 from 'crypto-js/enc-base64'
2
+ import Utf8 from 'crypto-js/enc-utf8'
3
+ import type { IUserPermissionVo } from '../interface/IUserPermissionVo'
4
+
5
+ const USER_INFO_STORAGE_KEY = 'userInfoStore'
6
+
7
+ /** wujie 子应用读宿主 sessionStorage,独立运行读自身 */
8
+ export function getRootWindow(): Window {
9
+ if (typeof window === 'undefined') return window
10
+ const w = window as Window & { __POWERED_BY_WUJIE__?: boolean }
11
+ return w.__POWERED_BY_WUJIE__ ? window.parent : window
12
+ }
13
+
14
+ /** 从 pinia-plugin-store 持久化的 sessionStorage 恢复用户信息 */
15
+ export function readUserInfoFromSessionStorage(storage?: Storage): IUserPermissionVo | null {
16
+ if (typeof window === 'undefined') return null
17
+ const store = storage ?? getRootWindow().sessionStorage
18
+ const raw = store.getItem(USER_INFO_STORAGE_KEY)
19
+ if (!raw) return null
20
+ try {
21
+ const parsed = JSON.parse(Base64.parse(raw).toString(Utf8)) as {
22
+ user?: IUserPermissionVo | null
23
+ }
24
+ return parsed?.user ?? null
25
+ } catch {
26
+ return null
27
+ }
28
+ }