2026-03-10 17:28:31 +08:00
|
|
|
import { readingApi } from "./client";
|
2026-03-10 16:44:24 +08:00
|
|
|
import type {
|
|
|
|
|
LoginRequest,
|
|
|
|
|
LoginResponse as ApiLoginResponse,
|
|
|
|
|
ResultLoginResponse,
|
|
|
|
|
ResultUserInfoResponse,
|
|
|
|
|
UserInfoResponse,
|
2026-03-10 17:28:31 +08:00
|
|
|
} from "./generated/model";
|
2026-02-28 17:51:15 +08:00
|
|
|
|
2026-03-10 17:28:31 +08:00
|
|
|
export type LoginParams = LoginRequest;
|
2026-02-28 17:51:15 +08:00
|
|
|
|
2026-03-10 16:44:24 +08:00
|
|
|
// Java 后端返回的平铺结构(保持与现有业务使用一致)
|
2026-03-10 17:28:31 +08:00
|
|
|
export interface LoginResponse extends Required<
|
|
|
|
|
Omit<ApiLoginResponse, "tenantId" | "role">
|
|
|
|
|
> {
|
|
|
|
|
role: "admin" | "school" | "teacher" | "parent";
|
|
|
|
|
tenantId?: number;
|
2026-02-28 17:51:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface UserProfile {
|
|
|
|
|
id: number;
|
|
|
|
|
name: string;
|
2026-03-10 17:28:31 +08:00
|
|
|
role: "admin" | "school" | "teacher";
|
2026-02-28 17:51:15 +08:00
|
|
|
tenantId?: number;
|
|
|
|
|
tenantName?: string;
|
|
|
|
|
email?: string;
|
|
|
|
|
phone?: string;
|
|
|
|
|
avatar?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 登录
|
|
|
|
|
export function login(params: LoginParams): Promise<LoginResponse> {
|
2026-03-10 16:44:24 +08:00
|
|
|
return readingApi.login(params).then((res) => {
|
2026-03-10 17:28:31 +08:00
|
|
|
const wrapped = res as ResultLoginResponse;
|
|
|
|
|
const data = (wrapped.data ?? {}) as ApiLoginResponse;
|
2026-03-10 16:44:24 +08:00
|
|
|
|
|
|
|
|
return {
|
2026-03-10 17:28:31 +08:00
|
|
|
token: data.token ?? "",
|
2026-03-10 16:44:24 +08:00
|
|
|
userId: data.userId ?? 0,
|
2026-03-10 17:28:31 +08:00
|
|
|
username: data.username ?? "",
|
|
|
|
|
name: data.name ?? "",
|
|
|
|
|
role: (data.role as LoginResponse["role"]) ?? "teacher",
|
2026-03-10 16:44:24 +08:00
|
|
|
tenantId: data.tenantId,
|
2026-03-10 17:28:31 +08:00
|
|
|
};
|
|
|
|
|
});
|
2026-02-28 17:51:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 登出
|
|
|
|
|
export function logout(): Promise<void> {
|
2026-03-10 17:28:31 +08:00
|
|
|
return readingApi.logout().then(() => undefined);
|
2026-02-28 17:51:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 刷新Token
|
|
|
|
|
export function refreshToken(): Promise<{ token: string }> {
|
2026-03-10 16:44:24 +08:00
|
|
|
// OpenAPI 目前未定义 refresh 接口,暂时保留原有调用路径以兼容后端
|
2026-03-10 17:28:31 +08:00
|
|
|
const { http } = require("./index");
|
|
|
|
|
return http.post("/api/v1/auth/refresh");
|
2026-02-28 17:51:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取当前用户信息
|
|
|
|
|
export function getProfile(): Promise<UserProfile> {
|
2026-03-10 16:44:24 +08:00
|
|
|
return readingApi.getCurrentUser().then((res) => {
|
2026-03-10 17:28:31 +08:00
|
|
|
const wrapped = res as ResultUserInfoResponse;
|
|
|
|
|
const data = (wrapped.data ?? {}) as UserInfoResponse;
|
2026-03-10 16:44:24 +08:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
id: data.id ?? 0,
|
2026-03-10 17:28:31 +08:00
|
|
|
name: data.name ?? "",
|
|
|
|
|
role: (data.role as UserProfile["role"]) ?? "teacher",
|
2026-03-10 16:44:24 +08:00
|
|
|
tenantId: data.tenantId,
|
|
|
|
|
tenantName: undefined,
|
|
|
|
|
email: data.email,
|
|
|
|
|
phone: data.phone,
|
|
|
|
|
avatar: data.avatarUrl,
|
2026-03-10 17:28:31 +08:00
|
|
|
};
|
|
|
|
|
});
|
2026-02-28 17:51:15 +08:00
|
|
|
}
|