dev 分支暂存

This commit is contained in:
2026-05-16 08:26:56 +08:00
parent 58c8caa570
commit eb4129176c
71 changed files with 8474 additions and 214 deletions
+10
View File
@@ -0,0 +1,10 @@
// 认证API(重新导出client中的认证函数)
export {
login,
register,
refreshToken,
setToken,
getToken,
clearToken,
isAuthenticated,
} from './client';
+154
View File
@@ -0,0 +1,154 @@
// HTTP 客户端封装
import type { AuthResponse } from '@/types/session';
const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:8080/api/v1';
/** 请求选项 */
interface RequestOptions {
method?: string;
body?: unknown;
headers?: Record<string, string>;
auth?: boolean;
}
/** API 响应格式 */
interface ApiResponse<T = unknown> {
data?: T;
error?: string;
status: number;
}
/**
* 发送API请求
*/
async function request<T = unknown>(endpoint: string, options: RequestOptions = {}): Promise<ApiResponse<T>> {
const { method = 'GET', body, auth = true } = options;
const headers: Record<string, string> = {
'Content-Type': 'application/json',
...options.headers,
};
if (auth) {
const token = localStorage.getItem('token');
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
}
try {
const response = await fetch(`${API_BASE_URL}${endpoint}`, {
method,
headers,
body: body ? JSON.stringify(body) : undefined,
});
const data = await response.json().catch(() => null);
if (!response.ok) {
return {
error: data?.error || `请求失败 (${response.status})`,
status: response.status,
};
}
return { data: data as T, status: response.status };
} catch (err) {
return {
error: err instanceof Error ? err.message : '网络错误',
status: 0,
};
}
}
/** 存储认证令牌 */
export function setToken(token: string) {
localStorage.setItem('token', token);
}
/** 获取认证令牌 */
export function getToken(): string | null {
return localStorage.getItem('token');
}
/** 清除认证令牌 */
export function clearToken() {
localStorage.removeItem('token');
localStorage.removeItem('user_id');
}
/** 检查是否已认证 */
export function isAuthenticated(): boolean {
return !!getToken();
}
// ========== 认证API ==========
export async function login(username: string, password: string): Promise<ApiResponse<AuthResponse>> {
const resp = await request<AuthResponse>('/auth/login', {
method: 'POST',
body: { username, password },
auth: false,
});
if (resp.data?.token) {
setToken(resp.data.token);
localStorage.setItem('user_id', resp.data.user_id);
}
return resp;
}
export async function register(username: string, password: string): Promise<ApiResponse<AuthResponse>> {
const resp = await request<AuthResponse>('/auth/register', {
method: 'POST',
body: { username, password },
auth: false,
});
if (resp.data?.token) {
setToken(resp.data.token);
localStorage.setItem('user_id', resp.data.user_id);
}
return resp;
}
export async function refreshToken(): Promise<ApiResponse<AuthResponse>> {
const resp = await request<AuthResponse>('/auth/refresh', { method: 'POST' });
if (resp.data?.token) {
setToken(resp.data.token);
}
return resp;
}
// ========== 会话API ==========
export async function createSession(title?: string) {
return request('/sessions', { method: 'POST', body: { title } });
}
export async function listSessions() {
return request('/sessions');
}
export async function getSession(id: string) {
return request(`/sessions/${id}`);
}
export async function deleteSession(id: string) {
return request(`/sessions/${id}`, { method: 'DELETE' });
}
// ========== 记忆API ==========
export async function searchMemory(query: string) {
return request(`/memory/search?q=${encodeURIComponent(query)}`);
}
export async function listMemories() {
return request('/memory');
}
export async function addMemory(content: string, category?: string, priority?: number) {
return request('/memory', { method: 'POST', body: { content, category, priority } });
}
export { request, type ApiResponse };
+6
View File
@@ -0,0 +1,6 @@
// 记忆API
export {
searchMemory,
listMemories,
addMemory,
} from './client';
+7
View File
@@ -0,0 +1,7 @@
// 会话API
export {
createSession,
listSessions,
getSession,
deleteSession,
} from './client';