configurable.api.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { defHttp } from '/@/utils/http/axios';
  2. enum Api {
  3. list = '/safety/ventanalyDevice/homedata2',
  4. getHomeData = '/safety/ventanalyDevice/homedata',
  5. getBDDustData = '/ventanaly-device/monitor/disaster/getDisDustHome',
  6. getBDFireData = '/ventanaly-device/monitor/disaster/getDisFireHome',
  7. }
  8. // 搞这个缓存是由于:目前代码上的设计是多个模块发出多次请求,每个模块自己负责消费前者的响应。
  9. // 这会导致相同的请求被同时发送多次。
  10. const cache = new Map<string, Promise<any>>();
  11. /**
  12. * 列表接口
  13. * @param params
  14. */
  15. export const list = (params) => defHttp.post({ url: Api.list, params });
  16. export const getHomeData = (params) => {
  17. const key = `${Api.getHomeData}?${JSON.stringify(params)}`;
  18. if (!cache.has(key)) {
  19. cache.set(
  20. key,
  21. defHttp.post({ url: Api.getHomeData, params }).finally(() => {
  22. cache.delete(key);
  23. })
  24. );
  25. }
  26. return cache.get(key) as Promise<any>;
  27. };
  28. export const getBDDustData = (params) => {
  29. const key = `${Api.getBDDustData}?${JSON.stringify(params)}`;
  30. if (!cache.has(key)) {
  31. cache.set(
  32. key,
  33. defHttp.post({ url: Api.getBDDustData, params }).finally(() => {
  34. cache.delete(key);
  35. })
  36. );
  37. }
  38. return cache.get(key) as Promise<any>;
  39. };
  40. export const getBDFireData = (params) => {
  41. const key = `${Api.getBDFireData}?${JSON.stringify(params)}`;
  42. if (!cache.has(key)) {
  43. cache.set(
  44. key,
  45. defHttp.post({ url: Api.getBDFireData, params }).finally(() => {
  46. cache.delete(key);
  47. })
  48. );
  49. }
  50. return (cache.get(key) as Promise<any>).then((res) => {
  51. res.pdArray.forEach((e) => {
  52. e.arrayFiber.forEach((j) => {
  53. j.fibreTemperatureArr = JSON.parse(j.fibreTemperature);
  54. });
  55. });
  56. res.sgGxObj.devGxcw.forEach((e) => {
  57. e.fibreTemperatureArr = JSON.parse(e.fibreTemperature);
  58. });
  59. return res;
  60. });
  61. };