0602
This commit is contained in:
@@ -0,0 +1,167 @@
|
||||
export default {
|
||||
namespaced: true,
|
||||
state: {
|
||||
// 打印机配置对象
|
||||
printerConfig: {
|
||||
connected: false, // 是否已连接
|
||||
deviceId: '', // 蓝牙设备ID
|
||||
deviceName: '', // 蓝牙设备名称
|
||||
model: '', // 打印机型号
|
||||
paperWidth: 100, // 纸张宽度(mm) - 统一默认值
|
||||
paperHeight: 100, // 纸张高度(mm) - 统一默认值
|
||||
serviceId: '', // 蓝牙服务ID
|
||||
writeCharId: '', // 蓝牙特征值ID
|
||||
lastUpdated: new Date().toISOString()
|
||||
}
|
||||
},
|
||||
mutations: {
|
||||
// 设置打印机配置(合并更新)
|
||||
SET_PRINTER_CONFIG(state, config) {
|
||||
state.printerConfig = { ...state.printerConfig, ...config };
|
||||
state.printerConfig.lastUpdated = new Date().toISOString();
|
||||
},
|
||||
// 清除打印机配置
|
||||
CLEAR_PRINTER_CONFIG(state) {
|
||||
state.printerConfig = {
|
||||
connected: false,
|
||||
deviceId: '',
|
||||
deviceName: '',
|
||||
model: '',
|
||||
paperWidth: 100,
|
||||
paperHeight: 100,
|
||||
serviceId: '',
|
||||
writeCharId: '',
|
||||
lastUpdated: new Date().toISOString()
|
||||
};
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
// 初始化打印机配置(从本地存储加载)
|
||||
async initPrinterConfig({ commit }) {
|
||||
try {
|
||||
const printerConfig = uni.getStorageSync('printerConfig');
|
||||
if (printerConfig) {
|
||||
commit('SET_PRINTER_CONFIG', printerConfig);
|
||||
} else {
|
||||
// 创建默认配置
|
||||
const defaultConfig = {
|
||||
connected: false,
|
||||
deviceId: '',
|
||||
deviceName: '',
|
||||
model: '',
|
||||
paperWidth: 100,
|
||||
paperHeight: 100,
|
||||
serviceId: '',
|
||||
writeCharId: '',
|
||||
lastUpdated: new Date().toISOString()
|
||||
};
|
||||
uni.setStorageSync('printerConfig', defaultConfig);
|
||||
commit('SET_PRINTER_CONFIG', defaultConfig);
|
||||
}
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('初始化打印机配置失败:', error);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
// 保存打印机配置到本地存储
|
||||
async savePrinterConfig({ commit, state }, config) {
|
||||
try {
|
||||
if (config) {
|
||||
commit('SET_PRINTER_CONFIG', config);
|
||||
}
|
||||
uni.setStorageSync('printerConfig', state.printerConfig);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('保存打印机配置失败:', error);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
// 连接打印机(更新连接状态并保存)
|
||||
async connectPrinter({ commit, dispatch }, device) {
|
||||
try {
|
||||
const config = {
|
||||
connected: true,
|
||||
deviceId: device.deviceId,
|
||||
deviceName: device.name || device.localName,
|
||||
lastUpdated: new Date().toISOString()
|
||||
};
|
||||
await dispatch('savePrinterConfig', config);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('连接打印机失败:', error);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
// 断开打印机连接
|
||||
async disconnectPrinter({ commit, dispatch }) {
|
||||
try {
|
||||
const config = {
|
||||
connected: false,
|
||||
deviceId: '',
|
||||
deviceName: '',
|
||||
lastUpdated: new Date().toISOString()
|
||||
};
|
||||
await dispatch('savePrinterConfig', config);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('断开打印机失败:', error);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
// 清除打印机配置(重置并删除本地存储)
|
||||
async clearPrinterConfig({ commit }) {
|
||||
try {
|
||||
commit('CLEAR_PRINTER_CONFIG');
|
||||
uni.removeStorageSync('printerConfig');
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('清除打印机配置失败:', error);
|
||||
return false;
|
||||
}
|
||||
},
|
||||
|
||||
// 更新纸张尺寸
|
||||
async updatePaperSize({ dispatch }, { width, height }) {
|
||||
try {
|
||||
await dispatch('savePrinterConfig', {
|
||||
paperWidth: width,
|
||||
paperHeight: height,
|
||||
lastUpdated: new Date().toISOString()
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('更新纸张尺寸失败:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
},
|
||||
getters: {
|
||||
// 连接状态
|
||||
isPrinterConnected: state => state.printerConfig.connected,
|
||||
|
||||
// 设备信息
|
||||
deviceId: state => state.printerConfig.deviceId,
|
||||
deviceName: state => state.printerConfig.deviceName,
|
||||
printerModel: state => state.printerConfig.model,
|
||||
|
||||
// 服务配置
|
||||
serviceId: state => state.printerConfig.serviceId,
|
||||
writeCharId: state => state.printerConfig.writeCharId,
|
||||
|
||||
// 纸张尺寸
|
||||
paperWidth: state => state.printerConfig.paperWidth,
|
||||
paperHeight: state => state.printerConfig.paperHeight,
|
||||
paperSize: state => `${state.printerConfig.paperWidth}×${state.printerConfig.paperHeight}mm`,
|
||||
|
||||
// 完整配置
|
||||
savedConfig: state => state.printerConfig,
|
||||
|
||||
// 配置是否为空
|
||||
hasSavedConfig: state => !!state.printerConfig.deviceId
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user