2025-10-21 11:40:47 +08:00
|
|
|
import { useUserData } from "./useUserData";
|
2025-10-22 16:32:49 +08:00
|
|
|
import { useEffect, useState, useCallback } from "react";
|
2025-10-21 11:40:47 +08:00
|
|
|
import logger from "@/utils/logger";
|
2025-10-22 16:32:49 +08:00
|
|
|
import type { IUserInfo, ISystemStatus } from "@/interfaces/database/user-setting";
|
2025-10-21 11:40:47 +08:00
|
|
|
import userService from "@/services/user_service";
|
|
|
|
|
import { rsaPsw } from "../utils/encryption";
|
2025-10-21 16:41:22 +08:00
|
|
|
import type { IFactory, IMyLlmModel } from "@/interfaces/database/llm";
|
|
|
|
|
import type { LLMFactory } from "@/constants/llm";
|
2025-10-21 11:40:47 +08:00
|
|
|
|
2025-10-21 16:41:22 +08:00
|
|
|
/**
|
|
|
|
|
* 个人中心设置
|
|
|
|
|
*/
|
2025-10-21 11:40:47 +08:00
|
|
|
export function useProfileSetting() {
|
2025-10-22 15:27:31 +08:00
|
|
|
const { fetchUserInfo, userInfo } = useUserData();
|
2025-10-21 11:40:47 +08:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
fetchUserInfo();
|
|
|
|
|
}, [fetchUserInfo]);
|
|
|
|
|
|
|
|
|
|
const updateUserInfo = async (newUserInfo: Partial<IUserInfo>) => {
|
|
|
|
|
try {
|
|
|
|
|
await userService.updateSetting(newUserInfo);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error('更新用户信息失败:', error);
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const changeUserPassword = async (data: { password: string; new_password: string }) => {
|
|
|
|
|
try {
|
|
|
|
|
const newPassword = rsaPsw(data.new_password);
|
|
|
|
|
const oldPassword = rsaPsw(data.password);
|
|
|
|
|
const res = await userService.updatePassword({
|
|
|
|
|
password: oldPassword,
|
|
|
|
|
new_password: newPassword,
|
|
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw error;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
userInfo,
|
|
|
|
|
updateUserInfo,
|
|
|
|
|
changeUserPassword,
|
|
|
|
|
};
|
2025-10-21 16:41:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* LLM 模型设置
|
|
|
|
|
*/
|
|
|
|
|
export function useLlmModelSetting() {
|
|
|
|
|
const [llmFactory, setLlmFactory] = useState<IFactory[]>([]);
|
|
|
|
|
const [myLlm, setMyLlm] = useState<Record<LLMFactory, IMyLlmModel>>();
|
|
|
|
|
|
2025-10-22 15:27:31 +08:00
|
|
|
const fetchLlmFactory = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const res = await userService.llm_factories_list();
|
|
|
|
|
const arr = res.data.data || [];
|
|
|
|
|
setLlmFactory(arr);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error('获取模型工厂失败:', error);
|
|
|
|
|
throw error;
|
2025-10-21 16:41:22 +08:00
|
|
|
}
|
2025-10-22 15:27:31 +08:00
|
|
|
}
|
2025-10-21 16:41:22 +08:00
|
|
|
|
2025-10-22 15:27:31 +08:00
|
|
|
const fetchMyLlm = async () => {
|
|
|
|
|
try {
|
|
|
|
|
const res = await userService.my_llm();
|
|
|
|
|
const llm_dic = res.data.data || {};
|
|
|
|
|
setMyLlm(llm_dic);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error('获取我的模型失败:', error);
|
|
|
|
|
throw error;
|
2025-10-21 16:41:22 +08:00
|
|
|
}
|
2025-10-22 15:27:31 +08:00
|
|
|
}
|
|
|
|
|
useEffect(() => {
|
2025-10-21 16:41:22 +08:00
|
|
|
fetchLlmFactory();
|
|
|
|
|
fetchMyLlm();
|
2025-10-22 15:27:31 +08:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const refreshLlmModel = async () => {
|
|
|
|
|
await fetchMyLlm();
|
|
|
|
|
// await fetchLlmFactory();
|
|
|
|
|
logger.info('刷新我的模型成功');
|
|
|
|
|
}
|
2025-10-21 16:41:22 +08:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
llmFactory,
|
|
|
|
|
myLlm,
|
2025-10-22 15:27:31 +08:00
|
|
|
refreshLlmModel,
|
2025-10-21 16:41:22 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-22 16:32:49 +08:00
|
|
|
/**
|
|
|
|
|
* 系统状态设置
|
|
|
|
|
*/
|
|
|
|
|
export function useSystemStatus() {
|
|
|
|
|
const [systemStatus, setSystemStatus] = useState<ISystemStatus | null>(null);
|
|
|
|
|
const [loading, setLoading] = useState(false);
|
|
|
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
|
|
|
|
|
|
const fetchSystemStatus = useCallback(async () => {
|
|
|
|
|
try {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
setError(null);
|
|
|
|
|
const res = await userService.system_status();
|
|
|
|
|
if (res.data.code === 0) {
|
|
|
|
|
setSystemStatus(res.data.data);
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(res.data.message || '获取系统状态失败');
|
|
|
|
|
}
|
|
|
|
|
} catch (error: any) {
|
|
|
|
|
const errorMessage = error.response?.data?.message || error.message || '获取系统状态失败';
|
|
|
|
|
setError(errorMessage);
|
|
|
|
|
logger.error('获取系统状态失败:', error);
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
systemStatus,
|
|
|
|
|
loading,
|
|
|
|
|
error,
|
|
|
|
|
fetchSystemStatus,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|