From b0b0d7b99890f85e3bb9e97fdc53dc68e71d74e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=86=8A=E7=86=8A=E7=86=8A=E5=AD=90=E8=B7=AF?= Date: Fri, 16 Jan 2026 10:43:34 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=B1=9E=E6=80=A7=E5=92=8C=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E9=80=BB=E8=BE=91=E4=BB=A5=E6=94=AF=E6=8C=81=E5=AF=BC?= =?UTF-8?q?=E8=88=AA=E6=A0=8F=E5=92=8C=E5=AE=89=E5=85=A8=E5=8C=BA=E5=9F=9F?= =?UTF-8?q?=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- miniprogram/app.ts | 45 +++++++++++++++++++++++++++++++++++++++++++-- typings/index.d.ts | 12 +++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/miniprogram/app.ts b/miniprogram/app.ts index 25d3d1e..4fbb5a2 100644 --- a/miniprogram/app.ts +++ b/miniprogram/app.ts @@ -1,5 +1,46 @@ // app.ts App({ - globalData: {}, - onLaunch() {}, + globalData: { + navBarHeight: 0, // 导航栏总高度 + navBarContentHeight: 0, // 胶囊那一栏的高度(不含状态栏) + statusBarHeight: 0, // 状态栏高度 + menuRight: 0, // 胶囊右边距 + menuTop: 0, // 胶囊顶部距离 + menuHeight: 0, // 胶囊高度 + tabbarHeight: 0, // 底部导航栏高度 + safeAreaBottom: 0, // 底部安全区域坐标 + safeAreaInsetBottom: 0, // 中间区域内容的底边坐标 + system: '', // 系统信息 + token: '', + }, + onLaunch() { + // 1. 获取胶囊按钮位置信息 + const menuButtonInfo = wx.getMenuButtonBoundingClientRect() + + // 2. 获取系统窗口信息 + const windowInfo = wx.getWindowInfo() + const deviceInfo = wx.getDeviceInfo() + + // 赋值基础信息 + this.globalData.system = deviceInfo.system + this.globalData.menuRight = windowInfo.screenWidth - menuButtonInfo.right + this.globalData.menuTop = menuButtonInfo.top + this.globalData.menuHeight = menuButtonInfo.height + this.globalData.statusBarHeight = windowInfo.statusBarHeight + this.globalData.safeAreaBottom = windowInfo.safeArea.bottom + + // 计算导航栏内容区域高度(不含状态栏) + // 解释:(胶囊上边距 - 状态栏高度) 是胶囊距离状态栏的间隙,上下各有一个间隙,所以 * 2 + const gap = menuButtonInfo.top - windowInfo.statusBarHeight + const navContentHeight = menuButtonInfo.height + gap * 2 + this.globalData.navBarContentHeight = navContentHeight + + // 计算导航栏+手机状态栏高度 + // 计算公式:导航栏高度 = 状态栏高度 + (胶囊上边距 - 状态栏高度) * 2 + 胶囊高度 + this.globalData.navBarHeight = windowInfo.statusBarHeight + navContentHeight + + // 计算底部小黑条高度 (屏幕高度 - 安全区域底部坐标) + const safeAreaInsetBottom = windowInfo.screenHeight - windowInfo.safeArea.bottom + this.globalData.safeAreaInsetBottom = safeAreaInsetBottom + }, }) diff --git a/typings/index.d.ts b/typings/index.d.ts index 232d149..1e43cfc 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -3,8 +3,18 @@ interface IAppOption { globalData: { + navBarHeight: number, + navBarContentHeight: number, + statusBarHeight: number, + menuRight: number, + menuTop: number, + menuHeight: number, + tabbarHeight: number, + safeAreaBottom: number, + safeAreaInsetBottom: number + system: string, + token: string userInfo?: WechatMiniprogram.UserInfo - token?: string } userInfoReadyCallback?: WechatMiniprogram.GetUserInfoSuccessCallback }