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
|
}
|