File size: 7,541 Bytes
f305776 7b71fb2 88e5a61 e4e6a45 04aba1b 7b71fb2 362ec6c 6b8fc2c f305776 fad2ec7 7b71fb2 f305776 7b71fb2 6a6f6eb fad2ec7 362ec6c 6b8fc2c fad2ec7 04aba1b f305776 7b71fb2 f305776 6a6f6eb 6b8fc2c 362ec6c 0cf2c67 7b71fb2 f305776 6a6f6eb 362ec6c 6b8fc2c f3d0ebd 6b8fc2c 88e5a61 6b8fc2c f3d0ebd 6b8fc2c 88e5a61 6b8fc2c f3d0ebd 6b8fc2c f305776 362ec6c 6b8fc2c f305776 04aba1b 6b8fc2c f305776 362ec6c f3d0ebd 04aba1b 362ec6c 04aba1b f3d0ebd 6b8fc2c 88e5a61 0cf2c67 362ec6c 6b8fc2c 362ec6c f3d0ebd 362ec6c f3d0ebd 6b8fc2c 88e5a61 f305776 362ec6c 6b8fc2c f305776 6b8fc2c 7b71fb2 f3d0ebd 7b71fb2 88e5a61 0cf2c67 7b71fb2 362ec6c eb38196 f3d0ebd 6b8fc2c 0cf2c67 c22c6b5 0cf2c67 88e5a61 6b8fc2c 362ec6c 6b8fc2c eb38196 f3d0ebd eb38196 f3d0ebd 88e5a61 eb38196 362ec6c c22c6b5 f3d0ebd 6b8fc2c 0cf2c67 c22c6b5 0cf2c67 88e5a61 6b8fc2c 362ec6c 04aba1b 6a6f6eb e4e6a45 71b7e06 04aba1b 7c4aa10 4f62080 7c4aa10 6b8fc2c 362ec6c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
import { BaseState } from '@/interfaces/common';
import { IKnowledgeFile } from '@/interfaces/database/knowledge';
import i18n from '@/locales/config';
import kbService, { getDocumentFile } from '@/services/kbService';
import { message } from 'antd';
import omit from 'lodash/omit';
import pick from 'lodash/pick';
import { DvaModel } from 'umi';
export interface KFModelState extends BaseState {
tenantIfo: any;
data: IKnowledgeFile[];
total: number;
currentRecord: Nullable<IKnowledgeFile>;
fileThumbnails: Record<string, string>;
}
const model: DvaModel<KFModelState> = {
namespace: 'kFModel',
state: {
tenantIfo: {},
data: [],
total: 0,
currentRecord: null,
searchString: '',
pagination: {
current: 1,
pageSize: 10,
},
fileThumbnails: {} as Record<string, string>,
},
reducers: {
updateState(state, { payload }) {
return {
...state,
...payload,
};
},
setCurrentRecord(state, { payload }) {
return { ...state, currentRecord: payload };
},
setSearchString(state, { payload }) {
return { ...state, searchString: payload };
},
setPagination(state, { payload }) {
return { ...state, pagination: { ...state.pagination, ...payload } };
},
setFileThumbnails(state, { payload }) {
return { ...state, fileThumbnails: payload };
},
},
effects: {
*createKf({ payload = {} }, { call }) {
const { data } = yield call(kbService.createKb, payload);
const { retcode } = data;
if (retcode === 0) {
message.success(i18n.t('message.created'));
}
},
*updateKf({ payload = {} }, { call }) {
const { data } = yield call(kbService.updateKb, payload);
const { retcode } = data;
if (retcode === 0) {
message.success(i18n.t('message.modified'));
}
},
*getKfDetail({ payload = {} }, { call }) {
const { data } = yield call(kbService.get_kb_detail, payload);
},
*getKfList({ payload = {} }, { call, put, select }) {
const state: KFModelState = yield select((state: any) => state.kFModel);
const requestBody = {
...payload,
page: state.pagination.current,
page_size: state.pagination.pageSize,
};
if (state.searchString) {
requestBody['keywords'] = state.searchString;
}
const { data } = yield call(kbService.get_document_list, requestBody);
const { retcode, data: res } = data;
if (retcode === 0) {
yield put({
type: 'updateState',
payload: {
data: res.docs,
total: res.total,
},
});
}
},
throttledGetDocumentList: [
function* ({ payload }, { call, put }) {
yield put({ type: 'getKfList', payload: { kb_id: payload } });
},
{ type: 'throttle', ms: 1000 }, // TODO: Provide type support for this effect
],
pollGetDocumentList: [
function* ({ payload }, { call, put }) {
yield put({ type: 'getKfList', payload: { kb_id: payload } });
},
{ type: 'poll', delay: 5000 }, // TODO: Provide type support for this effect
],
*updateDocumentStatus({ payload = {} }, { call, put }) {
const { data } = yield call(
kbService.document_change_status,
pick(payload, ['doc_id', 'status']),
);
const { retcode } = data;
if (retcode === 0) {
message.success(i18n.t('message.modified'));
yield put({
type: 'getKfList',
payload: { kb_id: payload.kb_id },
});
}
},
*document_rm({ payload = {} }, { call, put }) {
const { data } = yield call(kbService.document_rm, {
doc_id: payload.doc_id,
});
const { retcode } = data;
if (retcode === 0) {
message.success(i18n.t('message.deleted'));
yield put({
type: 'getKfList',
payload: { kb_id: payload.kb_id },
});
}
return retcode;
},
*document_rename({ payload = {} }, { call, put }) {
const { data } = yield call(
kbService.document_rename,
omit(payload, ['kb_id']),
);
const { retcode } = data;
if (retcode === 0) {
message.success(i18n.t('message.renamed'));
yield put({
type: 'getKfList',
payload: { kb_id: payload.kb_id },
});
}
return retcode;
},
*document_create({ payload = {} }, { call, put }) {
const { data } = yield call(kbService.document_create, payload);
const { retcode } = data;
if (retcode === 0) {
yield put({
type: 'getKfList',
payload: { kb_id: payload.kb_id },
});
message.success(i18n.t('message.created'));
}
return retcode;
},
*document_run({ payload = {} }, { call, put }) {
const { data } = yield call(
kbService.document_run,
omit(payload, ['knowledgeBaseId']),
);
const { retcode } = data;
if (retcode === 0) {
if (payload.knowledgeBaseId) {
yield put({
type: 'getKfList',
payload: { kb_id: payload.knowledgeBaseId },
});
}
message.success(i18n.t('message.operated'));
}
return retcode;
},
*document_change_parser({ payload = {} }, { call, put }) {
const { data } = yield call(
kbService.document_change_parser,
omit(payload, ['kb_id']),
);
const { retcode } = data;
if (retcode === 0) {
yield put({
type: 'getKfList',
payload: { kb_id: payload.kb_id },
});
message.success(i18n.t('message.modified'));
}
return retcode;
},
*fetch_document_thumbnails({ payload = {} }, { call, put }) {
const { data } = yield call(kbService.document_thumbnails, payload);
if (data.retcode === 0) {
yield put({ type: 'setFileThumbnails', payload: data.data });
}
},
*fetch_document_file({ payload = {} }, { call }) {
const documentId = payload;
try {
const ret = yield call(getDocumentFile, documentId);
return ret;
} catch (error) {
console.warn(error);
}
},
*upload_document({ payload = {} }, { call, put }) {
const formData = new FormData();
formData.append('file', payload.file);
formData.append('kb_id', payload.kb_id);
const { data } = yield call(kbService.document_upload, formData);
if (data.retcode === 0) {
yield put({
type: 'getKfList',
payload: { kb_id: payload.kb_id },
});
}
return data;
},
},
subscriptions: {
setup({ dispatch, history }) {
history.listen(({ location }) => {
const state: { from: string } = (location.state ?? {
from: '',
}) as { from: string };
if (
state.from === '/knowledge' || // TODO: Just directly determine whether the current page is on the knowledge list page.
location.pathname === '/knowledge/dataset/upload'
) {
dispatch({
type: 'setPagination',
payload: { current: 1, pageSize: 10 },
});
}
});
},
},
};
export default model;
|