83ccb207
杨鑫
最新
|
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
|
import type { UserInfo } from '@vben/types';
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { LOGIN_PATH } from '@vben/constants';
import { preferences } from '@vben/preferences';
import { resetAllStores, useAccessStore, useUserStore } from '@vben/stores';
import { notification } from 'ant-design-vue';
import { defineStore } from 'pinia';
import { thAppLogin } from '#/api/th';
import { $t } from '#/locales';
import { useDictStore } from './dict';
import { useThTenantStore } from './th-tenant';
/** 泰额多租户登录入参(POST /api/app/th-app-auth/login) */
export interface ThAuthLoginParams {
tenantId: string;
email: string;
password: string;
uuid?: string;
code?: string;
}
export const useAuthStore = defineStore('auth', () => {
const accessStore = useAccessStore();
const userStore = useUserStore();
const thTenantStore = useThTenantStore();
const router = useRouter();
const loginLoading = ref(false);
function buildUserInfoFromLogin(email: string, tenantName: string): UserInfo {
const normalizedEmail = email.trim();
return {
avatar: '',
email: normalizedEmail,
permissions: [],
realName: tenantName || normalizedEmail,
roles: [],
userId: normalizedEmail,
username: normalizedEmail,
homePath: preferences.app.defaultHomePath,
};
}
/**
* 泰额多租户登录
*/
async function authLogin(
params: ThAuthLoginParams,
onSuccess?: () => Promise<void> | void,
) {
let userInfo: null | UserInfo = null;
try {
loginLoading.value = true;
const result = await thAppLogin({
tenantId: params.tenantId,
email: params.email,
password: params.password,
uuid: params.uuid,
code: params.code,
});
if (!result.token?.trim()) {
throw new Error('登录失败:未返回 token');
}
accessStore.setAccessToken(result.token);
accessStore.setRefreshToken(result.refreshToken);
accessStore.setAccessCodes([]);
accessStore.setIsAccessChecked(false);
thTenantStore.setTenantContext({
tenantId: result.tenantId,
tenantName: result.tenantName,
locations: result.locations,
});
userInfo = buildUserInfoFromLogin(params.email, result.tenantName);
userStore.setUserInfo(userInfo);
if (accessStore.loginExpired) {
accessStore.setLoginExpired(false);
}
loginLoading.value = false;
if (onSuccess) {
await onSuccess();
} else {
await router.push(preferences.app.defaultHomePath);
}
notification.success({
description: `${$t('authentication.loginSuccessDesc')}:${userInfo.realName}`,
duration: 3,
message: $t('authentication.loginSuccess'),
});
} catch (error) {
loginLoading.value = false;
throw error;
}
return { userInfo };
}
async function logout(redirect: boolean = true) {
try {
const { doLogout, seeConnectionClose } = await import('#/api');
await seeConnectionClose();
await doLogout();
} catch (error) {
console.error(error);
} finally {
resetAllStores();
thTenantStore.$reset();
accessStore.setLoginExpired(false);
await router.replace({
path: LOGIN_PATH,
query: redirect
? {
redirect: encodeURIComponent(router.currentRoute.value.fullPath),
}
: {},
});
}
}
async function fetchUserInfo() {
const cached = userStore.userInfo;
if (cached?.username) {
return cached;
}
|
540ac0e3
杨鑫
前端修改bug
|
141
142
143
144
145
146
147
148
149
150
|
// 泰额 SAAS:用户信息来自 th-app-auth/login,不走 Yi 框架 /account
if (thTenantStore.tenantId && accessStore.accessToken) {
const fallback = buildUserInfoFromLogin(
cached?.email || cached?.username || 'user',
thTenantStore.tenantName || '',
);
userStore.setUserInfo(fallback);
return fallback;
}
|
83ccb207
杨鑫
最新
|
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
|
try {
const { getUserInfoApi } = await import('#/api');
const backUserInfo = await getUserInfoApi();
if (!backUserInfo) {
throw new Error('获取用户信息失败.');
}
const { permissionCodes = [], roleCodes = [], user } = backUserInfo;
const userInfo: UserInfo = {
avatar: user.avatar ?? '',
permissions: permissionCodes,
realName: user.nick,
roles: roleCodes,
userId: user.userId,
username: user.userName,
email: user.email ?? '',
};
userStore.setUserInfo(userInfo);
const dictStore = useDictStore();
dictStore.resetCache();
return userInfo;
} catch (error) {
if (cached) {
return cached;
}
throw error;
}
}
function $reset() {
loginLoading.value = false;
}
return {
$reset,
authLogin,
fetchUserInfo,
loginLoading,
logout,
};
});
|