api.ts 18 KB

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