117 lines
3.1 KiB
TypeScript
117 lines
3.1 KiB
TypeScript
import { useCallback, useEffect, useState } from 'react';
|
|
|
|
export interface LlmItem {
|
|
id: string;
|
|
name: string;
|
|
}
|
|
|
|
export interface CurrentUser {
|
|
id: string;
|
|
email: string;
|
|
is_active: boolean;
|
|
is_superuser: boolean;
|
|
is_verified: boolean;
|
|
pages_limit: number;
|
|
pages_used: number;
|
|
}
|
|
|
|
type RequestInfoLike = Parameters<typeof fetch>[0];
|
|
type RequestInitLike = Parameters<typeof fetch>[1];
|
|
|
|
export const knowledgetoolUrl = import.meta.env.VITE_AIPI_BACKEND_URL || '';
|
|
const LOGIN_URL = `${knowledgetoolUrl}/auth/jwt/login`;
|
|
const LLMS_URL = `${knowledgetoolUrl}/api/v1/expert-agents/llms`;
|
|
const ME_URL = `${knowledgetoolUrl}/users/me`;
|
|
|
|
export function useExpertAuthBootstrap() {
|
|
const [llmList, setLlmList] = useState<LlmItem[]>([]);
|
|
const [currentUser, setCurrentUser] = useState<CurrentUser | null>(null);
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
const authedFetch = useCallback(async (input: RequestInfoLike, init?: RequestInitLike) => {
|
|
const token = localStorage.getItem('tools_token');
|
|
const headers = new Headers(init?.headers);
|
|
if (token) {
|
|
headers.set('Authorization', `Bearer ${token}`);
|
|
}
|
|
return fetch(input, { ...init, headers });
|
|
}, []);
|
|
|
|
const login = useCallback(async () => {
|
|
const formData = new FormData();
|
|
formData.append('username', 'liuqiang@qq.com');
|
|
formData.append('password', '123456');
|
|
formData.append('grant_type', 'password');
|
|
|
|
const res = await fetch(LOGIN_URL, {
|
|
method: 'POST',
|
|
body: formData,
|
|
});
|
|
|
|
if (!res.ok) {
|
|
throw new Error(`登录失败: ${res.status} ${res.statusText}`);
|
|
}
|
|
|
|
const data = await res.json();
|
|
if (data?.access_token) {
|
|
localStorage.setItem('tools_token', data.access_token);
|
|
}
|
|
}, []);
|
|
|
|
const fetchLlmList = useCallback(async () => {
|
|
const res = await authedFetch(LLMS_URL, { method: 'GET' });
|
|
if (!res.ok) {
|
|
throw new Error(`llms 接口失败: ${res.status} ${res.statusText}`);
|
|
}
|
|
|
|
const data: unknown = await res.json();
|
|
const list = Array.isArray(data) ? (data as LlmItem[]) : [];
|
|
setLlmList(list);
|
|
return list;
|
|
}, [authedFetch]);
|
|
|
|
const fetchCurrentUser = useCallback(async () => {
|
|
const res = await authedFetch(ME_URL, { method: 'GET' });
|
|
if (!res.ok) {
|
|
// await login();
|
|
throw new Error(`users/me 接口失败: ${res.status} ${res.statusText}`);
|
|
}
|
|
const data: unknown = await res.json();
|
|
const user = (data as CurrentUser) ?? null;
|
|
setCurrentUser(user);
|
|
return user;
|
|
}, [authedFetch]);
|
|
|
|
const bootstrap = useCallback(async () => {
|
|
setLoading(true);
|
|
try {
|
|
if (!localStorage.getItem('tools_token')) {
|
|
await login();
|
|
}
|
|
await fetchCurrentUser();
|
|
} catch(error) {
|
|
console.log(error);
|
|
} finally {
|
|
await Promise.all([fetchLlmList()]);
|
|
setLoading(false);
|
|
}
|
|
}, [fetchCurrentUser, fetchLlmList, login]);
|
|
|
|
useEffect(() => {
|
|
void bootstrap().catch((error) => {
|
|
console.error('专家中心初始化失败:', error);
|
|
});
|
|
}, [bootstrap]);
|
|
|
|
return {
|
|
llmList,
|
|
currentUser,
|
|
loading,
|
|
authedFetch,
|
|
login,
|
|
fetchLlmList,
|
|
fetchCurrentUser,
|
|
bootstrap,
|
|
};
|
|
}
|