Files
chaos_it/src/lib/api/services/authService.ts
Chaos 50a3022e9d refactor(api): 重构API客户端以支持依赖注入
- 移除全局api实例,改用createApi工厂函数创建客户端
- 在服务层函数中添加api参数,实现依赖注入
- 更新设备、角色、用户等服务调用方式
- 移除请求头中的Authorization字段手动设置
- 在hooks.server.ts中初始化并挂载api到locals
- 修复HttpError类定义位置并完善错误处理逻辑
- 调整页面组件中main容器和表格布局样式
- 更新tailwindcss主题配置和相关CSS类名
- 修改分页大小默认值从10到12
- 删除冗余的COOKIE_TOKEN_KEY导入和重定向逻辑
2025-12-03 07:11:09 +08:00

31 lines
691 B
TypeScript

import type { AuthResponse, LoginPayload } from '$lib/types/auth';
import { ApiError } from '$lib/types/api.ts';
import type { ApiClient } from '$lib/api/httpClient.ts';
export const authService = {
/**
* 登录流程
*/
login: async (api: ApiClient,payload: LoginPayload): Promise<AuthResponse> => {
const response = await api.post<AuthResponse>('/auth/login', payload);
if (response.code !== 200 || !response.data) {
throw new ApiError(response);
}
return response.data;
},
/**
* 登出流程
*/
logout: async (api: ApiClient) => {
try {
await api.post('/auth/logout', {});
} catch (error) {
console.warn('Logout API call failed:', error);
}
}
};