api.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. 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 const ThemeAPI = {
  351. // 获取主题设置
  352. getThemeSettings: async (): Promise<ThemeSettings> => {
  353. try {
  354. const response = await axios.get(`${API_BASE_URL}/theme`);
  355. return response.data.data;
  356. } catch (error) {
  357. throw error;
  358. }
  359. },
  360. // 更新主题设置
  361. updateThemeSettings: async (themeData: Partial<ThemeSettings>): Promise<ThemeSettings> => {
  362. try {
  363. const response = await axios.put(`${API_BASE_URL}/theme`, themeData);
  364. return response.data.data;
  365. } catch (error) {
  366. throw error;
  367. }
  368. },
  369. // 重置主题设置
  370. resetThemeSettings: async (): Promise<ThemeSettings> => {
  371. try {
  372. const response = await axios.post(`${API_BASE_URL}/theme/reset`);
  373. return response.data.data;
  374. } catch (error) {
  375. throw error;
  376. }
  377. }
  378. };
  379. // 图表数据API接口类型
  380. interface ChartDataResponse<T> {
  381. message: string;
  382. data: T;
  383. }
  384. interface UserActivityData {
  385. date: string;
  386. count: number;
  387. }
  388. interface FileUploadsData {
  389. month: string;
  390. count: number;
  391. }
  392. interface FileTypesData {
  393. type: string;
  394. value: number;
  395. }
  396. interface DashboardOverviewData {
  397. userCount: number;
  398. fileCount: number;
  399. articleCount: number;
  400. todayLoginCount: number;
  401. }
  402. // 图表数据API
  403. export const ChartAPI = {
  404. // 获取用户活跃度数据
  405. getUserActivity: async (): Promise<ChartDataResponse<UserActivityData[]>> => {
  406. try {
  407. const response = await axios.get(`${API_BASE_URL}/charts/user-activity`);
  408. return response.data;
  409. } catch (error) {
  410. throw error;
  411. }
  412. },
  413. // 获取文件上传统计数据
  414. getFileUploads: async (): Promise<ChartDataResponse<FileUploadsData[]>> => {
  415. try {
  416. const response = await axios.get(`${API_BASE_URL}/charts/file-uploads`);
  417. return response.data;
  418. } catch (error) {
  419. throw error;
  420. }
  421. },
  422. // 获取文件类型分布数据
  423. getFileTypes: async (): Promise<ChartDataResponse<FileTypesData[]>> => {
  424. try {
  425. const response = await axios.get(`${API_BASE_URL}/charts/file-types`);
  426. return response.data;
  427. } catch (error) {
  428. throw error;
  429. }
  430. },
  431. // 获取仪表盘概览数据
  432. getDashboardOverview: async (): Promise<ChartDataResponse<DashboardOverviewData>> => {
  433. try {
  434. const response = await axios.get(`${API_BASE_URL}/charts/dashboard-overview`);
  435. return response.data;
  436. } catch (error) {
  437. throw error;
  438. }
  439. }
  440. };
  441. // 首页API相关类型定义
  442. interface HomeBannersResponse {
  443. message: string;
  444. data: KnowInfo[];
  445. }
  446. interface HomeNewsResponse {
  447. message: string;
  448. data: KnowInfo[];
  449. pagination: {
  450. total: number;
  451. current: number;
  452. pageSize: number;
  453. totalPages: number;
  454. };
  455. }
  456. interface HomeNoticesResponse {
  457. message: string;
  458. data: {
  459. id: number;
  460. title: string;
  461. content: string;
  462. created_at: string;
  463. }[];
  464. pagination: {
  465. total: number;
  466. current: number;
  467. pageSize: number;
  468. totalPages: number;
  469. };
  470. }
  471. // 首页API
  472. export const HomeAPI = {
  473. // 获取轮播图
  474. getBanners: async (): Promise<HomeBannersResponse> => {
  475. try {
  476. const response = await axios.get(`${API_BASE_URL}/home/banners`);
  477. return response.data;
  478. } catch (error) {
  479. throw error;
  480. }
  481. },
  482. // 获取新闻列表
  483. getNews: async (params?: {
  484. page?: number,
  485. pageSize?: number,
  486. category?: string
  487. }): Promise<HomeNewsResponse> => {
  488. try {
  489. const response = await axios.get(`${API_BASE_URL}/home/news`, { params });
  490. return response.data;
  491. } catch (error) {
  492. throw error;
  493. }
  494. },
  495. // 获取通知列表
  496. getNotices: async (params?: {
  497. page?: number,
  498. pageSize?: number
  499. }): Promise<HomeNoticesResponse> => {
  500. try {
  501. const response = await axios.get(`${API_BASE_URL}/home/notices`, { params });
  502. return response.data;
  503. } catch (error) {
  504. throw error;
  505. }
  506. }
  507. };
  508. // 地图相关API的接口类型定义
  509. export interface LoginLocationResponse {
  510. message: string;
  511. data: LoginLocation[];
  512. }
  513. export interface LoginLocationDetailResponse {
  514. message: string;
  515. data: LoginLocationDetail;
  516. }
  517. export interface LoginLocationUpdateResponse {
  518. message: string;
  519. data: LoginLocationDetail;
  520. }
  521. // 地图相关API
  522. export const MapAPI = {
  523. // 获取地图标记点数据
  524. getMarkers: async (params?: {
  525. startTime?: string;
  526. endTime?: string;
  527. userId?: number
  528. }): Promise<LoginLocationResponse> => {
  529. try {
  530. const response = await axios.get(`${API_BASE_URL}/map/markers`, { params });
  531. return response.data;
  532. } catch (error) {
  533. throw error;
  534. }
  535. },
  536. // 获取登录位置详情
  537. getLocationDetail: async (locationId: number): Promise<LoginLocationDetailResponse> => {
  538. try {
  539. const response = await axios.get(`${API_BASE_URL}/map/location/${locationId}`);
  540. return response.data;
  541. } catch (error) {
  542. throw error;
  543. }
  544. },
  545. // 更新登录位置信息
  546. updateLocation: async (locationId: number, data: {
  547. longitude: number;
  548. latitude: number;
  549. location_name?: string;
  550. }): Promise<LoginLocationUpdateResponse> => {
  551. try {
  552. const response = await axios.put(`${API_BASE_URL}/map/location/${locationId}`, data);
  553. return response.data;
  554. } catch (error) {
  555. throw error;
  556. }
  557. }
  558. };
  559. // 系统设置API
  560. export const SystemAPI = {
  561. // 获取所有系统设置
  562. getSettings: async (): Promise<SystemSettingGroupData[]> => {
  563. try {
  564. const response = await axios.get(`${API_BASE_URL}/settings`);
  565. return response.data.data;
  566. } catch (error) {
  567. throw error;
  568. }
  569. },
  570. // 获取指定分组的系统设置
  571. getSettingsByGroup: async (group: string): Promise<SystemSetting[]> => {
  572. try {
  573. const response = await axios.get(`${API_BASE_URL}/settings/group/${group}`);
  574. return response.data.data;
  575. } catch (error) {
  576. throw error;
  577. }
  578. },
  579. // 更新系统设置
  580. updateSettings: async (settings: Partial<SystemSetting>[]): Promise<SystemSetting[]> => {
  581. try {
  582. const response = await axios.put(`${API_BASE_URL}/settings`, settings);
  583. return response.data.data;
  584. } catch (error) {
  585. throw error;
  586. }
  587. },
  588. // 重置系统设置
  589. resetSettings: async (): Promise<SystemSetting[]> => {
  590. try {
  591. const response = await axios.post(`${API_BASE_URL}/settings/reset`);
  592. return response.data.data;
  593. } catch (error) {
  594. throw error;
  595. }
  596. }
  597. };
  598. // 消息API响应类型
  599. export interface MessageResponse {
  600. message: string;
  601. data?: any;
  602. }
  603. export interface MessagesResponse {
  604. data: UserMessage[];
  605. pagination: {
  606. total: number;
  607. current: number;
  608. pageSize: number;
  609. totalPages: number;
  610. };
  611. }
  612. export interface UnreadCountResponse {
  613. count: number;
  614. }
  615. // 消息API
  616. export const MessageAPI = {
  617. // 获取消息列表
  618. getMessages: async (params?: {
  619. page?: number,
  620. pageSize?: number,
  621. type?: MessageType,
  622. status?: MessageStatus
  623. }): Promise<MessagesResponse> => {
  624. try {
  625. const response = await axios.get(`${API_BASE_URL}/messages`, { params });
  626. return response.data;
  627. } catch (error) {
  628. throw error;
  629. }
  630. },
  631. // 获取消息详情
  632. getMessage: async (id: number): Promise<MessageResponse> => {
  633. try {
  634. const response = await axios.get(`${API_BASE_URL}/messages/${id}`);
  635. return response.data;
  636. } catch (error) {
  637. throw error;
  638. }
  639. },
  640. // 发送消息
  641. sendMessage: async (data: {
  642. title: string,
  643. content: string,
  644. type: MessageType,
  645. receiver_ids: number[]
  646. }): Promise<MessageResponse> => {
  647. try {
  648. const response = await axios.post(`${API_BASE_URL}/messages`, data);
  649. return response.data;
  650. } catch (error) {
  651. throw error;
  652. }
  653. },
  654. // 删除消息(软删除)
  655. deleteMessage: async (id: number): Promise<MessageResponse> => {
  656. try {
  657. const response = await axios.delete(`${API_BASE_URL}/messages/${id}`);
  658. return response.data;
  659. } catch (error) {
  660. throw error;
  661. }
  662. },
  663. // 获取未读消息数量
  664. getUnreadCount: async (): Promise<UnreadCountResponse> => {
  665. try {
  666. const response = await axios.get(`${API_BASE_URL}/messages/count/unread`);
  667. return response.data;
  668. } catch (error) {
  669. throw error;
  670. }
  671. },
  672. // 标记消息为已读
  673. markAsRead: async (id: number): Promise<MessageResponse> => {
  674. try {
  675. const response = await axios.post(`${API_BASE_URL}/messages/${id}/read`);
  676. return response.data;
  677. } catch (error) {
  678. throw error;
  679. }
  680. }
  681. };