refactor(api): 重构API客户端以支持依赖注入

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

View File

@@ -1,14 +1,14 @@
import { api } from '$lib/api/httpClient'; // 通常不需要 .ts 后缀
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 (payload: LoginPayload): Promise<AuthResponse> => {
// 1. 调用登录接口
login: async (api: ApiClient,payload: LoginPayload): Promise<AuthResponse> => {
const response = await api.post<AuthResponse>('/auth/login', payload);
if (response.code !== 200 || !response.data) {
@@ -21,12 +21,10 @@ export const authService = {
/**
* 登出流程
*/
logout: async () => {
logout: async (api: ApiClient) => {
try {
// Optionally call the backend logout endpoint
await api.post('/auth/logout', {});
} catch (error) {
// Even if the backend call fails, we still want to clear local state
console.warn('Logout API call failed:', error);
}
}