api.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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,
  7. LoginLocation, LoginLocationDetail,
  8. MessageType, MessageStatus, UserMessage,
  9. 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 = (window.CONFIG?.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. export 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. getCurrentUser: async (): Promise<UserResponse> => {
  201. try {
  202. const response = await axios.get(`${API_BASE_URL}/users/me/profile`);
  203. return response.data;
  204. } catch (error) {
  205. throw error;
  206. }
  207. },
  208. // 更新当前用户信息
  209. updateCurrentUser: async (userData: Partial<User>): Promise<UserUpdateResponse> => {
  210. try {
  211. const response = await axios.put(`${API_BASE_URL}/users/me/profile`, userData);
  212. return response.data;
  213. } catch (error) {
  214. throw error;
  215. }
  216. }
  217. };
  218. // 定义文件相关接口类型
  219. interface FileUploadPolicyResponse {
  220. message: string;
  221. data: MinioUploadPolicy | OSSUploadPolicy;
  222. }
  223. interface FileListResponse {
  224. message: string;
  225. data: {
  226. list: FileLibrary[];
  227. pagination: {
  228. current: number;
  229. pageSize: number;
  230. total: number;
  231. };
  232. };
  233. }
  234. interface FileSaveResponse {
  235. message: string;
  236. data: FileLibrary;
  237. }
  238. interface FileInfoResponse {
  239. message: string;
  240. data: FileLibrary;
  241. }
  242. interface FileDeleteResponse {
  243. message: string;
  244. }
  245. interface FileCategoryListResponse {
  246. data: FileCategory[];
  247. total: number;
  248. page: number;
  249. pageSize: number;
  250. }
  251. interface FileCategoryCreateResponse {
  252. message: string;
  253. data: FileCategory;
  254. }
  255. interface FileCategoryUpdateResponse {
  256. message: string;
  257. data: FileCategory;
  258. }
  259. interface FileCategoryDeleteResponse {
  260. message: string;
  261. }
  262. // 文件API接口定义
  263. export const FileAPI = {
  264. // 获取文件上传策略
  265. getUploadPolicy: async (filename: string, prefix: string = 'uploads/', maxSize: number = 10 * 1024 * 1024): Promise<FileUploadPolicyResponse> => {
  266. try {
  267. const response = await axios.get(`${API_BASE_URL}/upload/policy`, {
  268. params: { filename, prefix, maxSize }
  269. });
  270. return response.data;
  271. } catch (error) {
  272. throw error;
  273. }
  274. },
  275. // 保存文件信息
  276. saveFileInfo: async (fileData: Partial<FileLibrary>): Promise<FileSaveResponse> => {
  277. try {
  278. const response = await axios.post(`${API_BASE_URL}/upload/save`, fileData);
  279. return response.data;
  280. } catch (error) {
  281. throw error;
  282. }
  283. },
  284. // 获取文件列表
  285. getFileList: async (params?: {
  286. page?: number,
  287. pageSize?: number,
  288. category_id?: number,
  289. fileType?: string,
  290. keyword?: string
  291. }): Promise<FileListResponse> => {
  292. try {
  293. const response = await axios.get(`${API_BASE_URL}/upload/list`, { params });
  294. return response.data;
  295. } catch (error) {
  296. throw error;
  297. }
  298. },
  299. // 获取单个文件信息
  300. getFileInfo: async (id: number): Promise<FileInfoResponse> => {
  301. try {
  302. const response = await axios.get(`${API_BASE_URL}/upload/${id}`);
  303. return response.data;
  304. } catch (error) {
  305. throw error;
  306. }
  307. },
  308. // 更新文件下载计数
  309. updateDownloadCount: async (id: number): Promise<FileDeleteResponse> => {
  310. try {
  311. const response = await axios.post(`${API_BASE_URL}/upload/${id}/download`);
  312. return response.data;
  313. } catch (error) {
  314. throw error;
  315. }
  316. },
  317. // 删除文件
  318. deleteFile: async (id: number): Promise<FileDeleteResponse> => {
  319. try {
  320. const response = await axios.delete(`${API_BASE_URL}/upload/${id}`);
  321. return response.data;
  322. } catch (error) {
  323. throw error;
  324. }
  325. },
  326. // 获取文件分类列表
  327. getCategories: async (params?: {
  328. page?: number,
  329. pageSize?: number,
  330. search?: string
  331. }): Promise<FileCategoryListResponse> => {
  332. try {
  333. const response = await axios.get(`${API_BASE_URL}/file-categories`, { params });
  334. return response.data;
  335. } catch (error) {
  336. throw error;
  337. }
  338. },
  339. // 创建文件分类
  340. createCategory: async (data: Partial<FileCategory>): Promise<FileCategoryCreateResponse> => {
  341. try {
  342. const response = await axios.post(`${API_BASE_URL}/file-categories`, data);
  343. return response.data;
  344. } catch (error) {
  345. throw error;
  346. }
  347. },
  348. // 更新文件分类
  349. updateCategory: async (id: number, data: Partial<FileCategory>): Promise<FileCategoryUpdateResponse> => {
  350. try {
  351. const response = await axios.put(`${API_BASE_URL}/file-categories/${id}`, data);
  352. return response.data;
  353. } catch (error) {
  354. throw error;
  355. }
  356. },
  357. // 删除文件分类
  358. deleteCategory: async (id: number): Promise<FileCategoryDeleteResponse> => {
  359. try {
  360. const response = await axios.delete(`${API_BASE_URL}/file-categories/${id}`);
  361. return response.data;
  362. } catch (error) {
  363. throw error;
  364. }
  365. }
  366. };
  367. // Theme API 定义
  368. export const ThemeAPI = {
  369. // 获取主题设置
  370. getThemeSettings: async (): Promise<ThemeSettings> => {
  371. try {
  372. const response = await axios.get(`${API_BASE_URL}/theme`);
  373. return response.data.data;
  374. } catch (error) {
  375. throw error;
  376. }
  377. },
  378. // 更新主题设置
  379. updateThemeSettings: async (themeData: Partial<ThemeSettings>): Promise<ThemeSettings> => {
  380. try {
  381. const response = await axios.put(`${API_BASE_URL}/theme`, themeData);
  382. return response.data.data;
  383. } catch (error) {
  384. throw error;
  385. }
  386. },
  387. // 重置主题设置
  388. resetThemeSettings: async (): Promise<ThemeSettings> => {
  389. try {
  390. const response = await axios.post(`${API_BASE_URL}/theme/reset`);
  391. return response.data.data;
  392. } catch (error) {
  393. throw error;
  394. }
  395. }
  396. };
  397. // 图表数据API接口类型
  398. interface ChartDataResponse<T> {
  399. message: string;
  400. data: T;
  401. }
  402. interface UserActivityData {
  403. date: string;
  404. count: number;
  405. }
  406. interface FileUploadsData {
  407. month: string;
  408. count: number;
  409. }
  410. interface FileTypesData {
  411. type: string;
  412. value: number;
  413. }
  414. interface DashboardOverviewData {
  415. userCount: number;
  416. fileCount: number;
  417. articleCount: number;
  418. todayLoginCount: number;
  419. }
  420. // 图表数据API
  421. export const ChartAPI = {
  422. // 获取用户活跃度数据
  423. getUserActivity: async (): Promise<ChartDataResponse<UserActivityData[]>> => {
  424. try {
  425. const response = await axios.get(`${API_BASE_URL}/charts/user-activity`);
  426. return response.data;
  427. } catch (error) {
  428. throw error;
  429. }
  430. },
  431. // 获取文件上传统计数据
  432. getFileUploads: async (): Promise<ChartDataResponse<FileUploadsData[]>> => {
  433. try {
  434. const response = await axios.get(`${API_BASE_URL}/charts/file-uploads`);
  435. return response.data;
  436. } catch (error) {
  437. throw error;
  438. }
  439. },
  440. // 获取文件类型分布数据
  441. getFileTypes: async (): Promise<ChartDataResponse<FileTypesData[]>> => {
  442. try {
  443. const response = await axios.get(`${API_BASE_URL}/charts/file-types`);
  444. return response.data;
  445. } catch (error) {
  446. throw error;
  447. }
  448. },
  449. // 获取仪表盘概览数据
  450. getDashboardOverview: async (): Promise<ChartDataResponse<DashboardOverviewData>> => {
  451. try {
  452. const response = await axios.get(`${API_BASE_URL}/charts/dashboard-overview`);
  453. return response.data;
  454. } catch (error) {
  455. throw error;
  456. }
  457. }
  458. };
  459. // 首页API相关类型定义
  460. interface HomeBannersResponse {
  461. message: string;
  462. data: KnowInfo[];
  463. }
  464. interface HomeNewsResponse {
  465. message: string;
  466. data: KnowInfo[];
  467. pagination: {
  468. total: number;
  469. current: number;
  470. pageSize: number;
  471. totalPages: number;
  472. };
  473. }
  474. interface HomeNoticesResponse {
  475. message: string;
  476. data: {
  477. id: number;
  478. title: string;
  479. content: string;
  480. created_at: string;
  481. }[];
  482. pagination: {
  483. total: number;
  484. current: number;
  485. pageSize: number;
  486. totalPages: number;
  487. };
  488. }
  489. // 首页API
  490. export const HomeAPI = {
  491. // 获取轮播图
  492. getBanners: async (): Promise<HomeBannersResponse> => {
  493. try {
  494. const response = await axios.get(`${API_BASE_URL}/home/banners`);
  495. return response.data;
  496. } catch (error) {
  497. throw error;
  498. }
  499. },
  500. // 获取新闻列表
  501. getNews: async (params?: {
  502. page?: number,
  503. pageSize?: number,
  504. category?: string
  505. }): Promise<HomeNewsResponse> => {
  506. try {
  507. const response = await axios.get(`${API_BASE_URL}/home/news`, { params });
  508. return response.data;
  509. } catch (error) {
  510. throw error;
  511. }
  512. },
  513. // 获取通知列表
  514. getNotices: async (params?: {
  515. page?: number,
  516. pageSize?: number
  517. }): Promise<HomeNoticesResponse> => {
  518. try {
  519. const response = await axios.get(`${API_BASE_URL}/home/notices`, { params });
  520. return response.data;
  521. } catch (error) {
  522. throw error;
  523. }
  524. }
  525. };
  526. // 地图相关API的接口类型定义
  527. export interface LoginLocationResponse {
  528. message: string;
  529. data: LoginLocation[];
  530. }
  531. export interface LoginLocationDetailResponse {
  532. message: string;
  533. data: LoginLocationDetail;
  534. }
  535. export interface LoginLocationUpdateResponse {
  536. message: string;
  537. data: LoginLocationDetail;
  538. }
  539. // 地图相关API
  540. export const MapAPI = {
  541. // 获取地图标记点数据
  542. getMarkers: async (params?: {
  543. startTime?: string;
  544. endTime?: string;
  545. userId?: number
  546. }): Promise<LoginLocationResponse> => {
  547. try {
  548. const response = await axios.get(`${API_BASE_URL}/map/markers`, { params });
  549. return response.data;
  550. } catch (error) {
  551. throw error;
  552. }
  553. },
  554. // 获取登录位置详情
  555. getLocationDetail: async (locationId: number): Promise<LoginLocationDetailResponse> => {
  556. try {
  557. const response = await axios.get(`${API_BASE_URL}/map/location/${locationId}`);
  558. return response.data;
  559. } catch (error) {
  560. throw error;
  561. }
  562. },
  563. // 更新登录位置信息
  564. updateLocation: async (locationId: number, data: {
  565. longitude: number;
  566. latitude: number;
  567. location_name?: string;
  568. }): Promise<LoginLocationUpdateResponse> => {
  569. try {
  570. const response = await axios.put(`${API_BASE_URL}/map/location/${locationId}`, data);
  571. return response.data;
  572. } catch (error) {
  573. throw error;
  574. }
  575. }
  576. };
  577. // 系统设置API
  578. export const SystemAPI = {
  579. // 获取所有系统设置
  580. getSettings: async (): Promise<SystemSettingGroupData[]> => {
  581. try {
  582. const response = await axios.get(`${API_BASE_URL}/settings`);
  583. return response.data.data;
  584. } catch (error) {
  585. throw error;
  586. }
  587. },
  588. // 获取指定分组的系统设置
  589. getSettingsByGroup: async (group: string): Promise<SystemSetting[]> => {
  590. try {
  591. const response = await axios.get(`${API_BASE_URL}/settings/group/${group}`);
  592. return response.data.data;
  593. } catch (error) {
  594. throw error;
  595. }
  596. },
  597. // 更新系统设置
  598. updateSettings: async (settings: Partial<SystemSetting>[]): Promise<SystemSetting[]> => {
  599. try {
  600. const response = await axios.put(`${API_BASE_URL}/settings`, settings);
  601. return response.data.data;
  602. } catch (error) {
  603. throw error;
  604. }
  605. },
  606. // 重置系统设置
  607. resetSettings: async (): Promise<SystemSetting[]> => {
  608. try {
  609. const response = await axios.post(`${API_BASE_URL}/settings/reset`);
  610. return response.data.data;
  611. } catch (error) {
  612. throw error;
  613. }
  614. }
  615. };
  616. // 消息API响应类型
  617. export interface MessageResponse {
  618. message: string;
  619. data?: any;
  620. }
  621. export interface MessagesResponse {
  622. data: UserMessage[];
  623. pagination: {
  624. total: number;
  625. current: number;
  626. pageSize: number;
  627. totalPages: number;
  628. };
  629. }
  630. export interface UnreadCountResponse {
  631. count: number;
  632. }
  633. // 消息API
  634. export const MessageAPI = {
  635. // 获取消息列表
  636. getMessages: async (params?: {
  637. page?: number,
  638. pageSize?: number,
  639. type?: MessageType,
  640. status?: MessageStatus
  641. }): Promise<MessagesResponse> => {
  642. try {
  643. const response = await axios.get(`${API_BASE_URL}/messages`, { params });
  644. return response.data;
  645. } catch (error) {
  646. throw error;
  647. }
  648. },
  649. // 获取消息详情
  650. getMessage: async (id: number): Promise<MessageResponse> => {
  651. try {
  652. const response = await axios.get(`${API_BASE_URL}/messages/${id}`);
  653. return response.data;
  654. } catch (error) {
  655. throw error;
  656. }
  657. },
  658. // 发送消息
  659. sendMessage: async (data: {
  660. title: string,
  661. content: string,
  662. type: MessageType,
  663. receiver_ids: number[]
  664. }): Promise<MessageResponse> => {
  665. try {
  666. const response = await axios.post(`${API_BASE_URL}/messages`, data);
  667. return response.data;
  668. } catch (error) {
  669. throw error;
  670. }
  671. },
  672. // 删除消息(软删除)
  673. deleteMessage: async (id: number): Promise<MessageResponse> => {
  674. try {
  675. const response = await axios.delete(`${API_BASE_URL}/messages/${id}`);
  676. return response.data;
  677. } catch (error) {
  678. throw error;
  679. }
  680. },
  681. // 获取未读消息数量
  682. getUnreadCount: async (): Promise<UnreadCountResponse> => {
  683. try {
  684. const response = await axios.get(`${API_BASE_URL}/messages/count/unread`);
  685. return response.data;
  686. } catch (error) {
  687. throw error;
  688. }
  689. },
  690. // 标记消息为已读
  691. markAsRead: async (id: number): Promise<MessageResponse> => {
  692. try {
  693. const response = await axios.post(`${API_BASE_URL}/messages/${id}/read`);
  694. return response.data;
  695. } catch (error) {
  696. throw error;
  697. }
  698. }
  699. };