From 4502a4f673860f3ea7a21facf5af7f0afa976391 Mon Sep 17 00:00:00 2001 From: zm2000 <18386612143@163.com> Date: Mon, 30 Mar 2026 17:55:10 +0800 Subject: [PATCH] feat(store): add device info module with base device collection --- core/store/index.js | 4 +- core/store/modules/deviceInfo.js | 126 +++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 core/store/modules/deviceInfo.js diff --git a/core/store/index.js b/core/store/index.js index 2e97fd7..071ed9a 100644 --- a/core/store/index.js +++ b/core/store/index.js @@ -8,6 +8,7 @@ import broadcast from './modules/broadcast.js' import floatWindowManager from './modules/floatWindowManager.js' import bluetoothPrint from './modules/bluetoothPrint.js' import print from './modules/print.js' +import deviceInfo from './modules/deviceInfo.js' const store = new Vuex.Store({ modules: { @@ -16,7 +17,8 @@ const store = new Vuex.Store({ broadcast, floatWindowManager, bluetoothPrint, - print + print, + deviceInfo } }) diff --git a/core/store/modules/deviceInfo.js b/core/store/modules/deviceInfo.js new file mode 100644 index 0000000..4407bce --- /dev/null +++ b/core/store/modules/deviceInfo.js @@ -0,0 +1,126 @@ +// 设备信息本地存储键 +const STORAGE_KEY = 'device_info'; + +// 模块状态:保存设备信息主体和更新时间 +const state = () => ({ + deviceInfo: uni.getStorageSync(STORAGE_KEY) || null, + updatedAt: '' +}); + +// 便捷读取器 +const getters = { + // 当前设备信息对象 + deviceInfo: (state) => state.deviceInfo, + // 是否已存在设备信息 + hasDeviceInfo: (state) => !!state.deviceInfo, + // 最近更新时间(ISO字符串) + deviceInfoUpdatedAt: (state) => state.updatedAt +}; + +// 状态变更:负责数据落库和清理 +const mutations = { + // 设置设备信息并同步到本地缓存 + SET_DEVICE_INFO(state, payload) { + state.deviceInfo = payload || null; + state.updatedAt = payload ? new Date().toISOString() : ''; + if (payload) { + uni.setStorageSync(STORAGE_KEY, payload); + } else { + uni.removeStorageSync(STORAGE_KEY); + } + }, + // 清空设备信息并移除本地缓存 + CLEAR_DEVICE_INFO(state) { + state.deviceInfo = null; + state.updatedAt = ''; + uni.removeStorageSync(STORAGE_KEY); + } +}; + +// 对外动作:提供保存/读取/清空/采集能力 +const actions = { + // 手动设置设备信息 + setDeviceInfo({ commit }, payload) { + commit('SET_DEVICE_INFO', payload); + return true; + }, + /** + * 通用设备信息采集 + * @param {Object} options + * @param {Boolean} options.save - 是否自动保存到store和本地缓存,默认 true + * @returns {Object} baseDeviceInfo - 采集后的基础设备信息 + */ + collectBaseDeviceInfo({ dispatch }, options = {}) { + const { + save = true + } = options || {}; + + let systemInfo = {}; + try { + systemInfo = uni.getSystemInfoSync() || {}; + } catch (error) { + systemInfo = { + error: error && error.message ? error.message : 'getSystemInfoSync failed' + }; + } + + const baseDeviceInfo = { + brand: systemInfo.brand || '', + model: systemInfo.model || '', + system: systemInfo.system || '', + platform: systemInfo.platform || '', + deviceType: systemInfo.deviceType || '', + osName: systemInfo.osName || '', + osVersion: systemInfo.osVersion || '', + language: systemInfo.language || '', + screenWidth: systemInfo.screenWidth || 0, + screenHeight: systemInfo.screenHeight || 0, + windowWidth: systemInfo.windowWidth || 0, + windowHeight: systemInfo.windowHeight || 0, + statusBarHeight: systemInfo.statusBarHeight || 0, + pixelRatio: systemInfo.pixelRatio || 1, + safeArea: systemInfo.safeArea || null, + appVersion: systemInfo.appVersion || systemInfo.version || '', + deviceId: systemInfo.deviceId || '', + collectedAt: new Date().toISOString() + }; + + if (save) { + dispatch('setDeviceInfo', baseDeviceInfo); + } + + return baseDeviceInfo; + }, + // 获取当前设备信息 + getDeviceInfo({ state }) { + return state.deviceInfo; + }, + // 清空当前设备信息 + clearDeviceInfo({ commit }) { + commit('CLEAR_DEVICE_INFO'); + return true; + } +}; + +// 命名空间模块导出 +export default { + namespaced: true, + state, + getters, + mutations, + actions +}; + +/* +使用示例: + +1) 采集并自动保存基础设备信息(默认 save=true) +this.$store.dispatch('deviceInfo/collectBaseDeviceInfo'); + +2) 仅采集不保存 +const info = await this.$store.dispatch('deviceInfo/collectBaseDeviceInfo', { save: false }); + +3) 读取 / 清空 +const current = await this.$store.dispatch('deviceInfo/getDeviceInfo'); +await this.$store.dispatch('deviceInfo/clearDeviceInfo'); +*/