/** * 系统与窗口信息工具 * 兼容微信小程序新版规范(废弃 wx.getSystemInfoSync,推荐 getWindowInfo) */ export function getWindowInfoSafe() { try { if (typeof uni.getWindowInfo === 'function') { const win = uni.getWindowInfo(); if (win && (win.statusBarHeight !== undefined || win.windowWidth !== undefined)) { return win; } } if (typeof uni.getSystemInfoSync === 'function') { return uni.getSystemInfoSync(); } } catch (e) { // 忽略异常,降级兜底 } return { statusBarHeight: 20, windowWidth: 375, windowHeight: 667, screenWidth: 375, screenHeight: 667 }; } /** * 统一获取自定义导航栏精确尺寸(确保全站 TopBar 与页面吸顶元素 100% 零误差对齐, * 并防御微信开发者工具切换模拟器机型时 statusBarHeight 与胶囊 menu.top 不匹配的异常) */ export function getNavBarMetrics() { let statusBarHeight = 20; let navBarHeight = 44; let rightSpaceWidth = 16; let leftWidth = 50; try { const sys = getWindowInfoSafe(); const winWidth = (sys && sys.windowWidth) ? sys.windowWidth : 375; if (sys && sys.statusBarHeight) { statusBarHeight = sys.statusBarHeight; } // #ifdef MP-WEIXIN if (typeof uni.getMenuButtonBoundingClientRect === 'function') { const menu = uni.getMenuButtonBoundingClientRect(); if (menu && menu.top > 0 && menu.height > 0 && menu.width > 0) { // 校验 statusBarHeight 与胶囊 top 的间距是否在合理范围 (4px~12px) // 若模拟器切换机型导致 statusBarHeight >= menu.top,则以胶囊真实物理位置反推,确保标题与胶囊永远同轴居中且不贴底 let gap = menu.top - statusBarHeight; if (gap < 4 || gap > 12) { gap = 6; statusBarHeight = Math.max(20, menu.top - gap); } navBarHeight = menu.height + gap * 2; const rightMargin = (winWidth - menu.right) > 0 ? (winWidth - menu.right) : 7; rightSpaceWidth = menu.width + rightMargin * 2; leftWidth = Math.max(50, rightSpaceWidth); } } // #endif } catch (e) { statusBarHeight = 20; navBarHeight = 44; rightSpaceWidth = 16; leftWidth = 50; } return { statusBarHeight, navBarHeight, totalNavHeight: statusBarHeight + navBarHeight, rightSpaceWidth, leftWidth }; } export default { getWindowInfoSafe, getNavBarMetrics };