-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathuserInfo.ts
More file actions
410 lines (338 loc) · 11.3 KB
/
Copy pathuserInfo.ts
File metadata and controls
410 lines (338 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import type { ConnectionInfo, PermOrgItem, RdpGraphics, UserData, LangType } from "~/types/index";
export type SiteUserData = UserData & {
language?: string;
rdpClientOption?: RdpGraphics;
connectionInfoMap?: Record<string, ConnectionInfo>;
};
// 其实应该叫做 accountInfoStore 比较好
export const useUserInfoStore = defineStore(
"userInfo",
() => {
const { setLocale } = useI18n();
const settingManager = useSettingManager();
const {
setLang,
setLangGlobal,
setSiteLanguage,
removeSiteLanguage,
getSiteLanguage,
getDefaultLanguage,
hasSiteLanguage
} = settingManager;
const orgId = ref("");
const currentSite = ref("");
const loggedIn = ref(false);
const currentLanguage = ref<LangType>("zh");
const currentUser = ref<UserData | null>(null);
const currentOrganizations = ref<PermOrgItem[]>([]);
const userMap = ref<Record<string, SiteUserData>>({});
const currentRdpClientOption = ref<RdpGraphics>({});
const currentConnectionInfoMap = ref<Record<string, ConnectionInfo>>({});
const hasUser = computed(() => Object.keys(userMap.value).length > 0);
/**
* @description 设置用户登录状态
* @param l
*/
const setUserLoggedIn = (l: boolean) => {
loggedIn.value = l;
};
/**
* @description 获取用户数据
* @param site
* @returns
*/
const getUserData = (site: string) => {
if (!(site in userMap.value)) {
return null;
}
return userMap.value[site];
};
/**
* @description 设置用户数据
* @param site
* @param userData
*/
const setUserData = (site: string, userData: UserData & { language?: LangType }) => {
const baseLang = userData.language;
setSiteLanguage(site, baseLang);
const withLanguage = {
...(userData as SiteUserData),
language: baseLang
} as SiteUserData;
userMap.value[site] = withLanguage;
currentUser.value = withLanguage;
currentSite.value = site;
currentLanguage.value = baseLang;
setLocale(baseLang);
// 设置组织 ID
if (userData.org?.id) {
orgId.value = userData.org.id;
}
// 初始化当前站点连接信息映射以及 RDP 客户端选项
currentConnectionInfoMap.value = withLanguage.connectionInfoMap || {};
currentRdpClientOption.value = withLanguage.rdpClientOption || {};
};
/**
* @description 删除用户数据
* @param site
*/
const deleteUserData = (site: string) => {
// 退出当前站点时立即请求清理其 Cookie
useTauriCoreInvoke("logout", {
name: "main",
origin: site
});
if (!(site in userMap.value)) {
return;
}
const removedLang = userMap.value[site]?.language as LangType;
delete userMap.value[site];
removeSiteLanguage(site);
// 如果还有用户,则切换到下一个用户
if (hasUser.value) {
const nextUser = Object.values(userMap.value)[0] as SiteUserData | undefined;
if (nextUser) {
const siteLang = getSiteLanguage(nextUser.site);
const nextWithLang = {
...nextUser,
language: siteLang
} as SiteUserData;
userMap.value[nextUser.site] = nextWithLang;
currentUser.value = nextWithLang;
currentSite.value = nextUser.site;
currentLanguage.value = siteLang;
setLocale(siteLang as any);
// 更新组织 ID
if (nextWithLang.org?.id) {
orgId.value = nextWithLang.org.id;
}
// 同步连接信息映射以及 RDP 客户端选项
currentConnectionInfoMap.value = nextWithLang.connectionInfoMap || {};
currentRdpClientOption.value = nextWithLang.rdpClientOption || {};
currentOrganizations.value = nextWithLang.availableOrgs || [];
loggedIn.value = true;
nextTick(() => {
useEventBus().emit("refresh", undefined);
});
}
} else {
orgId.value = "";
currentSite.value = "";
loggedIn.value = false;
currentUser.value = null;
// 将最后一个登出用户的语言持久化为默认语言
setLang(removedLang);
setLocale(removedLang);
currentLanguage.value = removedLang;
userMap.value = {};
currentRdpClientOption.value = {};
currentConnectionInfoMap.value = {};
currentOrganizations.value = [];
nextTick(() => {
useEventBus().emit("clearAssets", undefined);
});
}
};
/**
* @description 设置当前站点
* @param site
*/
const setCurrentSite = (site: string) => {
currentSite.value = site;
// 当切换站点时,同时更新当前组织列表
const userData = getUserData(site);
if (userData) {
const siteLang = getSiteLanguage(site);
const withLang = {
...(userData as SiteUserData),
language: siteLang
} as SiteUserData;
userMap.value[site] = withLang;
currentUser.value = withLang;
currentOrganizations.value = withLang.availableOrgs || [];
currentLanguage.value = siteLang;
setLocale(siteLang as any);
if (userData.org?.id) {
orgId.value = userData.org.id;
}
// 同步当前站点的连接信息映射以及 RDP 客户端选项
currentConnectionInfoMap.value = withLang.connectionInfoMap || {};
currentRdpClientOption.value = withLang.rdpClientOption || {};
} else {
const fallbackLang = currentLanguage.value;
currentConnectionInfoMap.value = {};
currentRdpClientOption.value = {};
currentLanguage.value = fallbackLang;
setLocale(fallbackLang as any);
}
};
/**
* @description 设置当前组织列表
* @param orgs
*/
const setOrganizations = (orgs: PermOrgItem[]) => {
currentOrganizations.value = orgs;
if (currentUser.value && currentSite.value) {
const updatedUserData = {
...currentUser.value,
availableOrgs: orgs
};
userMap.value[currentSite.value] = updatedUserData as SiteUserData;
currentUser.value = updatedUserData;
}
};
/**
* @description 设置当前组织
* @param org
*/
const setCurrentOrg = (org: PermOrgItem) => {
if (!currentUser.value || !currentSite.value) {
console.error("No current user or site when setting organization");
return;
}
const updatedUserData = {
...currentUser.value,
org
};
currentUser.value = updatedUserData as UserData;
orgId.value = org.id;
userMap.value[currentSite.value] = updatedUserData as SiteUserData;
};
/**
* @description 设置用户连接信息
* @param connectionInfo
* @returns
*/
const setConnectionInfoToUser = (connectionInfo: ConnectionInfo) => {
if (!currentUser.value) {
return;
}
currentUser.value.connectionInfo = connectionInfo;
};
/**
* @description 获取资产连接信息
* @param assetId 资产 ID
* @returns
*/
const getConnectionInfoForAsset = (assetId: string) => {
if (!currentSite.value) return null;
const siteData = userMap.value[currentSite.value];
return siteData?.connectionInfoMap?.[assetId] || null;
};
/**
* @description 设置资产连接信息
* @param assetId
* @param connectionInfo
*/
const setConnectionInfoForAsset = (assetId: string, connectionInfo: ConnectionInfo) => {
if (!currentSite.value) return;
const site = currentSite.value;
const siteData = userMap.value[site];
if (!siteData) return;
if (!siteData.connectionInfoMap) {
siteData.connectionInfoMap = {};
}
const existing = siteData.connectionInfoMap[assetId];
const incomingProtocols = (connectionInfo.availableProtocols || [])
.map((p) => (typeof p === "string" ? p.trim() : ""))
.filter((p) => p.length > 0);
const mergedProtocols =
incomingProtocols.length > 0
? Array.from(new Set(incomingProtocols))
: existing?.availableProtocols;
siteData.connectionInfoMap[assetId] = {
...(existing || {}),
...connectionInfo,
...(mergedProtocols && mergedProtocols.length > 0 ? { availableProtocols: mergedProtocols } : {})
};
currentConnectionInfoMap.value = { ...siteData.connectionInfoMap };
};
const applyLanguageToAll = (lang: LangType) => {
const target = lang;
// 判断是否真的需要变更
const sites = Object.keys(userMap.value);
const hasDiffInUsers = sites.some((site) => (userMap.value[site]?.language as LangType) !== target);
const hasDiffInManager =
getDefaultLanguage() !== target || sites.some((site) => getSiteLanguage(site) !== target);
if (!hasDiffInUsers && !hasDiffInManager && currentLanguage.value === target) {
return;
}
// 更新 Setting Manager
setLangGlobal(target);
// 仅对有差异的站点进行修改
const sitesToUpdate = sites.filter((site) => (hasSiteLanguage(site) ? getSiteLanguage(site) !== target : true));
sitesToUpdate.forEach((site) => setSiteLanguage(site, target));
// 4) 同步当前内存中的每个用户对象
sites.forEach((site) => {
const user = userMap.value[site];
if (!user) return;
if ((user.language as LangType) === target) return;
userMap.value[site] = {
...(user as SiteUserData),
language: target
} as SiteUserData;
});
if (currentSite.value && userMap.value[currentSite.value]) {
currentUser.value = userMap.value[currentSite.value] as SiteUserData;
}
currentLanguage.value = target;
setLocale(target);
};
/**
* @description 设置 RDP 客户端选项
* @param rdpClientOption
*/
const setRdpClientOption = (rdpClientOption: RdpGraphics) => {
currentRdpClientOption.value = rdpClientOption;
// 同步到当前站点的用户数据中,便于持久化/切换站点后恢复
if (currentSite.value && userMap.value[currentSite.value]) {
const site = currentSite.value;
const siteData = userMap.value[site] as SiteUserData;
userMap.value[site] = {
...siteData,
rdpClientOption
} as SiteUserData;
}
};
return {
orgId,
userMap,
loggedIn,
currentSite,
currentUser,
currentLanguage,
currentOrganizations,
currentRdpClientOption,
currentConnectionInfoMap,
setUserData,
getUserData,
setCurrentOrg,
setCurrentSite,
deleteUserData,
setUserLoggedIn,
setOrganizations,
setRdpClientOption,
applyLanguageToAll,
setConnectionInfoToUser,
getConnectionInfoForAsset,
setConnectionInfoForAsset
};
},
{
persist: {
key: "userInfo",
storage: localStorage,
pick: [
"orgId",
"userMap",
"loggedIn",
"currentUser",
"currentSite",
"currentLanguage",
"currentOrganizations",
"currentRdpClientOption",
"currentConnectionInfoMap"
]
}
}
);