fei.wang
7 天以前 ae7b22322555448d95fd56f505bafa325c167a26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const state = {
  // 基站信息缓存
  baseStationInfo: {
    head: null, // 车头基站信息
    tail: null  // 车尾基站信息
  },
  // 是否已初始化
  isInitialized: false
}
 
const mutations = {
  // 设置基站信息
  SET_BASE_STATION_INFO(state, { position, info }) {
    state.baseStationInfo[position] = info
  },
  
  // 设置初始化状态
  SET_INITIALIZED(state, status) {
    state.isInitialized = status
  },
  
  // 清空所有基站信息
  CLEAR_BASE_STATION_INFO(state) {
    state.baseStationInfo = {
      head: null,
      tail: null
    }
    state.isInitialized = false
  }
}
 
const actions = {
  // 保存基站信息到缓存
  saveBaseStationInfo({ commit }, { position, info }) {
    // 验证信息完整性
    if (!info.id || !info.green || !info.yellow || !info.red) {
      throw new Error('基站信息不完整')
    }
    
    // 保存到Vuex
    commit('SET_BASE_STATION_INFO', { position, info })
    
    // 保存到本地存储
    try {
      const cachedData = uni.getStorageSync('baseStationInfo') || {}
      cachedData[position] = info
      uni.setStorageSync('baseStationInfo', cachedData)
      console.log('基站信息已保存到缓存:', position, info)
    } catch (error) {
      console.error('保存基站信息到本地存储失败:', error)
    }
  },
  
  // 从缓存加载基站信息
  loadBaseStationInfo({ commit }) {
    try {
      const cachedData = uni.getStorageSync('baseStationInfo')
      if (cachedData) {
        commit('SET_BASE_STATION_INFO', { position: 'head', info: cachedData.head })
        commit('SET_BASE_STATION_INFO', { position: 'tail', info: cachedData.tail })
        
        // 检查是否两个位置都有数据
        const hasHead = cachedData.head && cachedData.head.id
        const hasTail = cachedData.tail && cachedData.tail.id
        
        if (hasHead || hasTail) {
          commit('SET_INITIALIZED', true)
          console.log('从缓存加载基站信息成功')
          return Promise.resolve(true)
        }
      }
      return Promise.resolve(false)
    } catch (error) {
      console.error('从缓存加载基站信息失败:', error)
      return Promise.resolve(false)
    }
  },
  
  // 检查是否需要初始化
  async checkInitialization({ state, dispatch, commit }) {
    // 先尝试从缓存加载
    const hasCachedData = await dispatch('loadBaseStationInfo')
    
    if (!hasCachedData) {
      // 没有缓存数据,需要初始化
      commit('SET_INITIALIZED', false)
      return false
    }
    
    return true
  },
  
  // 清空所有基站信息
  clearBaseStationInfo({ commit }) {
    commit('CLEAR_BASE_STATION_INFO')
    
    // 清空本地存储
    try {
      uni.removeStorageSync('baseStationInfo')
      console.log('基站信息已清空')
    } catch (error) {
      console.error('清空本地存储失败:', error)
    }
  }
}
 
const getters = {
  // 获取指定位置的基站信息
  getBaseStationInfo: (state) => (position) => {
    return state.baseStationInfo[position]
  },
  
  // 获取所有基站信息
  getAllBaseStationInfo: (state) => {
    return state.baseStationInfo
  },
  
  // 检查是否已初始化
  isInitialized: (state) => {
    return state.isInitialized
  },
  
  // 检查是否有基站信息
  hasBaseStationInfo: (state) => {
    return !!(state.baseStationInfo.head || state.baseStationInfo.tail)
  }
}
 
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters