feat(store): add device info module with base device collection

This commit is contained in:
zm2000 2026-03-30 17:55:10 +08:00
parent 3762a36433
commit 4502a4f673
2 changed files with 129 additions and 1 deletions

View File

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

View File

@ -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');
*/