|
|
@@ -2,12 +2,14 @@ import { action, makeAutoObservable } from 'mobx';
|
|
|
import { message } from 'antd';
|
|
|
import { apis, ModifyDocumentApiParams, ModifyDocumentSettingApiParams } from '@/apis';
|
|
|
import { State, ReadonlyState, StateAction, DocumentLibInfoStore } from './types';
|
|
|
+import LocalStorage from '@/LocalStorage';
|
|
|
|
|
|
// 定义状态
|
|
|
const stateGenerator = (): ReadonlyState => ({
|
|
|
knowledge_id: '',
|
|
|
listLoading: false,
|
|
|
list: [],
|
|
|
+ processingList: [],
|
|
|
infoModalId: '',
|
|
|
infoModalOpen: false,
|
|
|
infoModalSettingId: '',
|
|
|
@@ -36,6 +38,9 @@ const stateActionsGenerator = (state: State): StateAction => {
|
|
|
setList: (list) => {
|
|
|
state.list = list;
|
|
|
},
|
|
|
+ setProcessingList: (list) => {
|
|
|
+ state.processingList = list;
|
|
|
+ },
|
|
|
setInfoModalId: (id) => {
|
|
|
state.infoModalId = id;
|
|
|
},
|
|
|
@@ -64,20 +69,30 @@ const useKnowledgeLibInfoStore = (): DocumentLibInfoStore => {
|
|
|
|
|
|
const api = {
|
|
|
// 获取知识列表
|
|
|
- fetchDocumentLibList: async () => {
|
|
|
+ fetchDocumentLibList: async (status = 1) => {
|
|
|
actions.setListLoading(true);
|
|
|
try {
|
|
|
const data = {
|
|
|
knowledge_id: state.knowledge_id,
|
|
|
page: state.page.page,
|
|
|
size: state.page.size,
|
|
|
+ status, // 0 未完成,1已完成
|
|
|
+
|
|
|
};
|
|
|
const res = await apis.fetchTakaiDocumentLibListApi(data);
|
|
|
- actions.setList(res.rows);
|
|
|
- actions.setPage({
|
|
|
- ...state.page,
|
|
|
- total: res.total,
|
|
|
- });
|
|
|
+ if (status === 1) {
|
|
|
+ actions.setList(res.rows);
|
|
|
+ actions.setPage({
|
|
|
+ ...state.page,
|
|
|
+ total: res.total,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (status === 0) {
|
|
|
+ res.rows?.forEach((item: any) => {
|
|
|
+ item.progress = 0
|
|
|
+ });
|
|
|
+ actions.setProcessingList(res.rows);
|
|
|
+ }
|
|
|
} catch (error: any) {
|
|
|
console.error(error);
|
|
|
} finally {
|
|
|
@@ -247,6 +262,74 @@ const useKnowledgeLibInfoStore = (): DocumentLibInfoStore => {
|
|
|
actions.setInfoModalSettingOpen(initialInfoModalSettingOpen);
|
|
|
actions.setKnowledgeDetail(initialKnowledgeDetail);
|
|
|
}
|
|
|
+ // SSE 连接
|
|
|
+ const sse: DocumentLibInfoStore['sse'] = (
|
|
|
+ onMessage?: (data: any) => void,
|
|
|
+ onError?: (error: any) => void,
|
|
|
+ onOpen?: () => void,
|
|
|
+ onClose?: () => void
|
|
|
+ ) => {
|
|
|
+ // 从 LocalStorage 获取认证令牌
|
|
|
+ const token = LocalStorage.getToken();
|
|
|
+ const user = LocalStorage.getUserInfo();
|
|
|
+ console.log('user',user)
|
|
|
+ // 如果没有令牌,提示错误并返回
|
|
|
+ if (!token) {
|
|
|
+ message.error('未获取到认证令牌,无法建立 SSE 连接');
|
|
|
+ if (onError) {
|
|
|
+ onError(new Error('未获取到认证令牌'));
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 客户端 ID
|
|
|
+ const clientid = 'e5cd7e4891bf95d1d19206ce24a7b32e';
|
|
|
+ // 构建完整的请求 URL,包含查询参数
|
|
|
+ // 将 Authorization 和 clientid 作为查询参数传递
|
|
|
+ const url = new URL('/api/resource/sse', window.location.origin);
|
|
|
+ url.searchParams.append('tokenValue', `${token}`);
|
|
|
+ url.searchParams.append('userId', `${user?.id}`);
|
|
|
+ const en = new EventSource(url)
|
|
|
+ en.onmessage = event => {
|
|
|
+ console.log('e.data', event.data)
|
|
|
+ let data: any = event.data;
|
|
|
+ try {
|
|
|
+ data = JSON.parse(event.data);
|
|
|
+ console.log('event--消息接收成功', data)
|
|
|
+ const flagProcessingList: any = [...state.processingList]
|
|
|
+ flagProcessingList.forEach((item: any) => {
|
|
|
+ if (item.documentId === data.documentId) {
|
|
|
+ console.log('进来没有---', data.progress)
|
|
|
+ item.progress = data.progress
|
|
|
+ item.status = data.status
|
|
|
+ if (data.progress === 100 || data.message === 'error') {
|
|
|
+ api.fetchDocumentLibList(0);
|
|
|
+ api.fetchDocumentLibList(1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ // console.log('flagProcessingList', flagProcessingList)
|
|
|
+ actions.setProcessingList(flagProcessingList)
|
|
|
+ } catch {
|
|
|
+ // 如果不是 JSON 格式,直接使用原始数据
|
|
|
+ }
|
|
|
+ }
|
|
|
+ en.onerror = e => {
|
|
|
+ console.log('err', e)
|
|
|
+ }
|
|
|
+ en.onopen = e => {
|
|
|
+ console.log('onopen', e)
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }
|
|
|
+ // 删除takai知识文件
|
|
|
+ const onDeleteTakaiDocumentLibApi = async (documentId: string) => {
|
|
|
+ const res = await apis.deleteTakaidelUnfinishedDocumentLibApi(documentId)
|
|
|
+ if (res.code === 200) {
|
|
|
+ api.fetchDocumentLibList(0);
|
|
|
+ api.fetchDocumentLibList(1);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
return {
|
|
|
state,
|
|
|
@@ -260,7 +343,9 @@ const useKnowledgeLibInfoStore = (): DocumentLibInfoStore => {
|
|
|
onClickSettings,
|
|
|
infoModalSettingOnClickConfirm,
|
|
|
infoModalSettingOnClickCancel,
|
|
|
- reset
|
|
|
+ reset,
|
|
|
+ onDeleteTakaiDocumentLibApi,
|
|
|
+ sse
|
|
|
};
|
|
|
};
|
|
|
|