store.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { makeAutoObservable } from 'mobx';
  2. import { message } from 'antd';
  3. import { apis } from '@/apis';
  4. import { State, ReadonlyState, StateAction, KnowledgeLibListStore } from './types';
  5. // 定义状态
  6. const stateGenerator = (): ReadonlyState => ({
  7. listLoading: false,
  8. list: [],
  9. page: {
  10. pageNumber: 1,
  11. pageSize: 10,
  12. total: 0,
  13. },
  14. });
  15. // 修改状态
  16. const stateActionsGenerator = (state: State): StateAction => {
  17. return {
  18. setListLoading: (loading) => {
  19. state.listLoading = loading;
  20. },
  21. setList: (list) => {
  22. state.list = list;
  23. },
  24. setPage: (page) => {
  25. state.page = page;
  26. },
  27. };
  28. };
  29. // 使用仓库
  30. const useKnowledgeLibListStore = (): KnowledgeLibListStore => {
  31. const state = makeAutoObservable(stateGenerator());
  32. const actions = stateActionsGenerator(state);
  33. const api = {
  34. // 获取知识库列表
  35. fetchKnowledgeLibList: async () => {
  36. actions.setListLoading(true);
  37. try {
  38. const data = {
  39. pageNumber: state.page.pageNumber,
  40. pageSize: state.page.pageSize,
  41. };
  42. const res = await apis.fetchKnowledgeLibList(data);
  43. actions.setList(res.data.list);
  44. actions.setPage({
  45. ...state.page,
  46. total: res.data.total,
  47. });
  48. } catch (error: any) {
  49. console.error(error);
  50. } finally {
  51. actions.setListLoading(false);
  52. }
  53. },
  54. // 删除知识库
  55. deleteKnowledgeLib: async (knowledgeId: string) => {
  56. try {
  57. await apis.deleteKnowledgeLib(knowledgeId);
  58. // 获取知识库列表
  59. await api.fetchKnowledgeLibList();
  60. message.success('删除成功');
  61. } catch (error: any) {
  62. message.error(error.msg);
  63. }
  64. },
  65. }
  66. // 更改分页
  67. const onChangePagination: KnowledgeLibListStore['onChangePagination'] = async (pageNumber, pageSize) => {
  68. actions.setPage({
  69. ...state.page,
  70. pageNumber: pageNumber,
  71. pageSize: pageSize,
  72. });
  73. // 获取知识库列表
  74. await api.fetchKnowledgeLibList();
  75. }
  76. // 点击删除
  77. const onClickDelete: KnowledgeLibListStore['onClickDelete'] = async (knowledgeId) => {
  78. // 删除知识库
  79. await api.deleteKnowledgeLib(knowledgeId);
  80. }
  81. // 初始渲染
  82. const init: KnowledgeLibListStore['init'] = async () => {
  83. // 获取知识库列表
  84. await api.fetchKnowledgeLibList();
  85. }
  86. // 状态重置
  87. const reset: KnowledgeLibListStore['reset'] = () => {
  88. const initialListLoading = stateGenerator().listLoading;
  89. const initialList = stateGenerator().list;
  90. const initialPage = stateGenerator().page;
  91. actions.setListLoading(initialListLoading);
  92. actions.setList(initialList);
  93. actions.setPage(initialPage);
  94. }
  95. return {
  96. state,
  97. onChangePagination,
  98. onClickDelete,
  99. init,
  100. reset
  101. };
  102. };
  103. export default useKnowledgeLibListStore();