/** * 应用状态管理模块 * 管理系统信息、导航栏高度等全局状态 */ import { defineStore } from 'pinia' export const useAppStore = defineStore('app', { state: () => ({ // 状态栏高度 statusBarHeight: 20, // 导航栏高度(不含状态栏) navbarHeight: 44, // 系统信息 systemInfo: {}, // 是否已初始化 initialized: false }), getters: { /** * 导航栏总高度(状态栏 + 导航栏) */ totalNavbarHeight: (state) => state.statusBarHeight + state.navbarHeight, /** * 内容区域顶部padding */ contentPaddingTop: (state) => state.statusBarHeight + state.navbarHeight, /** * 是否是iOS系统 */ isIOS: (state) => state.systemInfo.platform === 'ios', /** * 是否是Android系统 */ isAndroid: (state) => state.systemInfo.platform === 'android' }, actions: { /** * 初始化系统信息 */ initSystemInfo() { if (this.initialized) return try { const systemInfo = uni.getSystemInfoSync() this.systemInfo = systemInfo this.statusBarHeight = systemInfo.statusBarHeight || 20 // 获取胶囊按钮位置信息(微信小程序) // #ifdef MP-WEIXIN const menuButtonInfo = uni.getMenuButtonBoundingClientRect() if (menuButtonInfo) { // 导航栏高度 = 胶囊底部距离 - 状态栏高度 + 胶囊与底部的间距 this.navbarHeight = (menuButtonInfo.bottom - this.statusBarHeight) + (menuButtonInfo.top - this.statusBarHeight) } // #endif this.initialized = true } catch (e) { console.error('获取系统信息失败:', e) } }, /** * 获取安全区域底部高度 * @returns {number} */ getSafeAreaBottom() { return this.systemInfo.safeAreaInsets?.bottom || 0 } } })