import { ref } from 'vue'; import { conversationBoardAgentList } from './api-conversation-board'; import { useAllAgentStore } from '../../base/store/use-all-agents'; /** * 获取Agent list * 用于会话历史记录类别筛选 * @param {function} onSuccess * @returns */ export default function useAgentList4History(onSuccess) { // api请求的状态 const api_status = ref(1); // api状态 1 请求未启动 2 请求进心中 3 请求已成功 4 请求失败(网络成功,但是code不等于200) 5 请求出错 const api_message = ref(''); // 请求消息 与 api_status 对应的 message 消息内容 // 历史记录列表数据 const list_agents = ref([]); // 历史记录的列表数据 const allAgents = useAllAgentStore(); /** * 处理请求参数 */ const formatListOptions = () => { return {}; }; /** * 调用api请求 */ const handleApiRequest = () => { // TODO: 从本地store中获取,而非api // 否则每次进入此页面,都需要调用接口 const cache_agents = allAgents.agents; if (cache_agents.length !== 0) { if (typeof onSuccess === 'function') { onSuccess(cache_agents[0]); } list_agents.value = cache_agents; return; } // 处理查询参数 const opts = formatListOptions(); // 进入加载状态 api_status.value = 2; api_message.value = ''; conversationBoardAgentList(opts) .then((res) => { if (res.code / 1 === 200) { // 成功 const res_list = res.data; // 历史记录列表 if (res_list.length !== 0) { if (typeof onSuccess === 'function') { onSuccess(res_list); } } list_agents.value = res_list; // 在store中缓存数据 allAgents.setList(res_list); // api请求成功 api_status.value = 3; api_message.value = 'success'; } else { // api请求成功,但是业务失败 api_status.value = 4; api_message.value = err.message; } }) .catch((err) => { console.log(err); // api请求出错 api_status.value = 5; api_message.value = err.msg; }); }; return { getAgentList: handleApiRequest, api_status, api_message, list_agents }; }