use-violation-notice-company.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // 报警消息实时提醒
  2. import { getNewIssueList, updateReadIssueId } from '../apis';
  3. import useViolationNoticeStore, { getPlace, emitter } from '../stores/use-violation-notice-store';
  4. import { onUnmounted, onMounted } from 'vue';
  5. import dayjs from 'dayjs';
  6. import { push } from 'notivue';
  7. const useViolationRealtimeCompany = () => {
  8. /** 消息队列,最新的排在最前面,最老的排在最后面 */
  9. // 报警消息最后返回数据的id
  10. const violationStore = useViolationNoticeStore();
  11. let apiTimer: number;
  12. let isFirstLoad = true;
  13. //弹出消息
  14. const showNotice = () => {
  15. const showItem = violationStore.getLastNotice();
  16. // 从数组最末尾弹出消息
  17. if (!showItem) return;
  18. // 只显示当天的时分秒
  19. const renderTime = dayjs(showItem.createdAt).format('HH:mm:ss');
  20. const renderPlace = getPlace([showItem.workshopName, showItem.workspaceName]);
  21. push.success({
  22. props: {
  23. thumbnail: showItem.pictures?.[0],
  24. title: showItem.title,
  25. message: `<div>
  26. <div>地点:${renderPlace}</div>
  27. <div>时间:${renderTime}</div>
  28. </div>`,
  29. onClick: () => {
  30. // 这里打开问题列表
  31. },
  32. },
  33. onAutoClear(item) {
  34. showNotice();
  35. },
  36. onManualClear(item) {
  37. showNotice();
  38. },
  39. });
  40. };
  41. emitter.on('showNotice', showNotice);
  42. const getViolationsRealtime = () => {
  43. getNewIssueList().then((res) => {
  44. if (!res || res?.length === 0) return;
  45. if (isFirstLoad) {
  46. // 如果第一次加载,只显示第一条
  47. violationStore.add([res[0]]);
  48. isFirstLoad = false;
  49. } else {
  50. violationStore.add(res);
  51. }
  52. // 在此次调用更新id接口
  53. updateReadIssueId(res[0].id);
  54. });
  55. };
  56. /** 轮询api */
  57. const pollingApi = () => {
  58. getViolationsRealtime();
  59. clearInterval(apiTimer);
  60. apiTimer = window.setInterval(() => {
  61. getViolationsRealtime();
  62. }, 5000);
  63. };
  64. onMounted(() => {
  65. violationStore.clear();
  66. pollingApi();
  67. });
  68. onUnmounted(() => {
  69. clearInterval(apiTimer);
  70. violationStore.clear();
  71. });
  72. };
  73. export default useViolationRealtimeCompany;