HomeLease/utils/system.js

57 lines
1.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 获取系统信息对象(同步获取,只获取一次避免重复调用开销)
*/
const SYSTEM_INFO = uni.getSystemInfoSync()
/**
* 获取状态栏高度单位px
* @returns {number} 状态栏高度默认兜底值15px
*/
export const getStatusBarHeight = () => SYSTEM_INFO.statusBarHeight || 15
/**
* 获取标题栏高度(包含状态栏到标题栏底部的总高度)
* @returns {number} 标题栏高度单位px
* @description
* - 小程序端通过胶囊按钮位置计算
* - 非小程序端返回默认值40px
*/
export const getTitleBarHeight = () => {
// 检查是否在小程序环境(支持获取菜单按钮位置)
if (uni.getMenuButtonBoundingClientRect) {
// 获取小程序菜单按钮(胶囊按钮)的布局信息
let { top, height } = uni.getMenuButtonBoundingClientRect()
// 计算公式:按钮高度 + (按钮顶部到状态栏底部的距离) * 2
return height + (top - getStatusBarHeight()) * 2
} else {
// 非小程序环境如H5/App返回默认高度
return 40
}
}
/**
* 获取整个导航栏高度(状态栏 + 标题栏)
* @returns {number} 导航栏总高度单位px
*/
export const getNavBarHeight = () => getStatusBarHeight() + getTitleBarHeight()
/**
* 获取左侧返回按钮的右侧边界坐标(用于特殊平台布局计算)
* @returns {number} 左侧图标右侧的X坐标单位px
* @platform 今日头条小程序专属逻辑
*/
export const getLeftIconLeft = () => {
// #ifdef MP-TOUTIAO
// 今日头条小程序获取自定义按钮位置信息
let {
leftIcon: { left, width },
} = tt.getCustomButtonBoundingClientRect()
return left + parseInt(width) // 返回按钮右侧边界坐标
// #endif
// #ifndef MP-TOUTIAO
// 其他平台返回0表示不需要特殊处理
return 0
// #endif
}