auth.ts
3.52 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
// 统一登录接口(规范:优加系统 API · 统一登录)
// 继承 ApiClient,共享 http 与 RSA 加签/验签。
// 登录成功后取 res.token 写入本地(见 ./core/token),后续请求由 http 拦截器自动注入 Authorization。
import { ApiClient } from '../core/client';
/** 账号密码登录入参 */
export interface LoginParams {
account: string;
password: string;
}
/** 登录结果:令牌 + 当前用户角色与权限码(前端据此控制菜单与按钮显隐) */
export interface LoginResult {
token: string;
/** 令牌类型,固定 Bearer */
tokenType: string;
/** 令牌过期时间(date-time) */
expireAt: string;
userId: number;
account: string;
name: string;
/** 主属组织 ID */
orgId: number;
/** 数据域:1-全部数据 2-本部门及下级 3-本部门 4-仅本人 */
dataScope: number;
/** 角色编码集合 */
roleCodes: string[];
/** 权限码集合(前端据此控制按钮显隐,服务端仍会二次校验) */
permCodes: string[];
/** 是否需强制改密:true-本次使用的是初始/重置密码,前端应引导改密后才能正常使用 */
needChangePassword: boolean;
/**
* 老网关会话标识(形如 scrm-base-<uuid>-rop)。
* 优加主壳后端不用它,但积分核销等老 RPC 网关(glsx/scrm)把它作为 sessionId 放进请求体并参与加签。
* 登录接口已返回,这里补上字段,由登录流程落库到本地会话,子应用通过 getLoginSession() 读取。
*/
loginToken?: string;
}
/** 当前登录用户信息(角色 + 权限码 + 数据域) */
export interface UserInfo {
userId: number;
account: string;
name: string;
mobile: string;
orgId: number;
dataScope: number;
dataScopeLabel: string;
roleIds: number[];
roleCodes: string[];
permCodes: string[];
/** 本次登录时间(date-time) */
loginAt: string;
/** 会话过期时间(date-time) */
expireAt: string;
}
/** 修改密码入参 */
export interface ChangePasswordParams {
oldPassword: string;
newPassword: string;
}
class Auth extends ApiClient {
/** 账号密码登录 */
login(params: LoginParams) {
return this.post<LoginResult>('/system/auth/login', params);
}
/**
* 同一登录态内的用户信息缓存。
* AccessProvider 挂载后要用它恢复权限,个人资料页要用它展示账号信息,
* 加上开发期 StrictMode 的双挂载,不缓存会重复打同一个接口。
* 缓存的是请求本身,调用方各自取用;失败不缓存以便重试。
*/
private infoPromise: Promise<UserInfo> | null = null;
/** 当前登录用户信息(角色 + 权限码 + 数据域) */
info(): Promise<UserInfo> {
if (!this.infoPromise) {
this.infoPromise = this.get<UserInfo>('/system/auth/info').catch((e) => {
this.infoPromise = null;
throw e;
});
}
return this.infoPromise;
}
/** 登录 / 登出后调用:账号切换后用户信息必须重新拉取 */
clearCache() {
this.infoPromise = null;
}
/** 退出登录,注销当前令牌 */
logout() {
return this.post<void>('/system/auth/logout');
}
/** 修改密码(改后需重新登录;新密码须符合密码策略) */
changePassword(params: ChangePasswordParams) {
return this.post<void>('/system/auth/change-password', params);
}
/** 刷新当前用户的权限码缓存(权限调整后可用) */
refreshPerm() {
return this.post<number>('/system/auth/perm/refresh');
}
}
export const auth = new Auth();