api.ts 22 KB

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