0602
This commit is contained in:
@@ -0,0 +1,130 @@
|
||||
export default {
|
||||
namespaced: true,
|
||||
state: {
|
||||
// 称重设备配置
|
||||
scaleConfig: {
|
||||
connected: false, // 是否已连接
|
||||
deviceId: '', // 蓝牙设备ID
|
||||
deviceName: '', // 蓝牙设备名称
|
||||
model: 'A12', // 电子秤型号
|
||||
// ========== 蓝牙通信配置 ==========
|
||||
serviceId: '', // 蓝牙服务UUID
|
||||
characteristicId: '', // 特征值UUID(用于接收重量数据,通常为notify类型)
|
||||
writeCharacteristicId: '', // 可写特征值UUID(用于发送命令)
|
||||
lastConfigured: '' // 最后配置时间
|
||||
},
|
||||
// 当前重量数据
|
||||
currentWeight: null
|
||||
},
|
||||
mutations: {
|
||||
SET_SCALE_CONFIG(state, config) {
|
||||
state.scaleConfig = { ...state.scaleConfig, ...config }
|
||||
},
|
||||
SET_CURRENT_WEIGHT(state, weight) {
|
||||
state.currentWeight = weight
|
||||
},
|
||||
SET_WEIGHT_VALUE(state, { value, unit, stable }) {
|
||||
state.currentWeight = {
|
||||
value,
|
||||
unit: unit || (state.currentWeight && state.currentWeight.unit) || 'kg',
|
||||
stable,
|
||||
timestamp: new Date().toISOString()
|
||||
}
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
// 初始化称重设备配置
|
||||
async initScaleConfig({ commit }) {
|
||||
try {
|
||||
const scaleConfig = uni.getStorageSync('scaleConfig')
|
||||
if (scaleConfig) {
|
||||
commit('SET_SCALE_CONFIG', scaleConfig)
|
||||
} else {
|
||||
uni.setStorageSync('scaleConfig', {
|
||||
connected: false,
|
||||
deviceId: '',
|
||||
deviceName: '',
|
||||
model: 'A12',
|
||||
serviceId: '',
|
||||
characteristicId: '',
|
||||
writeCharacteristicId: '',
|
||||
lastConfigured: ''
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('初始化称重设备配置失败:', error)
|
||||
}
|
||||
},
|
||||
|
||||
// 保存称重设备配置
|
||||
async saveScaleConfig({ commit, state }, config) {
|
||||
try {
|
||||
commit('SET_SCALE_CONFIG', config)
|
||||
uni.setStorageSync('scaleConfig', state.scaleConfig)
|
||||
return true
|
||||
} catch (error) {
|
||||
console.error('保存称重设备配置失败:', error)
|
||||
return false
|
||||
}
|
||||
},
|
||||
|
||||
// 更新当前重量
|
||||
updateWeight({ commit }, weight) {
|
||||
commit('SET_CURRENT_WEIGHT', weight)
|
||||
},
|
||||
|
||||
// 更新重量值(简化版)
|
||||
setWeightValue({ commit }, payload) {
|
||||
commit('SET_WEIGHT_VALUE', payload)
|
||||
},
|
||||
|
||||
// 连接称重设备
|
||||
async connectScale({ commit, dispatch }, device) {
|
||||
try {
|
||||
commit('SET_SCALE_CONFIG', {
|
||||
connected: true,
|
||||
deviceId: device.deviceId,
|
||||
deviceName: device.name || device.localName
|
||||
})
|
||||
await dispatch('saveScaleConfig')
|
||||
return true
|
||||
} catch (error) {
|
||||
console.error('连接称重设备失败:', error)
|
||||
return false
|
||||
}
|
||||
},
|
||||
|
||||
// 断开称重设备
|
||||
async disconnectScale({ commit, dispatch }) {
|
||||
try {
|
||||
commit('SET_SCALE_CONFIG', {
|
||||
connected: false,
|
||||
deviceId: '',
|
||||
deviceName: ''
|
||||
})
|
||||
await dispatch('saveScaleConfig')
|
||||
return true
|
||||
} catch (error) {
|
||||
console.error('断开称重设备失败:', error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
},
|
||||
getters: {
|
||||
isScaleConnected: state => state.scaleConfig.connected,
|
||||
scaleModel: state => state.scaleConfig.model,
|
||||
// ========== 蓝牙配置获取器 ==========
|
||||
scaleServiceId: state => state.scaleConfig.serviceId,
|
||||
scaleCharacteristicId: state => state.scaleConfig.characteristicId,
|
||||
scaleWriteCharId: state => state.scaleConfig.writeCharacteristicId,
|
||||
scaleDeviceId: state => state.scaleConfig.deviceId,
|
||||
scaleDeviceName: state => state.scaleConfig.deviceName,
|
||||
currentWeight: state => state.currentWeight,
|
||||
// 格式化重量显示
|
||||
formattedWeight: state => {
|
||||
if (!state.currentWeight) return '-- kg'
|
||||
const { value, unit } = state.currentWeight
|
||||
return `${value && value.toFixed(2) || '0.00'} ${unit || 'kg'}`
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user