Files
GERP/src/api/auth.ts
T

70 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { get, post } from './request'
import type { UserInfo } from '@/types/api'
/** 登录参数(对齐后端 LoginSerializer 字段:UserName / UserPassword) */
export interface LoginParams {
UserName: string
UserPassword: string
/** 是否记住我(延长 Token 有效期,后端当前忽略该字段) */
RememberMe?: boolean
/** 2FA 验证码(开启二步验证时必填) */
TwoFactorCode?: string
}
/** 登录响应 */
export interface LoginResult {
/** 是否需要 2FA 验证 */
RequireTwoFactor?: boolean
/** 登录成功后的跳转地址 */
Redirect?: string
/** 临时 Token(2FA 流程用) */
TempToken?: string
}
/** 登录接口(兼容后端 /Auth/Login) */
export const login = (data: LoginParams): Promise<LoginResult> => {
return post<LoginResult>('/Auth/Login', data)
}
/** 注销(清除 Cookie) */
export const logout = (): Promise<void> => {
return post<void>('/Auth/DeleteAllCookies')
}
/** 刷新 Token */
export const refreshToken = (): Promise<{ AccessToken: string }> => {
return post<{ AccessToken: string }>('/Auth/RefreshToken')
}
/** 角色项(后端 GetUserRoles 返回的元素结构) */
export interface RoleItem {
RoleUUID?: string
RoleName: string
RoleStatus?: string | number
AssignScope?: string
RoleDesc?: string
CompanyName?: string
CompanyShort?: string
CompanyUUID?: string
/** 是否为当前激活角色 */
active?: boolean
}
/** 获取当前用户角色列表(解包后端 data.roles 两层结构) */
export const getUserRoles = (): Promise<RoleItem[]> => {
return get<{ roles: RoleItem[] }>('/Auth/GetUserRoles').then((res) => res.roles || [])
}
/** 切换当前角色 */
export const switchRole = (role: string): Promise<void> => {
return post<void>('/Auth/SwitchRole', { Role: role })
}
/** 获取当前登录用户信息(silent=true 时失败不弹全局提示,用于登录页会话恢复) */
export const getCurrentUser = (silent = false): Promise<UserInfo> => {
return get<UserInfo>('/Query/GetELTUserInfo', undefined, silent ? { silent: true } : undefined)
}
/** 获取用户头像 URL(直接作为图片 src 使用) */
export const getAvatarUrl = (): string => '/Query/GetELTUserAvatar'