configurable.api.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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>;
  51. };