// 设备信息本地存储键 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() }; // 输出 PDA 的屏幕宽高与窗口宽高,以及像素比,供调试使用 console.log('[deviceInfo] screen size:', { screenWidth: baseDeviceInfo.screenWidth, screenHeight: baseDeviceInfo.screenHeight, windowWidth: baseDeviceInfo.windowWidth, windowHeight: baseDeviceInfo.windowHeight, pixelRatio: baseDeviceInfo.pixelRatio }); 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'); */