2026-02-26 15:22:26 +08:00
|
|
|
import { http } from './index';
|
|
|
|
|
|
|
|
|
|
export interface LoginParams {
|
|
|
|
|
account: string;
|
|
|
|
|
password: string;
|
|
|
|
|
role: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface LoginResponse {
|
|
|
|
|
token: string;
|
|
|
|
|
user: {
|
|
|
|
|
id: number;
|
|
|
|
|
name: string;
|
|
|
|
|
role: 'admin' | 'school' | 'teacher';
|
|
|
|
|
tenantId?: number;
|
|
|
|
|
tenantName?: string;
|
|
|
|
|
email?: string;
|
|
|
|
|
phone?: string;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface UserProfile {
|
|
|
|
|
id: number;
|
2026-03-16 15:04:48 +08:00
|
|
|
username?: string;
|
2026-02-26 15:22:26 +08:00
|
|
|
name: string;
|
2026-03-16 15:04:48 +08:00
|
|
|
role: 'admin' | 'school' | 'teacher' | 'parent';
|
2026-02-26 15:22:26 +08:00
|
|
|
tenantId?: number;
|
|
|
|
|
tenantName?: string;
|
|
|
|
|
email?: string;
|
|
|
|
|
phone?: string;
|
|
|
|
|
avatar?: string;
|
2026-03-16 15:04:48 +08:00
|
|
|
avatarUrl?: string;
|
2026-02-26 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 登录
|
|
|
|
|
export function login(params: LoginParams): Promise<LoginResponse> {
|
2026-03-14 16:50:54 +08:00
|
|
|
return http.post('/v1/auth/login', {
|
2026-03-13 13:48:28 +08:00
|
|
|
username: params.account,
|
|
|
|
|
password: params.password,
|
|
|
|
|
role: params.role,
|
|
|
|
|
});
|
2026-02-26 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 登出
|
|
|
|
|
export function logout(): Promise<void> {
|
2026-03-14 16:50:54 +08:00
|
|
|
return http.post('/v1/auth/logout');
|
2026-02-26 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 刷新Token
|
|
|
|
|
export function refreshToken(): Promise<{ token: string }> {
|
2026-03-14 16:50:54 +08:00
|
|
|
return http.post('/v1/auth/refresh');
|
2026-02-26 15:22:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取当前用户信息
|
|
|
|
|
export function getProfile(): Promise<UserProfile> {
|
2026-03-14 16:50:54 +08:00
|
|
|
return http.get('/v1/auth/profile');
|
2026-02-26 15:22:26 +08:00
|
|
|
}
|
2026-03-21 12:45:56 +08:00
|
|
|
|
|
|
|
|
// 修改个人信息
|
|
|
|
|
export interface UpdateProfileDto {
|
|
|
|
|
name?: string;
|
|
|
|
|
phone?: string;
|
|
|
|
|
email?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface UpdateProfileResponse {
|
|
|
|
|
userInfo: UserProfile;
|
|
|
|
|
token: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function updateProfile(data: UpdateProfileDto): Promise<UpdateProfileResponse> {
|
|
|
|
|
return http.put('/v1/auth/profile', data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 修改密码(修改成功后 token 失效,需重新登录)
|
|
|
|
|
export function changePassword(oldPassword: string, newPassword: string): Promise<void> {
|
|
|
|
|
return http.post('/v1/auth/change-password', null, {
|
|
|
|
|
params: { oldPassword, newPassword },
|
|
|
|
|
});
|
|
|
|
|
}
|