qingyiay
2023-08-14 76c41f4669a36b0d4cb5b82906419408a6b7de96
store/index.js
@@ -1,14 +1,21 @@
import Vue from 'vue'
import Vuex from 'vuex'
import { webSocketUrl } from '@/api/request.js'
Vue.use(Vuex)
// 动态底部tabbar
const userRoleTabbar = {
   first: [{
         pagePath: 'pages/tabbar-page/index-tabbar/index-tabbar',
         iconPath: '../../static/tabBar-icon/Gfayun.png',
         selectedIconPath: '../../static/tabBar-icon/Bfayun1.png',
         iconPath: '../../static/tabBar-icon/home.png',
         selectedIconPath: '../../static/tabBar-icon/homeblue.png',
         text: '首页'
      },
      {
         pagePath: 'pages/customer-page/customer-my/faYunstatistics/faYunstatistics',
         iconPath: '../../static/tabBar-icon/statistics.png',
         selectedIconPath: '../../static/tabBar-icon/statisticsactive.png',
         text: '统计'
      },
      {
         pagePath: 'pages/tabbar-page/myPage-tabbar/myPage-tabbar',
@@ -19,8 +26,27 @@
   ],
   second: [{
         pagePath: 'pages/tabbar-page/index-tabbar/index-tabbar',
         iconPath: '../../static/tabBar-icon/Ghome.png',
         selectedIconPath: '../../static/tabBar-icon/Bhome.png',
         iconPath: '../../static/tabBar-icon/home.png',
         selectedIconPath: '../../static/tabBar-icon/homeblue.png',
         text: '首页'
      },
      {
         pagePath: 'pages/customer-page/customer-my/faYunstatistics/faYunstatistics',
         iconPath: '../../static/tabBar-icon/statistics.png',
         selectedIconPath: '../../static/tabBar-icon/statisticsactive.png',
         text: '统计'
      },
      {
         pagePath: 'pages/tabbar-page/myPage-tabbar/myPage-tabbar',
         iconPath: '../../static/tabBar-icon/Gmine10.png',
         selectedIconPath: '../../static/tabBar-icon/Bmine57.png',
         text: '我的'
      }
   ],
   three: [{
         pagePath: 'pages/tabbar-page/index-tabbar/index-tabbar',
         iconPath: '../../static/tabBar-icon/home.png',
         selectedIconPath: '../../static/tabBar-icon/homeblue.png',
         text: '首页'
      },
      {
@@ -35,7 +61,7 @@
   state: {
      // 用戶界面判斷
      userInfo: {},
      roleType: null, // 1客户,2货代,3司机
      roleType: null, // 1客户,2货代,3司机 5装卸员
      userTabbar: [], // 用户所在角色底部菜单,
      // 称重时的重量
      globalweigh: 0,
@@ -44,7 +70,30 @@
      // 全局weihousecode
      globalweighHouseCode: '',
      // 全局红外状态
      globalinfraredStatus: false
      globalinfraredStatus: false,
      // 司机修改个人信息
      globaluserInfo: {
         phone: '',
         name: '',
         username: '',
         type: 3,
         idCard: '',
         carNo: '',
         carImg: '',
         drivingImg: '',
         axleNum: '',
         weight: '',
         userId: ''
      },
      globalisconnect: true,
      globalisUploadimg: false,
      globalisLogin: false,
      socketTask: null,
      websocketData: null, // 存放从后端接收到的websocket数据
      globalIntervalId: null,
      is_open_socket: false, //避免重复连接
      connectNum: 1, //重连次数,
      reconnectTimeOut: null
   },
   mutations: {
      lengthchange(state, payload) {
@@ -62,12 +111,15 @@
         } else if (roleType == 2 || roleType == 3) {
            state.userTabbar = userRoleTabbar.second
            uni.setStorageSync('userTabbar', state.userTabbar)
         } else if (roleType == 5) {
            state.userTabbar = userRoleTabbar.three
            uni.setStorageSync('userTabbar', state.userTabbar)
         }
      },
      // 改变重量
      changeWeigh(state, payload) {
         state.globalweigh = payload
         console.log(state.globalweigh, '重量改变了')
         console.log(state.globalweigh, '全局重量改变了')
      },
      // 消息推送
      pushMessage(state, payload) {
@@ -81,8 +133,141 @@
      // 改变红外状态
      changeinfraredStatus(state, payload) {
         state.globalinfraredStatus = payload
      },
      changeuserInfo(state, payload) {
         state.globaluserInfo = payload
      },
      // 改版全局websocket状态
      changeisconnect(state, payload) {
         state.globalisconnect = payload
      },
      changeisUploadimg(state, payload) {
         state.globalisUploadimg = payload
      },
      changeisLogin(state, payload) {
         state.globalisLogin = payload
         console.log(state.globalisLogin, '全局登录状态改变了')
      },
      setWebsocketData(state, data) {
         state.websocketData = data
      },
      // 改变重连状态
      changereconnectState(state, payload) {
         state.is_open_socket = payload
      },
      // 改变重连次数
      changereconnectNum(state, payload) {
         if (state.connectNum < 6) {
            state.connectNum += payload
         } else {
            state.connectNum = payload
         }
      },
   },
   actions: {
      websocketInit({ state, dispatch, commit }) {
         let wsUrl = `${webSocketUrl}?access_token=${uni.getStorageSync('token')}`
         state.socketTast = uni.connectSocket({
            url: wsUrl,
            header: { CLIENT_TOC: 'Y' },
            success: res => {
               console.log(res, 'socketSuccess')
               if (res.errMsg === 'connectSocket:ok') {
                  commit('changeisconnect', true)
                  commit('changereconnectState', true)
               }
            },
            fail: res => {
               console.log(res, 'socketFail')
               console.log('ws失败');
               commit('changereconnectState', false)
               dispatch('reconnect')
            }
         })
         state.socketTast.onOpen(() => dispatch('websocketOnOpen'))
         state.socketTast.onMessage(result => dispatch('websocketOnMessage', result.data))
         state.socketTast.onClose(e => dispatch('websocketOnClose', e))
         state.socketTast.onError(e => dispatch('websocketOnError', e))
      },
      websocketOnOpen({ state, commit }) {
         console.log('ws打开')
         clearInterval(state.reconnectTimeOut)
         clearInterval(state.globalIntervalId)
         state.globalIntervalId = setInterval(() => {
            state.socketTast.send({
               data: JSON.stringify({ type: 'ping' }),
               success(e) {
                  console.log(e, '发送心跳成功')
               }
            })
         }, 30000)
      },
      websocketOnClose({ state, commit, dispatch }, e) {
         if (!state.socketTast) return
         console.log('ws关闭', e)
         state.socketTast.close(e => {
            commit('changeisconnect', false)
         })
         clearInterval(state.globalIntervalId)
         clearInterval(state.reconnectTimeOut)
         state.socketTast = null
         commit('changereconnectState', false)
         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才会
         console.log('ws报错', e)
         clearInterval(state.globalIntervalId)
         clearInterval(state.reconnectTimeOut)
         state.socketTast = null
         commit('changereconnectState', false)
         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
         }
      },
      // 接收数据
      websocketOnMessage({ state, commit }, result) {
         commit('setWebsocketData', result)
      },
      // 重新连接
      reconnect({ state, commit, dispatch }) {
         console.log(state.is_open_socket, '重新连接socket状态');
         clearInterval(state.globalIntervalId)
         if (!state.is_open_socket && state.globalisLogin) {
            state.reconnectTimeOut = setInterval(() => {
               dispatch('websocketInit')
            }, 5000)
         }
      }
   }
})
export default store
export default store