use-agent-list-4-history.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { ref } from 'vue';
  2. import { conversationBoardAgentList } from './api-conversation-board';
  3. import { useAllAgentStore } from '../../base/store/use-all-agents';
  4. /**
  5. * 获取Agent list
  6. * 用于会话历史记录类别筛选
  7. * @param {function} onSuccess
  8. * @returns
  9. */
  10. export default function useAgentList4History(onSuccess) {
  11. // api请求的状态
  12. const api_status = ref(1); // api状态 1 请求未启动 2 请求进心中 3 请求已成功 4 请求失败(网络成功,但是code不等于200) 5 请求出错
  13. const api_message = ref(''); // 请求消息 与 api_status 对应的 message 消息内容
  14. // 历史记录列表数据
  15. const list_agents = ref([]); // 历史记录的列表数据
  16. const allAgents = useAllAgentStore();
  17. /**
  18. * 处理请求参数
  19. */
  20. const formatListOptions = () => {
  21. return {};
  22. };
  23. /**
  24. * 调用api请求
  25. */
  26. const handleApiRequest = () => {
  27. // TODO: 从本地store中获取,而非api
  28. // 否则每次进入此页面,都需要调用接口
  29. const cache_agents = allAgents.agents;
  30. if (cache_agents.length !== 0) {
  31. if (typeof onSuccess === 'function') {
  32. onSuccess(cache_agents[0]);
  33. }
  34. list_agents.value = cache_agents;
  35. return;
  36. }
  37. // 处理查询参数
  38. const opts = formatListOptions();
  39. // 进入加载状态
  40. api_status.value = 2;
  41. api_message.value = '';
  42. conversationBoardAgentList(opts)
  43. .then((res) => {
  44. if (res.code / 1 === 200) {
  45. // 成功
  46. const res_list = res.data; // 历史记录列表
  47. if (res_list.length !== 0) {
  48. if (typeof onSuccess === 'function') {
  49. onSuccess(res_list);
  50. }
  51. }
  52. list_agents.value = res_list;
  53. // 在store中缓存数据
  54. allAgents.setList(res_list);
  55. // api请求成功
  56. api_status.value = 3;
  57. api_message.value = 'success';
  58. } else {
  59. // api请求成功,但是业务失败
  60. api_status.value = 4;
  61. api_message.value = err.message;
  62. }
  63. })
  64. .catch((err) => {
  65. console.log(err);
  66. // api请求出错
  67. api_status.value = 5;
  68. api_message.value = err.msg;
  69. });
  70. };
  71. return {
  72. getAgentList: handleApiRequest,
  73. api_status,
  74. api_message,
  75. list_agents
  76. };
  77. }