| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- import { makeAutoObservable } from 'mobx';
- import { message } from 'antd';
- import { apis } from '@/apis';
- import { State, ReadonlyState, StateAction, KnowledgeLibListStore } from './types';
- // 定义状态
- const stateGenerator = (): ReadonlyState => ({
- listLoading: false,
- list: [],
- page: {
- pageNumber: 1,
- pageSize: 10,
- total: 0,
- },
- });
- // 修改状态
- const stateActionsGenerator = (state: State): StateAction => {
- return {
- setListLoading: (loading) => {
- state.listLoading = loading;
- },
- setList: (list) => {
- state.list = list;
- },
- setPage: (page) => {
- state.page = page;
- },
- };
- };
- // 使用仓库
- const useKnowledgeLibListStore = (): KnowledgeLibListStore => {
- const state = makeAutoObservable(stateGenerator());
- const actions = stateActionsGenerator(state);
- const api = {
- // 获取知识库列表
- fetchKnowledgeLibList: async () => {
- actions.setListLoading(true);
- try {
- const data = {
- pageNumber: state.page.pageNumber,
- pageSize: state.page.pageSize,
- };
- const res = await apis.fetchKnowledgeLibList(data);
- actions.setList(res.data.list);
- actions.setPage({
- ...state.page,
- total: res.data.total,
- });
- } catch (error: any) {
- console.error(error);
- } finally {
- actions.setListLoading(false);
- }
- },
- // 删除知识库
- deleteKnowledgeLib: async (knowledgeId: string) => {
- try {
- await apis.deleteKnowledgeLib(knowledgeId);
- // 获取知识库列表
- await api.fetchKnowledgeLibList();
- message.success('删除成功');
- } catch (error: any) {
- message.error(error.msg);
- }
- },
- }
- // 更改分页
- const onChangePagination: KnowledgeLibListStore['onChangePagination'] = async (pageNumber, pageSize) => {
- actions.setPage({
- ...state.page,
- pageNumber: pageNumber,
- pageSize: pageSize,
- });
- // 获取知识库列表
- await api.fetchKnowledgeLibList();
- }
- // 点击删除
- const onClickDelete: KnowledgeLibListStore['onClickDelete'] = async (knowledgeId) => {
- // 删除知识库
- await api.deleteKnowledgeLib(knowledgeId);
- }
- // 初始渲染
- const init: KnowledgeLibListStore['init'] = async () => {
- // 获取知识库列表
- await api.fetchKnowledgeLibList();
- }
- // 状态重置
- const reset: KnowledgeLibListStore['reset'] = () => {
- const initialListLoading = stateGenerator().listLoading;
- const initialList = stateGenerator().list;
- const initialPage = stateGenerator().page;
- actions.setListLoading(initialListLoading);
- actions.setList(initialList);
- actions.setPage(initialPage);
- }
- return {
- state,
- onChangePagination,
- onClickDelete,
- init,
- reset
- };
- };
- export default useKnowledgeLibListStore();
|