fileHistory.ts
4.25 KB
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
// src/api/fileHistory.ts
import { service as http } from "@/utils/request";
export interface FileHistoryItem {
fileId: string | number;
displayName: string;
fileName: string;
extension: string;
fileType: string;
size: number;
fileSize: number;
last_accessed_at: string;
lastAccessedAt: string;
createdAt: string;
accessCount: number;
isChatAnswer?: boolean;
originalData?: any;
[key: string]: any;
}
export interface FileHistoryListResponse {
items: FileHistoryItem[];
total?: number;
page?: number;
size?: number;
}
/**
* 获取文件访问历史列表
* @param params - 分页参数
*/
export const getList = (params: {
page?: number;
size?: number;
sortBy?: string;
sortOrder?: "ASC" | "DESC";
}) => {
return http
.get<FileHistoryListResponse>("/files/recentAccess", { params })
.then((res: any) => {
// 转换数据格式以匹配前端期望
if (res.data && Array.isArray(res.data.items)) {
res.data.items = transformFileList(res.data.items);
}
return {
...res,
code: 200,
};
});
};
/**
* 删除文件历史记录
* @param id - 文件ID
* @param config - 可选配置 (如 { _silent: true })
*/
export const deleteHistory = (id: string | number, config = {}) => {
return http.delete(`/files/${id}/recentAccess`, config);
};
/**
* 标记文件为打开状态
* @param fileId - 文件ID
*/
export const openFile = (fileId: string | number) => {
return http.post(`/files/${fileId}/open`);
};
/**
* 标记文件为关闭状态
* @param fileId - 文件ID
*/
export const closeFile = (fileId: string | number) => {
return http.post(`/files/${fileId}/close`);
};
/**
* 获取当前打开的文件列表
*/
export const getOpenedFiles = () => {
return http.get<FileHistoryListResponse>("/files/opened")
.then((res: any) => {
// 转换数据格式
if (res.data && Array.isArray(res.data.items)) {
res.data.items = transformFileList(res.data.items);
} else if (res.data && Array.isArray(res.data)) {
// 如果直接返回数组
res.data = {
items: transformFileList(res.data),
total: res.data.length
};
}
return {
...res,
code: 200,
};
});
};
/**
* 转换文件列表数据格式
*/
function transformFileList(files: any[]): FileHistoryItem[] {
if (!Array.isArray(files)) return [];
// 过滤掉文件夹
const filteredFiles = files.filter((file) => !file.isFolder);
return filteredFiles.map((file) => {
if (!file || typeof file !== "object") return file;
// 从 mimeType 或文件名推导 fileType
let fileType = "";
if (file.name && file.name.includes(".")) {
fileType = file.name.split(".").pop()?.toLowerCase() || "";
} else if (file.mimeType) {
const mimeTypeMap: Record<string, string> = {
"application/pdf": "pdf",
"text/x-markdown": "md",
"text/markdown": "md",
"text/plain": "txt",
"application/msword": "doc",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document":
"docx",
"application/vnd.ms-excel": "xls",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
"xlsx",
"application/vnd.ms-powerpoint": "ppt",
"application/vnd.openxmlformats-officedocument.presentationml.presentation":
"pptx",
"image/jpeg": "jpg",
"image/png": "png",
"image/gif": "gif",
};
fileType = mimeTypeMap[file.mimeType] || "unknown";
}
return {
...file,
fileId: file.id,
fileName: file.name,
displayName: file.name,
fileSize: file.size,
fileType: fileType,
extension: fileType,
last_accessed_at: file.lastAccessedAt || file.createdAt,
lastAccessedAt: file.lastAccessedAt || file.createdAt,
createdAt: file.createdAt,
accessCount: file.accessCount || 1,
id: file.id,
name: file.name,
size: file.size,
};
});
}
// 导出默认对象
export default {
getList,
deleteHistory,
openFile,
closeFile,
getOpenedFiles,
};
// 同时导出命名对象(与 vite 项目保持一致)
export const fileHistoryApi = {
getList,
deleteHistory,
openFile,
closeFile,
getOpenedFiles,
};