api.ts 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743
  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, ZichanInfo, ZichanTransLog, DeviceInstance, DeviceType,
  6. AlertHandleLog, AlertNotifyConfig, DeviceAlert, DeviceAlertRule,
  7. DeviceMonitorData, FileLibrary, FileCategory, ZichanCategory, ZichanArea, RackServerType,
  8. RackInfo, RackServer, KnowInfo, WorkOrder, WorkOrderTemplate, WorkOrderLog,
  9. AuthContextType, ThemeContextType, Attachment, ThemeSettings,
  10. CategoryChartData, OnlineRateChartData, StateChartData, StateChartDataWithPercent,
  11. AlarmChartData, CategoryChartDataWithPercent, MapViewDevice, DeviceMapFilter, DeviceMapStats,
  12. DeviceMapDataResponse, DeviceMapStatsResponse, DeviceTreeNode, DeviceTreeStats,
  13. LoginLocation, LoginLocationDetail, SystemSetting, SystemSettingGroupData
  14. } from '../share/types.ts';
  15. import {
  16. DeviceCategory, DeviceStatus, AssetTransferType,
  17. } from '../share/types.ts';
  18. // 定义API基础URL
  19. const API_BASE_URL = '/api';
  20. // 获取OSS完整URL
  21. export const getOssUrl = (path: string): string => {
  22. // 获取全局配置中的OSS_HOST,如果不存在使用默认值
  23. const ossHost = (window.CONFIG?.OSS_BASE_URL) || '';
  24. // 确保path不以/开头
  25. const ossPath = path.startsWith('/') ? path.substring(1) : path;
  26. return `${ossHost}/${ossPath}`;
  27. };
  28. // ===================
  29. // Auth API 定义部分
  30. // ===================
  31. // 定义API返回数据类型
  32. interface AuthLoginResponse {
  33. message: string;
  34. token: string;
  35. refreshToken?: string;
  36. user: User;
  37. }
  38. interface AuthResponse {
  39. message: string;
  40. [key: string]: any;
  41. }
  42. // 定义Auth API接口类型
  43. interface AuthAPIType {
  44. login: (username: string, password: string) => Promise<AuthLoginResponse>;
  45. register: (username: string, email: string, password: string) => Promise<AuthResponse>;
  46. logout: () => Promise<AuthResponse>;
  47. getCurrentUser: () => Promise<User>;
  48. updateUser: (userId: number, userData: Partial<User>) => Promise<User>;
  49. changePassword: (oldPassword: string, newPassword: string) => Promise<AuthResponse>;
  50. requestPasswordReset: (email: string) => Promise<AuthResponse>;
  51. resetPassword: (token: string, newPassword: string) => Promise<AuthResponse>;
  52. }
  53. // Auth相关API
  54. export const AuthAPI: AuthAPIType = {
  55. // 登录API
  56. login: async (username: string, password: string) => {
  57. try {
  58. const response = await axios.post(`${API_BASE_URL}/auth/login`, { username, password });
  59. return response.data;
  60. } catch (error) {
  61. throw error;
  62. }
  63. },
  64. // 注册API
  65. register: async (username: string, email: string, password: string) => {
  66. try {
  67. const response = await axios.post(`${API_BASE_URL}/auth/register`, { username, email, password });
  68. return response.data;
  69. } catch (error) {
  70. throw error;
  71. }
  72. },
  73. // 登出API
  74. logout: async () => {
  75. try {
  76. const response = await axios.post(`${API_BASE_URL}/auth/logout`);
  77. return response.data;
  78. } catch (error) {
  79. throw error;
  80. }
  81. },
  82. // 获取当前用户信息
  83. getCurrentUser: async () => {
  84. try {
  85. const response = await axios.get(`${API_BASE_URL}/auth/me`);
  86. return response.data;
  87. } catch (error) {
  88. throw error;
  89. }
  90. },
  91. // 更新用户信息
  92. updateUser: async (userId: number, userData: Partial<User>) => {
  93. try {
  94. const response = await axios.put(`${API_BASE_URL}/auth/users/${userId}`, userData);
  95. return response.data;
  96. } catch (error) {
  97. throw error;
  98. }
  99. },
  100. // 修改密码
  101. changePassword: async (oldPassword: string, newPassword: string) => {
  102. try {
  103. const response = await axios.post(`${API_BASE_URL}/auth/change-password`, { oldPassword, newPassword });
  104. return response.data;
  105. } catch (error) {
  106. throw error;
  107. }
  108. },
  109. // 请求重置密码
  110. requestPasswordReset: async (email: string) => {
  111. try {
  112. const response = await axios.post(`${API_BASE_URL}/auth/request-password-reset`, { email });
  113. return response.data;
  114. } catch (error) {
  115. throw error;
  116. }
  117. },
  118. // 重置密码
  119. resetPassword: async (token: string, newPassword: string) => {
  120. try {
  121. const response = await axios.post(`${API_BASE_URL}/auth/reset-password`, { token, newPassword });
  122. return response.data;
  123. } catch (error) {
  124. throw error;
  125. }
  126. }
  127. };
  128. // 为UserAPI添加的接口响应类型
  129. interface UsersResponse {
  130. data: User[];
  131. pagination: {
  132. total: number;
  133. current: number;
  134. pageSize: number;
  135. totalPages: number;
  136. };
  137. }
  138. interface UserResponse {
  139. data: User;
  140. message?: string;
  141. }
  142. interface UserCreateResponse {
  143. message: string;
  144. data: User;
  145. }
  146. interface UserUpdateResponse {
  147. message: string;
  148. data: User;
  149. }
  150. interface UserDeleteResponse {
  151. message: string;
  152. id: number;
  153. }
  154. // 用户管理API
  155. export const UserAPI = {
  156. // 获取用户列表
  157. getUsers: async (params?: { page?: number, limit?: number, search?: string }): Promise<UsersResponse> => {
  158. try {
  159. const response = await axios.get(`${API_BASE_URL}/users`, { params });
  160. return response.data;
  161. } catch (error) {
  162. throw error;
  163. }
  164. },
  165. // 获取单个用户详情
  166. getUser: async (userId: number): Promise<UserResponse> => {
  167. try {
  168. const response = await axios.get(`${API_BASE_URL}/users/${userId}`);
  169. return response.data;
  170. } catch (error) {
  171. throw error;
  172. }
  173. },
  174. // 创建用户
  175. createUser: async (userData: Partial<User>): Promise<UserCreateResponse> => {
  176. try {
  177. const response = await axios.post(`${API_BASE_URL}/users`, userData);
  178. return response.data;
  179. } catch (error) {
  180. throw error;
  181. }
  182. },
  183. // 更新用户信息
  184. updateUser: async (userId: number, userData: Partial<User>): Promise<UserUpdateResponse> => {
  185. try {
  186. const response = await axios.put(`${API_BASE_URL}/users/${userId}`, userData);
  187. return response.data;
  188. } catch (error) {
  189. throw error;
  190. }
  191. },
  192. // 删除用户
  193. deleteUser: async (userId: number): Promise<UserDeleteResponse> => {
  194. try {
  195. const response = await axios.delete(`${API_BASE_URL}/users/${userId}`);
  196. return response.data;
  197. } catch (error) {
  198. throw error;
  199. }
  200. }
  201. };
  202. // 资产管理API
  203. export const ZichanAPI = {
  204. // 获取资产列表
  205. getZichanList: async (params?: {
  206. page?: number,
  207. limit?: number,
  208. asset_name?: string,
  209. device_category?: DeviceCategory,
  210. device_status?: DeviceStatus
  211. }) => {
  212. try {
  213. const response = await axios.get(`${API_BASE_URL}/zichan`, { params });
  214. return response.data;
  215. } catch (error) {
  216. throw error;
  217. }
  218. },
  219. // 获取单个资产
  220. getZichan: async (id: number) => {
  221. try {
  222. const response = await axios.get(`${API_BASE_URL}/zichan/${id}`);
  223. return response.data;
  224. } catch (error) {
  225. throw error;
  226. }
  227. },
  228. // 创建资产
  229. createZichan: async (data: Partial<ZichanInfo>) => {
  230. try {
  231. const response = await axios.post(`${API_BASE_URL}/zichan`, data);
  232. return response.data;
  233. } catch (error) {
  234. throw error;
  235. }
  236. },
  237. // 更新资产
  238. updateZichan: async (id: number, data: Partial<ZichanInfo>) => {
  239. try {
  240. const response = await axios.put(`${API_BASE_URL}/zichan/${id}`, data);
  241. return response.data;
  242. } catch (error) {
  243. throw error;
  244. }
  245. },
  246. // 删除资产
  247. deleteZichan: async (id: number) => {
  248. try {
  249. const response = await axios.delete(`${API_BASE_URL}/zichan/${id}`);
  250. return response.data;
  251. } catch (error) {
  252. throw error;
  253. }
  254. }
  255. };
  256. // 资产流转API接口定义
  257. export const ZichanTransferAPI = {
  258. // 获取资产流转记录列表
  259. getTransferList: async (params?: { page?: number, limit?: number, asset_id?: number, asset_transfer?: AssetTransferType }) => {
  260. try {
  261. const response = await axios.get(`${API_BASE_URL}/zichan-transfer`, { params });
  262. return response.data;
  263. } catch (error) {
  264. throw error;
  265. }
  266. },
  267. // 获取资产流转记录详情
  268. getTransfer: async (id: number) => {
  269. try {
  270. const response = await axios.get(`${API_BASE_URL}/zichan-transfer/${id}`);
  271. return response.data;
  272. } catch (error) {
  273. throw error;
  274. }
  275. },
  276. // 创建资产流转记录
  277. createTransfer: async (data: Partial<ZichanTransLog>) => {
  278. try {
  279. const response = await axios.post(`${API_BASE_URL}/zichan-transfer`, data);
  280. return response.data;
  281. } catch (error) {
  282. throw error;
  283. }
  284. },
  285. // 更新资产流转记录
  286. updateTransfer: async (id: number, data: Partial<ZichanTransLog>) => {
  287. try {
  288. const response = await axios.put(`${API_BASE_URL}/zichan-transfer/${id}`, data);
  289. return response.data;
  290. } catch (error) {
  291. throw error;
  292. }
  293. },
  294. // 删除资产流转记录
  295. deleteTransfer: async (id: number) => {
  296. try {
  297. const response = await axios.delete(`${API_BASE_URL}/zichan-transfer/${id}`);
  298. return response.data;
  299. } catch (error) {
  300. throw error;
  301. }
  302. }
  303. };
  304. // 设备实例API接口定义
  305. interface DeviceInstancesResponse {
  306. data: DeviceInstance[];
  307. pagination: {
  308. total: number;
  309. current: number;
  310. pageSize: number;
  311. totalPages: number;
  312. };
  313. }
  314. interface DeviceInstanceResponse {
  315. data: DeviceInstance;
  316. asset_info?: ZichanInfo;
  317. type_info?: DeviceType;
  318. message?: string;
  319. }
  320. interface DeviceInstanceCreateResponse {
  321. message: string;
  322. data: DeviceInstance;
  323. }
  324. interface DeviceInstanceUpdateResponse {
  325. message: string;
  326. data: DeviceInstance;
  327. }
  328. interface DeviceInstanceDeleteResponse {
  329. message: string;
  330. id: number;
  331. }
  332. // 设备类型API接口定义
  333. interface DeviceTypeResponse {
  334. data: DeviceType[];
  335. pagination: {
  336. total: number;
  337. current: number;
  338. pageSize: number;
  339. totalPages: number;
  340. };
  341. }
  342. interface DeviceTypeDetailResponse {
  343. data: DeviceType;
  344. message?: string;
  345. }
  346. interface DeviceTypeCreateResponse {
  347. message: string;
  348. data: DeviceType;
  349. }
  350. interface DeviceTypeUpdateResponse {
  351. message: string;
  352. data: DeviceType;
  353. }
  354. interface DeviceTypeDeleteResponse {
  355. message: string;
  356. id: number;
  357. }
  358. export const DeviceTypeAPI = {
  359. // 获取设备类型列表
  360. getDeviceTypes: async (params?: {
  361. page?: number,
  362. pageSize?: number,
  363. code?: string,
  364. name?: string,
  365. is_enabled?: boolean
  366. }): Promise<DeviceTypeResponse> => {
  367. try {
  368. const response = await axios.get('/api/device/types', { params });
  369. return response.data;
  370. } catch (error) {
  371. throw error;
  372. }
  373. },
  374. // 获取单个设备类型信息
  375. getDeviceType: async (id: number): Promise<DeviceTypeDetailResponse> => {
  376. try {
  377. const response = await axios.get(`/api/device/types/${id}`);
  378. return response.data;
  379. } catch (error) {
  380. throw error;
  381. }
  382. },
  383. // 创建设备类型
  384. createDeviceType: async (data: Partial<DeviceType>): Promise<DeviceTypeCreateResponse> => {
  385. try {
  386. const response = await axios.post('/api/device/types', data);
  387. return response.data;
  388. } catch (error) {
  389. throw error;
  390. }
  391. },
  392. // 更新设备类型
  393. updateDeviceType: async (id: number, data: Partial<DeviceType>): Promise<DeviceTypeUpdateResponse> => {
  394. try {
  395. const response = await axios.put(`/api/device/types/${id}`, data);
  396. return response.data;
  397. } catch (error) {
  398. throw error;
  399. }
  400. },
  401. // 删除设备类型
  402. deleteDeviceType: async (id: number): Promise<DeviceTypeDeleteResponse> => {
  403. try {
  404. const response = await axios.delete(`/api/device/types/${id}`);
  405. return response.data;
  406. } catch (error) {
  407. throw error;
  408. }
  409. },
  410. // 获取设备类型图标
  411. getTypeIcons: async (): Promise<{data: Record<string, string>, success: boolean}> => {
  412. try {
  413. const response = await axios.get('/api/device/types/icons');
  414. return response.data;
  415. } catch (error) {
  416. throw error;
  417. }
  418. }
  419. };
  420. export const DeviceInstanceAPI = {
  421. // 获取设备实例列表
  422. getDeviceInstances: async (params?: {
  423. page?: number,
  424. limit?: number,
  425. type_id?: number,
  426. protocol?: string,
  427. status?: number
  428. }): Promise<DeviceInstancesResponse> => {
  429. try {
  430. const response = await axios.get('/api/device/instances', { params });
  431. return response.data;
  432. } catch (error) {
  433. throw error;
  434. }
  435. },
  436. // 获取单个设备实例信息
  437. getDeviceInstance: async (id: number): Promise<DeviceInstanceResponse> => {
  438. try {
  439. const response = await axios.get(`/api/device/instances/${id}`);
  440. return response.data;
  441. } catch (error) {
  442. throw error;
  443. }
  444. },
  445. // 创建设备实例
  446. createDeviceInstance: async (data: Partial<DeviceInstance>): Promise<DeviceInstanceCreateResponse> => {
  447. try {
  448. const response = await axios.post('/api/device/instances', data);
  449. return response.data;
  450. } catch (error) {
  451. throw error;
  452. }
  453. },
  454. // 更新设备实例
  455. updateDeviceInstance: async (id: number, data: Partial<DeviceInstance>): Promise<DeviceInstanceUpdateResponse> => {
  456. try {
  457. const response = await axios.put(`/api/device/instances/${id}`, data);
  458. return response.data;
  459. } catch (error) {
  460. throw error;
  461. }
  462. },
  463. // 删除设备实例
  464. deleteDeviceInstance: async (id: number): Promise<DeviceInstanceDeleteResponse> => {
  465. try {
  466. const response = await axios.delete(`/api/device/instances/${id}`);
  467. return response.data;
  468. } catch (error) {
  469. throw error;
  470. }
  471. }
  472. };
  473. // 机柜管理API接口定义
  474. export const RackAPI = {
  475. // 获取机柜列表
  476. getRackList: async (params?: {
  477. page?: number,
  478. limit?: number,
  479. rack_name?: string,
  480. rack_code?: string,
  481. area?: string
  482. }) => {
  483. try {
  484. const response = await axios.get(`${API_BASE_URL}/racks`, { params });
  485. return response.data;
  486. } catch (error) {
  487. throw error;
  488. }
  489. },
  490. // 获取单个机柜信息
  491. getRack: async (id: number) => {
  492. try {
  493. const response = await axios.get(`${API_BASE_URL}/racks/${id}`);
  494. return response.data;
  495. } catch (error) {
  496. throw error;
  497. }
  498. },
  499. // 创建机柜
  500. createRack: async (data: Partial<RackInfo>) => {
  501. try {
  502. const response = await axios.post(`${API_BASE_URL}/racks`, data);
  503. return response.data;
  504. } catch (error) {
  505. throw error;
  506. }
  507. },
  508. // 更新机柜
  509. updateRack: async (id: number, data: Partial<RackInfo>) => {
  510. try {
  511. const response = await axios.put(`${API_BASE_URL}/racks/${id}`, data);
  512. return response.data;
  513. } catch (error) {
  514. throw error;
  515. }
  516. },
  517. // 删除机柜
  518. deleteRack: async (id: number) => {
  519. try {
  520. const response = await axios.delete(`${API_BASE_URL}/racks/${id}`);
  521. return response.data;
  522. } catch (error) {
  523. throw error;
  524. }
  525. }
  526. };
  527. // 机柜服务器API接口定义
  528. export const RackServerAPI = {
  529. // 获取机柜服务器列表
  530. getRackServerList: async (params?: {
  531. page?: number,
  532. limit?: number,
  533. rack_id?: number,
  534. asset_id?: number,
  535. server_type?: string
  536. }) => {
  537. try {
  538. const response = await axios.get(`${API_BASE_URL}/rack-servers`, { params });
  539. return response.data;
  540. } catch (error) {
  541. throw error;
  542. }
  543. },
  544. // 获取单个机柜服务器信息
  545. getRackServer: async (id: number) => {
  546. try {
  547. const response = await axios.get(`${API_BASE_URL}/rack-servers/${id}`);
  548. return response.data;
  549. } catch (error) {
  550. throw error;
  551. }
  552. },
  553. // 创建机柜服务器
  554. createRackServer: async (data: Partial<RackServer>) => {
  555. try {
  556. const response = await axios.post(`${API_BASE_URL}/rack-servers`, data);
  557. return response.data;
  558. } catch (error) {
  559. throw error;
  560. }
  561. },
  562. // 更新机柜服务器
  563. updateRackServer: async (id: number, data: Partial<RackServer>) => {
  564. try {
  565. const response = await axios.put(`${API_BASE_URL}/rack-servers/${id}`, data);
  566. return response.data;
  567. } catch (error) {
  568. throw error;
  569. }
  570. },
  571. // 删除机柜服务器
  572. deleteRackServer: async (id: number) => {
  573. try {
  574. const response = await axios.delete(`${API_BASE_URL}/rack-servers/${id}`);
  575. return response.data;
  576. } catch (error) {
  577. throw error;
  578. }
  579. }
  580. };
  581. interface DeviceMonitorDataResponse {
  582. data: DeviceMonitorData[];
  583. total: number;
  584. page: number;
  585. pageSize: number;
  586. }
  587. interface DeviceMonitorResponse {
  588. data: DeviceMonitorData;
  589. message?: string;
  590. }
  591. interface MonitorCreateResponse {
  592. data: DeviceMonitorData;
  593. message: string;
  594. }
  595. interface MonitorUpdateResponse {
  596. data: DeviceMonitorData;
  597. message: string;
  598. }
  599. interface MonitorDeleteResponse {
  600. message: string;
  601. }
  602. // 告警相关响应类型
  603. interface DeviceAlertDataResponse {
  604. data: DeviceAlert[];
  605. total: number;
  606. page: number;
  607. pageSize: number;
  608. }
  609. interface DeviceAlertResponse {
  610. data: DeviceAlert;
  611. message?: string;
  612. }
  613. interface AlertCreateResponse {
  614. data: DeviceAlert;
  615. message: string;
  616. }
  617. interface AlertUpdateResponse {
  618. data: DeviceAlert;
  619. message: string;
  620. }
  621. interface AlertDeleteResponse {
  622. message: string;
  623. }
  624. // 告警处理相关响应类型
  625. interface AlertHandleDataResponse {
  626. data: AlertHandleLog[];
  627. total: number;
  628. page: number;
  629. pageSize: number;
  630. }
  631. interface AlertHandleResponse {
  632. data: AlertHandleLog;
  633. message?: string;
  634. }
  635. // 告警通知配置相关响应类型
  636. interface AlertNotifyConfigDataResponse {
  637. data: AlertNotifyConfig[];
  638. total: number;
  639. page: number;
  640. pageSize: number;
  641. }
  642. interface AlertNotifyConfigResponse {
  643. data: AlertNotifyConfig;
  644. message?: string;
  645. }
  646. // 监控API接口定义
  647. export const MonitorAPI = {
  648. // 获取监控数据
  649. getMonitorData: async (params?: {
  650. page?: number,
  651. limit?: number,
  652. device_id?: number,
  653. device_type?: string,
  654. start_time?: string,
  655. end_time?: string
  656. }): Promise<DeviceMonitorDataResponse> => {
  657. try {
  658. const response = await axios.get(`${API_BASE_URL}/monitor/data`, { params });
  659. return response.data;
  660. } catch (error) {
  661. throw error;
  662. }
  663. },
  664. // 获取设备树数据
  665. getDeviceTree: async (params?: {
  666. status?: string,
  667. keyword?: string
  668. }): Promise<{ data: DeviceTreeNode[] }> => {
  669. try {
  670. const response = await axios.get(`${API_BASE_URL}/monitor/devices/tree`, { params });
  671. return response.data;
  672. } catch (error) {
  673. throw error;
  674. }
  675. },
  676. // 获取设备树统计数据
  677. getDeviceTreeStats: async (): Promise<{ data: DeviceTreeStats }> => {
  678. try {
  679. const response = await axios.get(`${API_BASE_URL}/monitor/devices/tree/statistics`);
  680. return response.data;
  681. } catch (error) {
  682. throw error;
  683. }
  684. },
  685. // 获取设备地图数据
  686. getDeviceMapData: async (params?: {
  687. type_code?: string,
  688. device_status?: DeviceStatus,
  689. keyword?: string,
  690. device_id?: number
  691. }): Promise<{ data: MapViewDevice[], stats: DeviceMapStats }> => {
  692. try {
  693. const response = await axios.get(`${API_BASE_URL}/monitor/devices/map`, { params });
  694. return response.data;
  695. } catch (error) {
  696. throw error;
  697. }
  698. },
  699. // 获取单个监控数据
  700. getMonitor: async (id: number): Promise<DeviceMonitorResponse> => {
  701. try {
  702. const response = await axios.get(`${API_BASE_URL}/monitor/data/${id}`);
  703. return response.data;
  704. } catch (error) {
  705. throw error;
  706. }
  707. },
  708. // 创建监控数据
  709. createMonitor: async (data: Partial<DeviceMonitorData>): Promise<MonitorCreateResponse> => {
  710. try {
  711. const response = await axios.post(`${API_BASE_URL}/monitor/data`, data);
  712. return response.data;
  713. } catch (error) {
  714. throw error;
  715. }
  716. },
  717. // 更新监控数据
  718. updateMonitor: async (id: number, data: Partial<DeviceMonitorData>): Promise<MonitorUpdateResponse> => {
  719. try {
  720. const response = await axios.put(`${API_BASE_URL}/monitor/data/${id}`, data);
  721. return response.data;
  722. } catch (error) {
  723. throw error;
  724. }
  725. },
  726. // 删除监控数据
  727. deleteMonitor: async (id: number): Promise<MonitorDeleteResponse> => {
  728. try {
  729. const response = await axios.delete(`${API_BASE_URL}/monitor/data/${id}`);
  730. return response.data;
  731. } catch (error) {
  732. throw error;
  733. }
  734. }
  735. };
  736. // 告警API接口定义
  737. export const AlertAPI = {
  738. // 获取告警数据
  739. getAlertData: async (params?: {
  740. page?: number,
  741. limit?: number,
  742. alert_type?: string,
  743. alert_level?: string,
  744. start_time?: string,
  745. end_time?: string
  746. }): Promise<DeviceAlertDataResponse> => {
  747. try {
  748. const response = await axios.get(`${API_BASE_URL}/alerts`, { params });
  749. return response.data;
  750. } catch (error) {
  751. throw error;
  752. }
  753. },
  754. // 获取单个告警数据
  755. getAlert: async (id: number): Promise<DeviceAlert> => {
  756. try {
  757. const response = await axios.get(`${API_BASE_URL}/alerts/${id}`);
  758. return response.data;
  759. } catch (error) {
  760. throw error;
  761. }
  762. },
  763. // 创建告警数据
  764. createAlert: async (data: Partial<DeviceAlert>): Promise<AlertCreateResponse> => {
  765. try {
  766. const response = await axios.post(`${API_BASE_URL}/alerts`, data);
  767. return response.data;
  768. } catch (error) {
  769. throw error;
  770. }
  771. },
  772. // 更新告警数据
  773. updateAlert: async (id: number, data: Partial<DeviceAlert>): Promise<AlertUpdateResponse> => {
  774. try {
  775. const response = await axios.put(`${API_BASE_URL}/alerts/${id}`, data);
  776. return response.data;
  777. } catch (error) {
  778. throw error;
  779. }
  780. },
  781. // 删除告警数据
  782. deleteAlert: async (id: number): Promise<AlertDeleteResponse> => {
  783. try {
  784. const response = await axios.delete(`${API_BASE_URL}/alerts/${id}`);
  785. return response.data;
  786. } catch (error) {
  787. throw error;
  788. }
  789. }
  790. };
  791. // 告警处理API接口定义
  792. export const AlertHandleAPI = {
  793. // 获取告警处理数据
  794. getAlertHandleData: async (params?: {
  795. page?: number,
  796. limit?: number,
  797. alert_id?: number,
  798. handle_type?: string,
  799. start_time?: string,
  800. end_time?: string
  801. }): Promise<AlertHandleDataResponse> => {
  802. try {
  803. const response = await axios.get(`${API_BASE_URL}/alert-handles`, { params });
  804. return response.data;
  805. } catch (error) {
  806. throw error;
  807. }
  808. },
  809. // 获取单个告警处理数据
  810. getAlertHandle: async (id: number): Promise<AlertHandleResponse> => {
  811. try {
  812. const response = await axios.get(`${API_BASE_URL}/alert-handles/${id}`);
  813. return response.data;
  814. } catch (error) {
  815. throw error;
  816. }
  817. },
  818. // 创建告警处理数据
  819. createAlertHandle: async (data: Partial<AlertHandleLog>) => {
  820. try {
  821. const response = await axios.post(`${API_BASE_URL}/alert-handles`, data);
  822. return response.data;
  823. } catch (error) {
  824. throw error;
  825. }
  826. },
  827. // 更新告警处理数据
  828. updateAlertHandle: async (id: number, data: Partial<AlertHandleLog>) => {
  829. try {
  830. const response = await axios.put(`${API_BASE_URL}/alert-handles/${id}`, data);
  831. return response.data;
  832. } catch (error) {
  833. throw error;
  834. }
  835. },
  836. // 删除告警处理数据
  837. deleteAlertHandle: async (id: number) => {
  838. try {
  839. const response = await axios.delete(`${API_BASE_URL}/alert-handles/${id}`);
  840. return response.data;
  841. } catch (error) {
  842. throw error;
  843. }
  844. }
  845. };
  846. // 告警通知配置API接口定义
  847. export const AlertNotifyConfigAPI = {
  848. // 获取告警通知配置
  849. getAlertNotifyConfig: async (params?: {
  850. page?: number,
  851. limit?: number,
  852. device_id?: number,
  853. alert_level?: string
  854. }): Promise<AlertNotifyConfigDataResponse> => {
  855. try {
  856. const response = await axios.get(`${API_BASE_URL}/alert-notify-configs`, { params });
  857. return response.data;
  858. } catch (error) {
  859. throw error;
  860. }
  861. },
  862. // 创建告警通知配置
  863. createAlertNotifyConfig: async (data: Partial<AlertNotifyConfig>): Promise<AlertNotifyConfigResponse> => {
  864. try {
  865. const response = await axios.post(`${API_BASE_URL}/alert-notify-configs`, data);
  866. return response.data;
  867. } catch (error) {
  868. throw error;
  869. }
  870. },
  871. // 更新告警通知配置
  872. updateAlertNotifyConfig: async (id: number, data: Partial<AlertNotifyConfig>): Promise<AlertNotifyConfigResponse> => {
  873. try {
  874. const response = await axios.put(`${API_BASE_URL}/alert-notify-configs/${id}`, data);
  875. return response.data;
  876. } catch (error) {
  877. throw error;
  878. }
  879. },
  880. // 删除告警通知配置
  881. deleteAlertNotifyConfig: async (id: number): Promise<AlertNotifyConfigResponse> => {
  882. try {
  883. const response = await axios.delete(`${API_BASE_URL}/alert-notify-configs/${id}`);
  884. return response.data;
  885. } catch (error) {
  886. throw error;
  887. }
  888. }
  889. };
  890. // 设备告警规则API接口定义
  891. export const DeviceAlertRuleAPI = {
  892. // 获取设备告警规则
  893. getDeviceAlertRules: async (params?: {
  894. page?: number,
  895. limit?: number,
  896. device_id?: number,
  897. rule_type?: string
  898. }): Promise<DeviceAlertDataResponse> => {
  899. try {
  900. const response = await axios.get(`${API_BASE_URL}/device-alert-rules`, { params });
  901. return response.data;
  902. } catch (error) {
  903. throw error;
  904. }
  905. },
  906. // 获取单个设备告警规则
  907. getDeviceAlertRule: async (id: number): Promise<DeviceAlertResponse> => {
  908. try {
  909. const response = await axios.get(`${API_BASE_URL}/device-alert-rules/${id}`);
  910. return response.data;
  911. } catch (error) {
  912. throw error;
  913. }
  914. },
  915. // 创建设备告警规则
  916. createDeviceAlertRule: async (data: Partial<DeviceAlertRule>): Promise<DeviceAlertResponse> => {
  917. try {
  918. const response = await axios.post(`${API_BASE_URL}/device-alert-rules`, data);
  919. return response.data;
  920. } catch (error) {
  921. throw error;
  922. }
  923. },
  924. // 更新设备告警规则
  925. updateDeviceAlertRule: async (id: number, data: Partial<DeviceAlertRule>): Promise<DeviceAlertResponse> => {
  926. try {
  927. const response = await axios.put(`${API_BASE_URL}/device-alert-rules/${id}`, data);
  928. return response.data;
  929. } catch (error) {
  930. throw error;
  931. }
  932. },
  933. // 删除设备告警规则
  934. deleteDeviceAlertRule: async (id: number): Promise<AlertDeleteResponse> => {
  935. try {
  936. const response = await axios.delete(`${API_BASE_URL}/device-alert-rules/${id}`);
  937. return response.data;
  938. } catch (error) {
  939. throw error;
  940. }
  941. }
  942. };
  943. // 资产分类API响应类型
  944. interface ZichanCategoryResponse {
  945. data: ZichanCategory[];
  946. pagination: {
  947. total: number;
  948. current: number;
  949. pageSize: number;
  950. };
  951. }
  952. // 资产归属区域API响应类型
  953. interface ZichanAreaResponse {
  954. data: ZichanArea[];
  955. pagination: {
  956. total: number;
  957. current: number;
  958. pageSize: number;
  959. };
  960. }
  961. // 机柜服务器类型API响应类型
  962. interface RackServerTypeResponse {
  963. data: RackServerType[];
  964. pagination: {
  965. total: number;
  966. current: number;
  967. pageSize: number;
  968. };
  969. }
  970. // 资产分类API接口定义
  971. export const ZichanCategoryAPI = {
  972. // 获取资产分类列表
  973. getZichanCategoryList: async (params?: {
  974. page?: number,
  975. limit?: number,
  976. name?: string,
  977. code?: string
  978. }) => {
  979. try {
  980. const response = await axios.get(`${API_BASE_URL}/zichan-categories`, { params });
  981. return response.data;
  982. } catch (error) {
  983. throw error;
  984. }
  985. },
  986. // 获取单个资产分类信息
  987. getZichanCategory: async (id: number) => {
  988. try {
  989. const response = await axios.get(`${API_BASE_URL}/zichan-categories/${id}`);
  990. return response.data;
  991. } catch (error) {
  992. throw error;
  993. }
  994. },
  995. // 创建资产分类
  996. createZichanCategory: async (data: Partial<ZichanCategory>) => {
  997. try {
  998. const response = await axios.post(`${API_BASE_URL}/zichan-categories`, data);
  999. return response.data;
  1000. } catch (error) {
  1001. throw error;
  1002. }
  1003. },
  1004. // 更新资产分类
  1005. updateZichanCategory: async (id: number, data: Partial<ZichanCategory>) => {
  1006. try {
  1007. const response = await axios.put(`${API_BASE_URL}/zichan-categories/${id}`, data);
  1008. return response.data;
  1009. } catch (error) {
  1010. throw error;
  1011. }
  1012. },
  1013. // 删除资产分类
  1014. deleteZichanCategory: async (id: number) => {
  1015. try {
  1016. const response = await axios.delete(`${API_BASE_URL}/zichan-categories/${id}`);
  1017. return response.data;
  1018. } catch (error) {
  1019. throw error;
  1020. }
  1021. }
  1022. };
  1023. // 资产归属区域API接口定义
  1024. export const ZichanAreaAPI = {
  1025. // 获取资产归属区域列表
  1026. getZichanAreaList: async (params?: {
  1027. page?: number,
  1028. limit?: number,
  1029. name?: string,
  1030. code?: string
  1031. }) => {
  1032. try {
  1033. const response = await axios.get(`${API_BASE_URL}/zichan-areas`, { params });
  1034. return response.data;
  1035. } catch (error) {
  1036. throw error;
  1037. }
  1038. },
  1039. // 获取单个资产归属区域信息
  1040. getZichanArea: async (id: number) => {
  1041. try {
  1042. const response = await axios.get(`${API_BASE_URL}/zichan-areas/${id}`);
  1043. return response.data;
  1044. } catch (error) {
  1045. throw error;
  1046. }
  1047. },
  1048. // 创建资产归属区域
  1049. createZichanArea: async (data: Partial<ZichanArea>) => {
  1050. try {
  1051. const response = await axios.post(`${API_BASE_URL}/zichan-areas`, data);
  1052. return response.data;
  1053. } catch (error) {
  1054. throw error;
  1055. }
  1056. },
  1057. // 更新资产归属区域
  1058. updateZichanArea: async (id: number, data: Partial<ZichanArea>) => {
  1059. try {
  1060. const response = await axios.put(`${API_BASE_URL}/zichan-areas/${id}`, data);
  1061. return response.data;
  1062. } catch (error) {
  1063. throw error;
  1064. }
  1065. },
  1066. // 删除资产归属区域
  1067. deleteZichanArea: async (id: number) => {
  1068. try {
  1069. const response = await axios.delete(`${API_BASE_URL}/zichan-areas/${id}`);
  1070. return response.data;
  1071. } catch (error) {
  1072. throw error;
  1073. }
  1074. }
  1075. };
  1076. // 机柜服务器类型API接口定义
  1077. export const RackServerTypeAPI = {
  1078. // 获取机柜服务器类型列表
  1079. getRackServerTypeList: async (params?: {
  1080. page?: number,
  1081. limit?: number,
  1082. name?: string,
  1083. code?: string
  1084. }): Promise<RackServerTypeResponse> => {
  1085. try {
  1086. const response = await axios.get(`${API_BASE_URL}/rack-server-types`, { params });
  1087. return response.data;
  1088. } catch (error) {
  1089. throw error;
  1090. }
  1091. },
  1092. // 获取单个机柜服务器类型信息
  1093. getRackServerType: async (id: number) => {
  1094. try {
  1095. const response = await axios.get(`${API_BASE_URL}/rack-server-types/${id}`);
  1096. return response.data;
  1097. } catch (error) {
  1098. throw error;
  1099. }
  1100. },
  1101. // 创建机柜服务器类型
  1102. createRackServerType: async (data: Partial<RackServerType>) => {
  1103. try {
  1104. const response = await axios.post(`${API_BASE_URL}/rack-server-types`, data);
  1105. return response.data;
  1106. } catch (error) {
  1107. throw error;
  1108. }
  1109. },
  1110. // 更新机柜服务器类型
  1111. updateRackServerType: async (id: number, data: Partial<RackServerType>) => {
  1112. try {
  1113. const response = await axios.put(`${API_BASE_URL}/rack-server-types/${id}`, data);
  1114. return response.data;
  1115. } catch (error) {
  1116. throw error;
  1117. }
  1118. },
  1119. // 删除机柜服务器类型
  1120. deleteRackServerType: async (id: number) => {
  1121. try {
  1122. const response = await axios.delete(`${API_BASE_URL}/rack-server-types/${id}`);
  1123. return response.data;
  1124. } catch (error) {
  1125. throw error;
  1126. }
  1127. }
  1128. };
  1129. // 定义文件相关接口类型
  1130. interface FileUploadPolicyResponse {
  1131. message: string;
  1132. data: MinioUploadPolicy | OSSUploadPolicy;
  1133. }
  1134. interface FileListResponse {
  1135. message: string;
  1136. data: {
  1137. list: FileLibrary[];
  1138. pagination: {
  1139. current: number;
  1140. pageSize: number;
  1141. total: number;
  1142. };
  1143. };
  1144. }
  1145. interface FileSaveResponse {
  1146. message: string;
  1147. data: FileLibrary;
  1148. }
  1149. interface FileInfoResponse {
  1150. message: string;
  1151. data: FileLibrary;
  1152. }
  1153. interface FileDeleteResponse {
  1154. message: string;
  1155. }
  1156. interface FileCategoryListResponse {
  1157. data: FileCategory[];
  1158. total: number;
  1159. page: number;
  1160. pageSize: number;
  1161. }
  1162. interface FileCategoryCreateResponse {
  1163. message: string;
  1164. data: FileCategory;
  1165. }
  1166. interface FileCategoryUpdateResponse {
  1167. message: string;
  1168. data: FileCategory;
  1169. }
  1170. interface FileCategoryDeleteResponse {
  1171. message: string;
  1172. }
  1173. // 文件API接口定义
  1174. export const FileAPI = {
  1175. // 获取文件上传策略
  1176. getUploadPolicy: async (filename: string, prefix: string = 'uploads/', maxSize: number = 10 * 1024 * 1024): Promise<FileUploadPolicyResponse> => {
  1177. try {
  1178. const response = await axios.get(`${API_BASE_URL}/upload/policy`, {
  1179. params: { filename, prefix, maxSize }
  1180. });
  1181. return response.data;
  1182. } catch (error) {
  1183. throw error;
  1184. }
  1185. },
  1186. // 保存文件信息
  1187. saveFileInfo: async (fileData: Partial<FileLibrary>): Promise<FileSaveResponse> => {
  1188. try {
  1189. const response = await axios.post(`${API_BASE_URL}/upload/save`, fileData);
  1190. return response.data;
  1191. } catch (error) {
  1192. throw error;
  1193. }
  1194. },
  1195. // 获取文件列表
  1196. getFileList: async (params?: {
  1197. page?: number,
  1198. pageSize?: number,
  1199. category_id?: number,
  1200. fileType?: string,
  1201. keyword?: string
  1202. }): Promise<FileListResponse> => {
  1203. try {
  1204. const response = await axios.get(`${API_BASE_URL}/upload/list`, { params });
  1205. return response.data;
  1206. } catch (error) {
  1207. throw error;
  1208. }
  1209. },
  1210. // 获取单个文件信息
  1211. getFileInfo: async (id: number): Promise<FileInfoResponse> => {
  1212. try {
  1213. const response = await axios.get(`${API_BASE_URL}/upload/${id}`);
  1214. return response.data;
  1215. } catch (error) {
  1216. throw error;
  1217. }
  1218. },
  1219. // 更新文件下载计数
  1220. updateDownloadCount: async (id: number): Promise<FileDeleteResponse> => {
  1221. try {
  1222. const response = await axios.post(`${API_BASE_URL}/upload/${id}/download`);
  1223. return response.data;
  1224. } catch (error) {
  1225. throw error;
  1226. }
  1227. },
  1228. // 删除文件
  1229. deleteFile: async (id: number): Promise<FileDeleteResponse> => {
  1230. try {
  1231. const response = await axios.delete(`${API_BASE_URL}/upload/${id}`);
  1232. return response.data;
  1233. } catch (error) {
  1234. throw error;
  1235. }
  1236. },
  1237. // 获取文件分类列表
  1238. getCategories: async (params?: {
  1239. page?: number,
  1240. pageSize?: number,
  1241. search?: string
  1242. }): Promise<FileCategoryListResponse> => {
  1243. try {
  1244. const response = await axios.get(`${API_BASE_URL}/file-categories`, { params });
  1245. return response.data;
  1246. } catch (error) {
  1247. throw error;
  1248. }
  1249. },
  1250. // 创建文件分类
  1251. createCategory: async (data: Partial<FileCategory>): Promise<FileCategoryCreateResponse> => {
  1252. try {
  1253. const response = await axios.post(`${API_BASE_URL}/file-categories`, data);
  1254. return response.data;
  1255. } catch (error) {
  1256. throw error;
  1257. }
  1258. },
  1259. // 更新文件分类
  1260. updateCategory: async (id: number, data: Partial<FileCategory>): Promise<FileCategoryUpdateResponse> => {
  1261. try {
  1262. const response = await axios.put(`${API_BASE_URL}/file-categories/${id}`, data);
  1263. return response.data;
  1264. } catch (error) {
  1265. throw error;
  1266. }
  1267. },
  1268. // 删除文件分类
  1269. deleteCategory: async (id: number): Promise<FileCategoryDeleteResponse> => {
  1270. try {
  1271. const response = await axios.delete(`${API_BASE_URL}/file-categories/${id}`);
  1272. return response.data;
  1273. } catch (error) {
  1274. throw error;
  1275. }
  1276. }
  1277. };
  1278. // // 添加图表类型定义(从大屏移植)
  1279. // interface CategoryChartData {
  1280. // 设备分类: string;
  1281. // 设备数: number;
  1282. // }
  1283. // interface CategoryChartDataWithPercent extends CategoryChartData {
  1284. // 百分比: string;
  1285. // }
  1286. // interface OnlineRateChartData {
  1287. // time_interval: string;
  1288. // online_devices: number;
  1289. // total_devices: number;
  1290. // }
  1291. // interface StateChartData {
  1292. // 资产流转: string;
  1293. // 设备数: number;
  1294. // }
  1295. // interface StateChartDataWithPercent extends StateChartData {
  1296. // 百分比: string;
  1297. // }
  1298. // interface AlarmChartData {
  1299. // time_interval: string;
  1300. // total_devices: number;
  1301. // }
  1302. // 统一图表数据查询函数
  1303. export const chartQueryFns = {
  1304. // 资产分类数据查询
  1305. fetchCategoryData: async (): Promise<CategoryChartDataWithPercent[]> => {
  1306. const res = await axios.get<CategoryChartData[]>(`${API_BASE_URL}/big/zichan_category_chart`);
  1307. // 预先计算百分比
  1308. const data = res.data;
  1309. const total = data.reduce((sum: number, item: CategoryChartData) => sum + item['设备数'], 0);
  1310. // 为每个数据项添加百分比字段
  1311. return data.map(item => ({
  1312. ...item,
  1313. 百分比: total > 0 ? (item['设备数'] / total * 100).toFixed(1) : '0'
  1314. }));
  1315. },
  1316. // 在线率变化数据查询
  1317. fetchOnlineRateData: async (params?: {
  1318. created_at_gte?: string;
  1319. created_at_lte?: string;
  1320. dimension?: string;
  1321. }): Promise<OnlineRateChartData[]> => {
  1322. // 可选参数
  1323. // const params = {
  1324. // created_at_gte: dayjs().subtract(7, 'day').format('YYYY-MM-DD HH:mm:ss'),
  1325. // created_at_lte: dayjs().format('YYYY-MM-DD HH:mm:ss'),
  1326. // dimension: 'day'
  1327. // };
  1328. const res = await axios.get<OnlineRateChartData[]>(`${API_BASE_URL}/big/zichan_online_rate_chart`, { params });
  1329. return res.data;
  1330. },
  1331. // 资产流转状态数据查询
  1332. fetchStateData: async (): Promise<StateChartDataWithPercent[]> => {
  1333. const res = await axios.get<StateChartData[]>(`${API_BASE_URL}/big/zichan_state_chart`);
  1334. // 预先计算百分比
  1335. const data = res.data;
  1336. const total = data.reduce((sum: number, item: StateChartData) => sum + item['设备数'], 0);
  1337. // 为每个数据项添加百分比字段
  1338. return data.map(item => ({
  1339. ...item,
  1340. 百分比: total > 0 ? (item['设备数'] / total * 100).toFixed(1) : '0'
  1341. }));
  1342. },
  1343. // 告警数据变化查询
  1344. fetchAlarmData: async (params?: {
  1345. created_at_gte?: string;
  1346. created_at_lte?: string;
  1347. dimension?: string;
  1348. }): Promise<AlarmChartData[]> => {
  1349. // 可选参数
  1350. // const params = {
  1351. // created_at_gte: dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
  1352. // created_at_lte: dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss'),
  1353. // dimension: 'hour'
  1354. // };
  1355. const res = await axios.get<AlarmChartData[]>(`${API_BASE_URL}/big/zichan_alarm_chart`, { params });
  1356. return res.data;
  1357. }
  1358. };
  1359. // Theme API 响应类型
  1360. export interface ThemeSettingsResponse {
  1361. message: string;
  1362. data: ThemeSettings;
  1363. }
  1364. // Theme API 定义
  1365. export const ThemeAPI = {
  1366. // 获取主题设置
  1367. getThemeSettings: async (): Promise<ThemeSettings> => {
  1368. try {
  1369. const response = await axios.get(`${API_BASE_URL}/theme`);
  1370. return response.data.data;
  1371. } catch (error) {
  1372. throw error;
  1373. }
  1374. },
  1375. // 更新主题设置
  1376. updateThemeSettings: async (themeData: Partial<ThemeSettings>): Promise<ThemeSettings> => {
  1377. try {
  1378. const response = await axios.put(`${API_BASE_URL}/theme`, themeData);
  1379. return response.data.data;
  1380. } catch (error) {
  1381. throw error;
  1382. }
  1383. },
  1384. // 重置主题设置
  1385. resetThemeSettings: async (): Promise<ThemeSettings> => {
  1386. try {
  1387. const response = await axios.post(`${API_BASE_URL}/theme/reset`);
  1388. return response.data.data;
  1389. } catch (error) {
  1390. throw error;
  1391. }
  1392. }
  1393. };
  1394. // 图表数据API接口类型
  1395. interface ChartDataResponse<T> {
  1396. message: string;
  1397. data: T;
  1398. }
  1399. interface UserActivityData {
  1400. date: string;
  1401. count: number;
  1402. }
  1403. interface FileUploadsData {
  1404. month: string;
  1405. count: number;
  1406. }
  1407. interface FileTypesData {
  1408. type: string;
  1409. value: number;
  1410. }
  1411. interface DashboardOverviewData {
  1412. userCount: number;
  1413. fileCount: number;
  1414. articleCount: number;
  1415. todayLoginCount: number;
  1416. }
  1417. // 图表数据API
  1418. export const ChartAPI = {
  1419. // 获取用户活跃度数据
  1420. getUserActivity: async (): Promise<ChartDataResponse<UserActivityData[]>> => {
  1421. try {
  1422. const response = await axios.get(`${API_BASE_URL}/charts/user-activity`);
  1423. return response.data;
  1424. } catch (error) {
  1425. throw error;
  1426. }
  1427. },
  1428. // 获取文件上传统计数据
  1429. getFileUploads: async (): Promise<ChartDataResponse<FileUploadsData[]>> => {
  1430. try {
  1431. const response = await axios.get(`${API_BASE_URL}/charts/file-uploads`);
  1432. return response.data;
  1433. } catch (error) {
  1434. throw error;
  1435. }
  1436. },
  1437. // 获取文件类型分布数据
  1438. getFileTypes: async (): Promise<ChartDataResponse<FileTypesData[]>> => {
  1439. try {
  1440. const response = await axios.get(`${API_BASE_URL}/charts/file-types`);
  1441. return response.data;
  1442. } catch (error) {
  1443. throw error;
  1444. }
  1445. },
  1446. // 获取仪表盘概览数据
  1447. getDashboardOverview: async (): Promise<ChartDataResponse<DashboardOverviewData>> => {
  1448. try {
  1449. const response = await axios.get(`${API_BASE_URL}/charts/dashboard-overview`);
  1450. return response.data;
  1451. } catch (error) {
  1452. throw error;
  1453. }
  1454. }
  1455. };
  1456. // 地图相关API的接口类型定义
  1457. export interface LoginLocationResponse {
  1458. message: string;
  1459. data: LoginLocation[];
  1460. }
  1461. export interface LoginLocationDetailResponse {
  1462. message: string;
  1463. data: LoginLocationDetail;
  1464. }
  1465. export interface LoginLocationUpdateResponse {
  1466. message: string;
  1467. data: LoginLocationDetail;
  1468. }
  1469. // 地图相关API
  1470. export const MapAPI = {
  1471. // 获取地图标记点数据
  1472. getMarkers: async (params?: {
  1473. startTime?: string;
  1474. endTime?: string;
  1475. userId?: number
  1476. }): Promise<LoginLocationResponse> => {
  1477. try {
  1478. const response = await axios.get(`${API_BASE_URL}/map/markers`, { params });
  1479. return response.data;
  1480. } catch (error) {
  1481. throw error;
  1482. }
  1483. },
  1484. // 获取登录位置详情
  1485. getLocationDetail: async (locationId: number): Promise<LoginLocationDetailResponse> => {
  1486. try {
  1487. const response = await axios.get(`${API_BASE_URL}/map/location/${locationId}`);
  1488. return response.data;
  1489. } catch (error) {
  1490. throw error;
  1491. }
  1492. },
  1493. // 更新登录位置信息
  1494. updateLocation: async (locationId: number, data: {
  1495. longitude: number;
  1496. latitude: number;
  1497. location_name?: string;
  1498. }): Promise<LoginLocationUpdateResponse> => {
  1499. try {
  1500. const response = await axios.put(`${API_BASE_URL}/map/location/${locationId}`, data);
  1501. return response.data;
  1502. } catch (error) {
  1503. throw error;
  1504. }
  1505. }
  1506. };
  1507. // 系统设置API响应类型
  1508. interface SystemSettingsResponse {
  1509. message: string;
  1510. data: SystemSetting[];
  1511. }
  1512. // 系统设置API
  1513. export const SystemAPI = {
  1514. // 获取所有系统设置
  1515. getSettings: async (): Promise<SystemSettingGroupData[]> => {
  1516. try {
  1517. const response = await axios.get(`${API_BASE_URL}/settings`);
  1518. return response.data.data;
  1519. } catch (error) {
  1520. throw error;
  1521. }
  1522. },
  1523. // 获取指定分组的系统设置
  1524. getSettingsByGroup: async (group: string): Promise<SystemSetting[]> => {
  1525. try {
  1526. const response = await axios.get(`${API_BASE_URL}/settings/group/${group}`);
  1527. return response.data.data;
  1528. } catch (error) {
  1529. throw error;
  1530. }
  1531. },
  1532. // 更新系统设置
  1533. updateSettings: async (settings: Partial<SystemSetting>[]): Promise<SystemSetting[]> => {
  1534. try {
  1535. const response = await axios.put(`${API_BASE_URL}/settings`, settings);
  1536. return response.data.data;
  1537. } catch (error) {
  1538. throw error;
  1539. }
  1540. },
  1541. // 重置系统设置
  1542. resetSettings: async (): Promise<SystemSetting[]> => {
  1543. try {
  1544. const response = await axios.post(`${API_BASE_URL}/settings/reset`);
  1545. return response.data.data;
  1546. } catch (error) {
  1547. throw error;
  1548. }
  1549. }
  1550. };