154 lines
3.8 KiB
TypeScript
154 lines
3.8 KiB
TypeScript
/**
|
||
* 登录客户端
|
||
* 调用后端 /auth/login 接口,传递 OAuth 用户信息,获取 JWT token
|
||
*/
|
||
|
||
import { API_BASE_URL } from "~/config/api-config";
|
||
import axios from 'axios';
|
||
|
||
/**
|
||
* 登录请求参数(OAuth 方式)
|
||
*/
|
||
export interface LoginRequest {
|
||
userInfo: {
|
||
sub: string;
|
||
username?: string;
|
||
nickname?: string;
|
||
email?: string;
|
||
phone_number?: string;
|
||
ou_id?: string;
|
||
ou_name?: string;
|
||
is_leader?: boolean;
|
||
[key: string]: unknown;
|
||
};
|
||
expiresIn: number;
|
||
area?: string;
|
||
}
|
||
|
||
/**
|
||
* 登录响应
|
||
*/
|
||
export interface LoginResponse {
|
||
success: boolean;
|
||
data?: {
|
||
access_token: string;
|
||
token_type: string;
|
||
expires_in: number;
|
||
issued_time: string; // 🔑 后端返回的签发时间,格式:"2025-11-18 17:41:06"
|
||
user_info: {
|
||
user_id?: string;
|
||
username: string;
|
||
nick_name: string;
|
||
email?: string;
|
||
phone_number?: string;
|
||
ou_id: string;
|
||
ou_name: string;
|
||
is_leader: boolean;
|
||
user_role: string;
|
||
sub: string;
|
||
area?: string; // 🔑 用户所属地区
|
||
// 🔑 组织信息字段(可能为null)
|
||
tenant_name?: string | null;
|
||
dep_name?: string | null;
|
||
dep_short_name?: string | null;
|
||
};
|
||
};
|
||
error?: string;
|
||
message?: string;
|
||
}
|
||
|
||
/**
|
||
* 调用后端登录接口(OAuth 方式)
|
||
*
|
||
* @param loginData 登录数据(OAuth 用户信息)
|
||
* @returns 登录响应(包含 JWT token)
|
||
*/
|
||
export async function loginWithOAuth(loginData: LoginRequest): Promise<LoginResponse> {
|
||
const loginUrl = `${API_BASE_URL}/api/auth/login`;
|
||
|
||
console.log("📝 [Login Client] 调用后端 OAuth 登录接口:", loginUrl);
|
||
|
||
try {
|
||
const response = await axios.post(loginUrl, loginData, {
|
||
headers: {
|
||
"Content-Type": "application/json",
|
||
"Accept": "application/json"
|
||
}
|
||
});
|
||
|
||
console.log("✅ [Login Client] OAuth 登录成功");
|
||
return response.data;
|
||
} catch (error) {
|
||
if (axios.isAxiosError(error)) {
|
||
const errorData = error.response?.data || {};
|
||
console.error("❌ [Login Client] OAuth 登录请求失败:", error.response?.status, errorData);
|
||
|
||
return {
|
||
success: false,
|
||
error: errorData.error || errorData.message || `登录失败: ${error.response?.status || 'Unknown'}`
|
||
};
|
||
}
|
||
|
||
console.error("❌ [Login Client] OAuth 登录请求异常:", error);
|
||
return {
|
||
success: false,
|
||
error: error instanceof Error ? error.message : "网络请求失败"
|
||
};
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 密码登录请求参数
|
||
*/
|
||
export interface PasswordLoginRequest {
|
||
username: string;
|
||
password: string;
|
||
}
|
||
|
||
/**
|
||
* 调用后端登录接口(密码方式)
|
||
*
|
||
* @param username 用户名
|
||
* @param password 密码
|
||
* @returns 登录响应(包含 JWT token)
|
||
*/
|
||
export async function loginWithPassword(
|
||
username: string,
|
||
password: string
|
||
): Promise<LoginResponse> {
|
||
const loginUrl = `${API_BASE_URL}/api/auth/login`;
|
||
|
||
console.log("📝 [Login Client] 调用后端密码登录接口:", loginUrl);
|
||
|
||
try {
|
||
const response = await axios.post(loginUrl, {
|
||
username,
|
||
password
|
||
}, {
|
||
headers: {
|
||
"Content-Type": "application/json",
|
||
"Accept": "application/json"
|
||
}
|
||
});
|
||
|
||
console.log("✅ [Login Client] 密码登录成功");
|
||
return response.data;
|
||
} catch (error) {
|
||
if (axios.isAxiosError(error)) {
|
||
const errorData = error.response?.data || {};
|
||
console.error("❌ [Login Client] 密码登录请求失败:", error.response?.status, errorData);
|
||
|
||
return {
|
||
success: false,
|
||
error: errorData.error || errorData.message || `登录失败: ${error.response?.status || 'Unknown'}`
|
||
};
|
||
}
|
||
|
||
console.error("❌ [Login Client] 密码登录请求异常:", error);
|
||
return {
|
||
success: false,
|
||
error: error instanceof Error ? error.message : "网络请求失败"
|
||
};
|
||
}
|
||
}
|