api.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. import axios from 'axios';
  2. import type { MinioUploadPolicy, OSSUploadPolicy } from '@d8d-appcontainer/types';
  3. import 'dayjs/locale/zh-cn';
  4. import type {
  5. User, FileLibrary, FileCategory, ThemeSettings,
  6. SystemSetting, SystemSettingGroupData, LoginLocation, LoginLocationDetail
  7. } from '../share/types.ts';
  8. // 定义API基础URL
  9. const API_BASE_URL = '/api';
  10. // 获取OSS完整URL
  11. export const getOssUrl = (path: string): string => {
  12. // 获取全局配置中的OSS_HOST,如果不存在使用默认值
  13. const ossHost = (window.CONFIG?.OSS_BASE_URL) || '';
  14. // 确保path不以/开头
  15. const ossPath = path.startsWith('/') ? path.substring(1) : path;
  16. return `${ossHost}/${ossPath}`;
  17. };
  18. // ===================
  19. // Auth API 定义部分
  20. // ===================
  21. // 定义API返回数据类型
  22. interface AuthLoginResponse {
  23. message: string;
  24. token: string;
  25. refreshToken?: string;
  26. user: User;
  27. }
  28. interface AuthResponse {
  29. message: string;
  30. [key: string]: any;
  31. }
  32. // 定义Auth API接口类型
  33. interface AuthAPIType {
  34. login: (username: string, password: string, latitude?: number, longitude?: number) => Promise<AuthLoginResponse>;
  35. register: (username: string, email: string, password: string) => Promise<AuthResponse>;
  36. logout: () => Promise<AuthResponse>;
  37. getCurrentUser: () => Promise<User>;
  38. updateUser: (userId: number, userData: Partial<User>) => Promise<User>;
  39. changePassword: (oldPassword: string, newPassword: string) => Promise<AuthResponse>;
  40. requestPasswordReset: (email: string) => Promise<AuthResponse>;
  41. resetPassword: (token: string, newPassword: string) => Promise<AuthResponse>;
  42. }
  43. // Auth相关API
  44. export const AuthAPI: AuthAPIType = {
  45. // 登录API
  46. login: async (username: string, password: string, latitude?: number, longitude?: number) => {
  47. try {
  48. const response = await axios.post(`${API_BASE_URL}/auth/login`, {
  49. username,
  50. password,
  51. latitude,
  52. longitude
  53. });
  54. return response.data;
  55. } catch (error) {
  56. throw error;
  57. }
  58. },
  59. // 注册API
  60. register: async (username: string, email: string, password: string) => {
  61. try {
  62. const response = await axios.post(`${API_BASE_URL}/auth/register`, { username, email, password });
  63. return response.data;
  64. } catch (error) {
  65. throw error;
  66. }
  67. },
  68. // 登出API
  69. logout: async () => {
  70. try {
  71. const response = await axios.post(`${API_BASE_URL}/auth/logout`);
  72. return response.data;
  73. } catch (error) {
  74. throw error;
  75. }
  76. },
  77. // 获取当前用户信息
  78. getCurrentUser: async () => {
  79. try {
  80. const response = await axios.get(`${API_BASE_URL}/auth/me`);
  81. return response.data;
  82. } catch (error) {
  83. throw error;
  84. }
  85. },
  86. // 更新用户信息
  87. updateUser: async (userId: number, userData: Partial<User>) => {
  88. try {
  89. const response = await axios.put(`${API_BASE_URL}/auth/users/${userId}`, userData);
  90. return response.data;
  91. } catch (error) {
  92. throw error;
  93. }
  94. },
  95. // 修改密码
  96. changePassword: async (oldPassword: string, newPassword: string) => {
  97. try {
  98. const response = await axios.post(`${API_BASE_URL}/auth/change-password`, { oldPassword, newPassword });
  99. return response.data;
  100. } catch (error) {
  101. throw error;
  102. }
  103. },
  104. // 请求重置密码
  105. requestPasswordReset: async (email: string) => {
  106. try {
  107. const response = await axios.post(`${API_BASE_URL}/auth/request-password-reset`, { email });
  108. return response.data;
  109. } catch (error) {
  110. throw error;
  111. }
  112. },
  113. // 重置密码
  114. resetPassword: async (token: string, newPassword: string) => {
  115. try {
  116. const response = await axios.post(`${API_BASE_URL}/auth/reset-password`, { token, newPassword });
  117. return response.data;
  118. } catch (error) {
  119. throw error;
  120. }
  121. }
  122. };
  123. // 为UserAPI添加的接口响应类型
  124. interface UsersResponse {
  125. data: User[];
  126. pagination: {
  127. total: number;
  128. current: number;
  129. pageSize: number;
  130. totalPages: number;
  131. };
  132. }
  133. interface UserResponse {
  134. data: User;
  135. message?: string;
  136. }
  137. interface UserCreateResponse {
  138. message: string;
  139. data: User;
  140. }
  141. interface UserUpdateResponse {
  142. message: string;
  143. data: User;
  144. }
  145. interface UserDeleteResponse {
  146. message: string;
  147. id: number;
  148. }
  149. // 用户管理API
  150. export const UserAPI = {
  151. // 获取用户列表
  152. getUsers: async (params?: { page?: number, limit?: number, search?: string }): Promise<UsersResponse> => {
  153. try {
  154. const response = await axios.get(`${API_BASE_URL}/users`, { params });
  155. return response.data;
  156. } catch (error) {
  157. throw error;
  158. }
  159. },
  160. // 获取单个用户详情
  161. getUser: async (userId: number): Promise<UserResponse> => {
  162. try {
  163. const response = await axios.get(`${API_BASE_URL}/users/${userId}`);
  164. return response.data;
  165. } catch (error) {
  166. throw error;
  167. }
  168. },
  169. // 创建用户
  170. createUser: async (userData: Partial<User>): Promise<UserCreateResponse> => {
  171. try {
  172. const response = await axios.post(`${API_BASE_URL}/users`, userData);
  173. return response.data;
  174. } catch (error) {
  175. throw error;
  176. }
  177. },
  178. // 更新用户信息
  179. updateUser: async (userId: number, userData: Partial<User>): Promise<UserUpdateResponse> => {
  180. try {
  181. const response = await axios.put(`${API_BASE_URL}/users/${userId}`, userData);
  182. return response.data;
  183. } catch (error) {
  184. throw error;
  185. }
  186. },
  187. // 删除用户
  188. deleteUser: async (userId: number): Promise<UserDeleteResponse> => {
  189. try {
  190. const response = await axios.delete(`${API_BASE_URL}/users/${userId}`);
  191. return response.data;
  192. } catch (error) {
  193. throw error;
  194. }
  195. }
  196. };
  197. // 定义文件相关接口类型
  198. interface FileUploadPolicyResponse {
  199. message: string;
  200. data: MinioUploadPolicy | OSSUploadPolicy;
  201. }
  202. interface FileListResponse {
  203. message: string;
  204. data: {
  205. list: FileLibrary[];
  206. pagination: {
  207. current: number;
  208. pageSize: number;
  209. total: number;
  210. };
  211. };
  212. }
  213. interface FileSaveResponse {
  214. message: string;
  215. data: FileLibrary;
  216. }
  217. interface FileInfoResponse {
  218. message: string;
  219. data: FileLibrary;
  220. }
  221. interface FileDeleteResponse {
  222. message: string;
  223. }
  224. interface FileCategoryListResponse {
  225. data: FileCategory[];
  226. total: number;
  227. page: number;
  228. pageSize: number;
  229. }
  230. interface FileCategoryCreateResponse {
  231. message: string;
  232. data: FileCategory;
  233. }
  234. interface FileCategoryUpdateResponse {
  235. message: string;
  236. data: FileCategory;
  237. }
  238. interface FileCategoryDeleteResponse {
  239. message: string;
  240. }
  241. // 文件API接口定义
  242. export const FileAPI = {
  243. // 获取文件上传策略
  244. getUploadPolicy: async (filename: string, prefix: string = 'uploads/', maxSize: number = 10 * 1024 * 1024): Promise<FileUploadPolicyResponse> => {
  245. try {
  246. const response = await axios.get(`${API_BASE_URL}/upload/policy`, {
  247. params: { filename, prefix, maxSize }
  248. });
  249. return response.data;
  250. } catch (error) {
  251. throw error;
  252. }
  253. },
  254. // 保存文件信息
  255. saveFileInfo: async (fileData: Partial<FileLibrary>): Promise<FileSaveResponse> => {
  256. try {
  257. const response = await axios.post(`${API_BASE_URL}/upload/save`, fileData);
  258. return response.data;
  259. } catch (error) {
  260. throw error;
  261. }
  262. },
  263. // 获取文件列表
  264. getFileList: async (params?: {
  265. page?: number,
  266. pageSize?: number,
  267. category_id?: number,
  268. fileType?: string,
  269. keyword?: string
  270. }): Promise<FileListResponse> => {
  271. try {
  272. const response = await axios.get(`${API_BASE_URL}/upload/list`, { params });
  273. return response.data;
  274. } catch (error) {
  275. throw error;
  276. }
  277. },
  278. // 获取单个文件信息
  279. getFileInfo: async (id: number): Promise<FileInfoResponse> => {
  280. try {
  281. const response = await axios.get(`${API_BASE_URL}/upload/${id}`);
  282. return response.data;
  283. } catch (error) {
  284. throw error;
  285. }
  286. },
  287. // 更新文件下载计数
  288. updateDownloadCount: async (id: number): Promise<FileDeleteResponse> => {
  289. try {
  290. const response = await axios.post(`${API_BASE_URL}/upload/${id}/download`);
  291. return response.data;
  292. } catch (error) {
  293. throw error;
  294. }
  295. },
  296. // 删除文件
  297. deleteFile: async (id: number): Promise<FileDeleteResponse> => {
  298. try {
  299. const response = await axios.delete(`${API_BASE_URL}/upload/${id}`);
  300. return response.data;
  301. } catch (error) {
  302. throw error;
  303. }
  304. },
  305. // 获取文件分类列表
  306. getCategories: async (params?: {
  307. page?: number,
  308. pageSize?: number,
  309. search?: string
  310. }): Promise<FileCategoryListResponse> => {
  311. try {
  312. const response = await axios.get(`${API_BASE_URL}/file-categories`, { params });
  313. return response.data;
  314. } catch (error) {
  315. throw error;
  316. }
  317. },
  318. // 创建文件分类
  319. createCategory: async (data: Partial<FileCategory>): Promise<FileCategoryCreateResponse> => {
  320. try {
  321. const response = await axios.post(`${API_BASE_URL}/file-categories`, data);
  322. return response.data;
  323. } catch (error) {
  324. throw error;
  325. }
  326. },
  327. // 更新文件分类
  328. updateCategory: async (id: number, data: Partial<FileCategory>): Promise<FileCategoryUpdateResponse> => {
  329. try {
  330. const response = await axios.put(`${API_BASE_URL}/file-categories/${id}`, data);
  331. return response.data;
  332. } catch (error) {
  333. throw error;
  334. }
  335. },
  336. // 删除文件分类
  337. deleteCategory: async (id: number): Promise<FileCategoryDeleteResponse> => {
  338. try {
  339. const response = await axios.delete(`${API_BASE_URL}/file-categories/${id}`);
  340. return response.data;
  341. } catch (error) {
  342. throw error;
  343. }
  344. }
  345. };
  346. // Theme API 响应类型
  347. export interface ThemeSettingsResponse {
  348. message: string;
  349. data: ThemeSettings;
  350. }
  351. // Theme API 定义
  352. export const ThemeAPI = {
  353. // 获取主题设置
  354. getThemeSettings: async (): Promise<ThemeSettings> => {
  355. try {
  356. const response = await axios.get(`${API_BASE_URL}/theme`);
  357. return response.data.data;
  358. } catch (error) {
  359. throw error;
  360. }
  361. },
  362. // 更新主题设置
  363. updateThemeSettings: async (themeData: Partial<ThemeSettings>): Promise<ThemeSettings> => {
  364. try {
  365. const response = await axios.put(`${API_BASE_URL}/theme`, themeData);
  366. return response.data.data;
  367. } catch (error) {
  368. throw error;
  369. }
  370. },
  371. // 重置主题设置
  372. resetThemeSettings: async (): Promise<ThemeSettings> => {
  373. try {
  374. const response = await axios.post(`${API_BASE_URL}/theme/reset`);
  375. return response.data.data;
  376. } catch (error) {
  377. throw error;
  378. }
  379. }
  380. };
  381. // 图表数据API接口类型
  382. interface ChartDataResponse<T> {
  383. message: string;
  384. data: T;
  385. }
  386. interface UserActivityData {
  387. date: string;
  388. count: number;
  389. }
  390. interface FileUploadsData {
  391. month: string;
  392. count: number;
  393. }
  394. interface FileTypesData {
  395. type: string;
  396. value: number;
  397. }
  398. interface DashboardOverviewData {
  399. userCount: number;
  400. fileCount: number;
  401. articleCount: number;
  402. todayLoginCount: number;
  403. }
  404. // 图表数据API
  405. export const ChartAPI = {
  406. // 获取用户活跃度数据
  407. getUserActivity: async (): Promise<ChartDataResponse<UserActivityData[]>> => {
  408. try {
  409. const response = await axios.get(`${API_BASE_URL}/charts/user-activity`);
  410. return response.data;
  411. } catch (error) {
  412. throw error;
  413. }
  414. },
  415. // 获取文件上传统计数据
  416. getFileUploads: async (): Promise<ChartDataResponse<FileUploadsData[]>> => {
  417. try {
  418. const response = await axios.get(`${API_BASE_URL}/charts/file-uploads`);
  419. return response.data;
  420. } catch (error) {
  421. throw error;
  422. }
  423. },
  424. // 获取文件类型分布数据
  425. getFileTypes: async (): Promise<ChartDataResponse<FileTypesData[]>> => {
  426. try {
  427. const response = await axios.get(`${API_BASE_URL}/charts/file-types`);
  428. return response.data;
  429. } catch (error) {
  430. throw error;
  431. }
  432. },
  433. // 获取仪表盘概览数据
  434. getDashboardOverview: async (): Promise<ChartDataResponse<DashboardOverviewData>> => {
  435. try {
  436. const response = await axios.get(`${API_BASE_URL}/charts/dashboard-overview`);
  437. return response.data;
  438. } catch (error) {
  439. throw error;
  440. }
  441. }
  442. };
  443. // 地图相关API的接口类型定义
  444. export interface LoginLocationResponse {
  445. message: string;
  446. data: LoginLocation[];
  447. }
  448. export interface LoginLocationDetailResponse {
  449. message: string;
  450. data: LoginLocationDetail;
  451. }
  452. export interface LoginLocationUpdateResponse {
  453. message: string;
  454. data: LoginLocationDetail;
  455. }
  456. // 地图相关API
  457. export const MapAPI = {
  458. // 获取地图标记点数据
  459. getMarkers: async (params?: {
  460. startTime?: string;
  461. endTime?: string;
  462. userId?: number
  463. }): Promise<LoginLocationResponse> => {
  464. try {
  465. const response = await axios.get(`${API_BASE_URL}/map/markers`, { params });
  466. return response.data;
  467. } catch (error) {
  468. throw error;
  469. }
  470. },
  471. // 获取登录位置详情
  472. getLocationDetail: async (locationId: number): Promise<LoginLocationDetailResponse> => {
  473. try {
  474. const response = await axios.get(`${API_BASE_URL}/map/location/${locationId}`);
  475. return response.data;
  476. } catch (error) {
  477. throw error;
  478. }
  479. },
  480. // 更新登录位置信息
  481. updateLocation: async (locationId: number, data: {
  482. longitude: number;
  483. latitude: number;
  484. location_name?: string;
  485. }): Promise<LoginLocationUpdateResponse> => {
  486. try {
  487. const response = await axios.put(`${API_BASE_URL}/map/location/${locationId}`, data);
  488. return response.data;
  489. } catch (error) {
  490. throw error;
  491. }
  492. }
  493. };
  494. // 系统设置API
  495. export const SystemAPI = {
  496. // 获取所有系统设置
  497. getSettings: async (): Promise<SystemSettingGroupData[]> => {
  498. try {
  499. const response = await axios.get(`${API_BASE_URL}/settings`);
  500. return response.data.data;
  501. } catch (error) {
  502. throw error;
  503. }
  504. },
  505. // 获取指定分组的系统设置
  506. getSettingsByGroup: async (group: string): Promise<SystemSetting[]> => {
  507. try {
  508. const response = await axios.get(`${API_BASE_URL}/settings/group/${group}`);
  509. return response.data.data;
  510. } catch (error) {
  511. throw error;
  512. }
  513. },
  514. // 更新系统设置
  515. updateSettings: async (settings: Partial<SystemSetting>[]): Promise<SystemSetting[]> => {
  516. try {
  517. const response = await axios.put(`${API_BASE_URL}/settings`, settings);
  518. return response.data.data;
  519. } catch (error) {
  520. throw error;
  521. }
  522. },
  523. // 重置系统设置
  524. resetSettings: async (): Promise<SystemSetting[]> => {
  525. try {
  526. const response = await axios.post(`${API_BASE_URL}/settings/reset`);
  527. return response.data.data;
  528. } catch (error) {
  529. throw error;
  530. }
  531. }
  532. };