From 335b4fbf4a383bc0e767dd6d9c88a1b1712d7224 Mon Sep 17 00:00:00 2001
From: web <candymxq888@outlook.com>
Date: 星期六, 12 四月 2025 16:23:11 +0800
Subject: [PATCH] fix:添加报警

---
 .env.development                    |   11 +-
 /dev/null                           |   20 -----
 src/api/screen/index.js             |    5 
 src/main.js                         |    1 
 .env.production                     |   10 +-
 src/utils/requestPolling.js         |  112 ++++++++++++++++++++++++++++
 src/views/screen/pumpInfo/index.vue |    6 -
 src/views/screen/home/index.vue     |   23 ++++-
 8 files changed, 145 insertions(+), 43 deletions(-)

diff --git a/.env.development b/.env.development
index 88988b0..7199d89 100644
--- a/.env.development
+++ b/.env.development
@@ -1,15 +1,14 @@
-n
+
 # 页面标题
-VITE_APP_TITLE = 后台管理系统
+    VITE_APP_TITLE = 后台管理系统
 
 # 开发环境配置
-VITE_APP_ENV = 'development'
+    VITE_APP_ENV = 'development'
 
 # 图片地址
-VITE_APP_IMG_BASEURL='http://192.168.0.200:8036/upload'
+    VITE_APP_IMG_BASEURL='http://192.168.0.200:8036/upload'
 
 # 南京接口
-  # VITE_APP_PUBLIC_REQUEST_API = 'http://113.250.189.120:8030'
   # VITE_APP_PUBLIC_REQUEST_API = 'http://113.250.189.120:8036'
 #后端本地
-   VITE_APP_PUBLIC_REQUEST_API = 'http://192.168.0.200:8038'
+    VITE_APP_PUBLIC_REQUEST_API = 'http://192.168.0.200:8038'
diff --git a/.env.production b/.env.production
index 97124c1..f57c2c3 100644
--- a/.env.production
+++ b/.env.production
@@ -1,14 +1,14 @@
 
 # 页面标题
-VITE_APP_TITLE = 后台管理系统
+    VITE_APP_TITLE = 后台管理系统
 
 # 开发环境配置
-VITE_APP_ENV = 'production'
+    VITE_APP_ENV = 'production'
 
 # 图片地址
-VITE_APP_IMG_BASEURL = 'http://113.250.189.120:8036/upload'
+    VITE_APP_IMG_BASEURL = 'http://113.250.189.120:8036/upload'
 
 # 南京管理系统/开发环境
- VITE_APP_PUBLIC_REQUEST_API = 'http://113.250.189.120:8036'
+    VITE_APP_PUBLIC_REQUEST_API = 'http://113.250.189.120:8036'
 # 是否在打包时开启压缩,支持 gzip 和 brotli
-VITE_BUILD_COMPRESS = gzip
+    VITE_BUILD_COMPRESS = gzip
diff --git a/src/api/screen/index.js b/src/api/screen/index.js
index c3c67fa..6d84a84 100644
--- a/src/api/screen/index.js
+++ b/src/api/screen/index.js
@@ -1,11 +1,12 @@
 import {publicRequest} from '@/utils/request.js'
+import {requestPolling} from '@/utils/requestPolling.js'
 
 
 /**
  * 获取泵房信息
  */
 export const getPumpData = (data) => {
-    return publicRequest({
+    return requestPolling({
         url: '/waterFacilityParameter/getData',
         method: 'post',
         data
@@ -16,7 +17,7 @@
  * 获取泵房报警信息
  */
 export const getPumpWarning = (data) => {
-    return publicRequest({
+    return requestPolling({
         url: '/alarmHistory/search',
         method: 'post',
         data
diff --git a/src/main.js b/src/main.js
index 45b0392..bfb5348 100644
--- a/src/main.js
+++ b/src/main.js
@@ -30,7 +30,6 @@
 import elementIcons from '@/components/SvgIcon/svgicon'
 
 import './permission' // permission control
-import './warning.js'
 import { btnPerms } from './utils/permission'
 
 import { parseTime, resetForm, addDateRange, handleTree, selectDictLabel, selectDictLabels } from '@/utils/ruoyi'
diff --git a/src/utils/requestPolling.js b/src/utils/requestPolling.js
new file mode 100644
index 0000000..4bd710a
--- /dev/null
+++ b/src/utils/requestPolling.js
@@ -0,0 +1,112 @@
+import axios from 'axios'
+import { ElNotification , ElMessageBox, ElMessage, ElLoading } from 'element-plus'
+import {getToken, removeToken} from '@/utils/auth'
+import errorCode from '@/utils/errorCode'
+import { tansParams, blobValidate } from '@/utils/ruoyi'
+import cache from '@/plugins/cache'
+// import { saveAs } from 'file-saver'
+// import useUserStore from '@/store/modules/user'
+
+// let downloadLoadingInstance;
+// 是否显示重新登录
+export let isRelogin = { show: false };
+
+axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
+
+export const requestPolling = axios.create({
+    // axios中请求配置有baseURL选项,表示请求URL公共部分
+    //baseURL: import.meta.env.VITE_APP_BASE_API,
+    baseURL: import.meta.env.VITE_APP_PUBLIC_REQUEST_API,
+    // 超时
+    timeout: 5000
+})
+
+
+// request拦截器
+requestPolling.interceptors.request.use((config) => {
+    // 是否需要设置 token
+    const isToken = (config.headers || {}).isToken === false
+    // 是否需要防止数据重复提交
+    const isRepeatSubmit = (config.headers || {}).repeatSubmit === false
+    if (getToken() && !isToken) {
+        config.headers['token'] =  getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
+    }
+    // if (localStorage.getItem('token')) {
+    //   config.headers!['token'] = localStorage.getItem('token');
+    // }
+    // get请求映射params参数
+    if (config.method === 'get' && config.params) {
+        let url = config.url + '?' + tansParams(config.params);
+        url = url.slice(0, -1);
+        config.params = {};
+        config.url = url;
+    }
+    if (!isRepeatSubmit && (config.method === 'post' || config.method === 'put')) {
+        const requestObj = {
+            url: config.url,
+            data: typeof config.data === 'object' ? JSON.stringify(config.data) : config.data,
+            time: new Date().getTime()
+        }
+        const requestSize = Object.keys(JSON.stringify(requestObj)).length; // 请求数据大小
+        const limitSize = 5 * 1024 * 1024; // 限制存放数据5M
+        if (requestSize >= limitSize) {
+            console.warn(`[${config.url}]: ` + '请求数据大小超出允许的5M限制,无法进行防重复提交验证。')
+            return config;
+        }
+        const sessionObj = cache.session.getJSON('sessionObj')
+        if (sessionObj === undefined || sessionObj === null || sessionObj === '') {
+            cache.session.setJSON('sessionObj', requestObj)
+        } else {
+            const s_url = sessionObj.url;                // 请求地址
+            const s_data = sessionObj.data;              // 请求数据
+            const s_time = sessionObj.time;              // 请求时间
+            const interval = 2000;                       // 间隔时间(ms),小于此时间视为重复提交
+            // if (s_data === requestObj.data && requestObj.time - s_time < interval && s_url === requestObj.url) {
+            //   const message = '数据正在处理,请勿重复提交';
+            //   console.warn(`[${s_url}]: ` + message)
+            //   return Promise.reject(new Error(message))
+            // } else {
+            cache.session.setJSON('sessionObj', requestObj)
+            // }
+        }
+    }
+    return config
+}, error => {
+    console.log(error)
+    Promise.reject(error)
+})
+
+// 响应拦截器
+requestPolling.interceptors.response.use(res => {
+        // 未设置状态码则默认成功状态
+        const code = res.data.code || 200;
+        // 获取错误信息
+        const msg = errorCode[code] || res.data.msg || errorCode['default']
+        // 二进制数据则直接返回
+        if (res.request.responseType ===  'blob' || res.request.responseType ===  'arraybuffer') {
+            return res.data
+        }
+        if (code === 401) {
+            if (!isRelogin.show) {
+                isRelogin.show = true;
+                ElMessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', { confirmButtonText: '重新登录', cancelButtonText: '取消', type: 'warning' }).then(() => {
+                    isRelogin.show = false;
+                    removeToken()
+                    window.location.reload();
+                }).catch(() => {
+                    isRelogin.show = false;
+                });
+            }
+            return Promise.reject('无效的会话,或者会话已过期,请重新登录。')
+        } else if (code === 500) {
+            return Promise.reject(new Error(msg))
+        } else if (code === 601) {
+            return Promise.reject(new Error(msg))
+        } else if (code !== 200) {
+            return Promise.reject('error')
+        } else {
+            return  Promise.resolve(res.data)
+        }
+    }
+)
+
diff --git a/src/views/screen/home/index.vue b/src/views/screen/home/index.vue
index a77ce38..7727726 100644
--- a/src/views/screen/home/index.vue
+++ b/src/views/screen/home/index.vue
@@ -86,12 +86,16 @@
 <script setup lang="ts">
 import {onMounted, onUnmounted, ref} from "vue";
 import {getPumpData, getPumpWarning} from '@/api/screen/index.js'
+import { ElMessageBox } from 'element-plus'
 
 const tucengVal = ref(0);
 const warnList = ref([])
 const showMsg = ref(0)
 const pointList = ref([])
+const dislogOpen = ref(false)
 let timer = null;
+
+const warnCode = ['B001StartNumber', 'B002StartNumber', 'B003StartNumber', 'B004StartNumber']
 
 const clickMsgFun = (index: number) => {
     showMsg.value = showMsg.value === index ? null : index;
@@ -101,9 +105,16 @@
 const getWarning = () => {
     getPumpWarning({limit: 10, page:1}).then(res => {
         warnList.value = res.data.list
-    }).catch(err => {
-        if(err.message.includes("timeout")){
-            clearInterval(timer)
+        const warnDialogList = res.data.list.filter(el => warnCode.includes(el.code))
+        if(!dislogOpen.value && warnDialogList.length > 0) {
+            dislogOpen.value=true
+            ElMessageBox.confirm(warnDialogList[0].facilityName, warnDialogList[0].description, {
+                showCancelButton: false,
+                confirmButtonText: '关闭',
+                type: 'error'
+            }).then(() => {
+                console.log('关闭')
+            })
         }
     })
 }
@@ -147,7 +158,7 @@
     // 定时获取报警信息
     timer = setInterval(() => {
         getWarning()
-    }, 3000)
+    }, 5000)
 })
 onUnmounted(() => {
     if(timer){
@@ -259,6 +270,8 @@
             transition: all .5s;
             &-t {
                 background-image: url('@/assets/images_lc/title_font.png');
+                background-repeat: no-repeat;
+                background-size: 100% 100%;
                 padding-left: 16%;
                 font-size: 1.2rem;
             }
@@ -329,6 +342,8 @@
             flex-direction: column;
             .warnRecord-title {
                 background-image: url('@/assets/images_lc/title_font.png');
+                background-repeat: no-repeat;
+                background-size: 100% 100%;
                 padding-left: 16%;
                 font-size: 1.3rem;
             }
diff --git a/src/views/screen/pumpInfo/index.vue b/src/views/screen/pumpInfo/index.vue
index fb16372..6284550 100644
--- a/src/views/screen/pumpInfo/index.vue
+++ b/src/views/screen/pumpInfo/index.vue
@@ -46,10 +46,6 @@
             }
         })
         list.value = newArr.splice(0,hasPump)
-    }).catch(err => {
-        if(err.message.includes("timeout")){
-            clearInterval(timer)
-        }
     })
 }
 
@@ -58,7 +54,7 @@
     // 定时3秒回调一次,更新数据
     timer = setInterval(() => {
         getData()
-    }, 3000)
+    }, 5000)
 })
 
 onUnmounted(() => {
diff --git a/src/warning.js b/src/warning.js
deleted file mode 100644
index c2331cf..0000000
--- a/src/warning.js
+++ /dev/null
@@ -1,20 +0,0 @@
-import {getPumpWarning} from '@/api/screen/index.js'
-import { ElNotification } from 'element-plus'
-
-let timeout = 1000 * 3;
-let timer = null;
-
-// timer = setInterval(() => {
-//     getPumpWarning({limit: 1, page:1}).then(res => {
-//         if (res.data.list.length > 0) {
-//             ElNotification.closeAll()
-//             ElNotification({
-//                 title: '错误',
-//                 message: res.data.list[0].description,
-//                 type: 'error',
-//                 duration: 0
-//             })
-//         }
-//     })
-// }, timeout)
-

--
Gitblit v1.9.3