qingyiay
2023-09-07 491249bdf736a5f7d50417c889ad2db734b12383
store/index.js
@@ -105,7 +105,11 @@
      globalIntervalId: null,
      is_open_socket: false, //避免重复连接
      connectNum: 1, //重连次数,
      reconnectTimeOut: null
      reconnectTimeOut: null,
      // 称重稳定态  0/稳定 1/非稳定  非稳定态不能点称重
      globalWarning: false,
      globalShowWeigh: [],
      isFirstLogin: true
   },
   mutations: {
      lengthchange(state, payload) {
@@ -169,8 +173,23 @@
      },
      // 改变重连次数
      changereconnectNum(state, payload) {
         state.connectNum = payload
         if (state.connectNum <= 6) {
            state.connectNum += payload
         } else {
            state.connectNum = payload
         }
      },
      // 改变稳定态
      changeWarning(state, payload) {
         state.globalWarning = payload
      },
      changeglobalShowWeigh(state, payload) {
         state.globalShowWeigh = payload
      },
      changeisFirstLogin(state, payload) {
         state.isFirstLogin = payload
         console.log(state.isFirstLogin, '第一次登录');
      }
   },
   actions: {
      websocketInit({ state, dispatch, commit }) {
@@ -221,12 +240,22 @@
         clearInterval(state.reconnectTimeOut)
         state.socketTast = null
         commit('changereconnectState', false)
         if (state.connectNum < 6) {
            dispatch('reconnect')
         } else {
         if (state.connectNum <= 6 && state.globalisLogin) {
            uni.showToast({
               title: `连接失败,正在尝试第${state.connectNum}次连接`,
               icon: 'none'
            })
            commit('changereconnectNum', 1)
            dispatch('reconnect')
         } else if (state.connectNum > 6 && state.globalisLogin) {
            commit('changereconnectNum', 1)
            uni.showToast({
               title: '网络异常,请稍后重试',
               icon: 'none'
            })
         } else {
            return
         }
      },
      websocketOnError({ state, commit, dispatch }, e) {
         // 如果重连状态为false则不进行重连,为true才会
@@ -235,17 +264,21 @@
         clearInterval(state.reconnectTimeOut)
         state.socketTast = null
         commit('changereconnectState', false)
         if (state.connectNum < 6) {
         if (state.connectNum < 6 && state.globalisLogin) {
            uni.showToast({
               title: `连接失败,正尝试第${state.connectNum}次连接`,
               title: `连接失败,正在尝试第${state.connectNum}次连接`,
               icon: 'none'
            })
            let num = 1;
            num++;
            commit('changereconnectNum', num)
            dispatch('reconnect')
         } else {
            commit('changereconnectNum', 1)
            dispatch('reconnect')
         } else if (state.connectNum >= 6 && state.globalisLogin) {
            commit('changereconnectNum', 1)
            uni.showToast({
               title: '网络异常,请稍后重试',
               icon: 'none'
            })
         } else {
            return
         }
      },
      // 接收数据
@@ -256,7 +289,7 @@
      reconnect({ state, commit, dispatch }) {
         console.log(state.is_open_socket, '重新连接socket状态');
         clearInterval(state.globalIntervalId)
         if (!state.is_open_socket) {
         if (!state.is_open_socket && state.globalisLogin) {
            state.reconnectTimeOut = setInterval(() => {
               dispatch('websocketInit')
            }, 5000)