uniapp小程序获取手机设备安全距离

utils.js

复制代码
let systemInfo = null;

export const getSystemInfo = () => {
  if (!systemInfo) {
    systemInfo = uni.getSystemInfoSync();
    // 补充安全区域默认值
    systemInfo.safeAreaInsets = systemInfo.safeAreaInsets || {
      top: 0,
      bottom: 0,
      left: 0,
      right: 0
    };
    // 确保statusBarHeight存在(兼容旧版本)
    systemInfo.statusBarHeight = systemInfo.statusBarHeight || 
      (systemInfo.platform === 'android' ? 25 : 20);
  }
  return systemInfo;
};

// 获取状态栏高度(px)
export const getStatusBarHeight = () => getSystemInfo().statusBarHeight;

// 获取iPhone底部下巴高度(px)
export const getIphoneBottomHeight = () => {
  const { platform, safeAreaInsets } = getSystemInfo();
  return platform === 'ios' ? safeAreaInsets.bottom : 0;
};

// 是否全面屏设备
export const isNotchScreen = () => {
  const { model, statusBarHeight } = getSystemInfo();
  return statusBarHeight > 20 || /iphone x|iphone 1[1-5]/i.test(model);
};

import { getSystemInfo, getStatusBarHeight, getIphoneBottomHeight } from '@/utils/device';

复制代码
  computed: {
    // 底部安全区域高度(单位:px)
    bottomSafeArea() {
      return getIphoneBottomHeight();
    },
    // 转换为rpx单位
    bottomSafeAreaRpx() {
      return this.bottomSafeArea * (750 / this.deviceInfo.windowWidth);
    }
  },
  created() {
    // 获取设备状态栏高度
    const { statusBarHeight } = uni.getSystemInfoSync();
    this.navBarHeight = statusBarHeight;
  },
相关推荐
济南壹软网络科技有限公司3 小时前
国际盲盒电商新范式:深度剖析 V4 系统的全球化云原生架构
uni-app·盲盒源码·国际盲盒源码·盲盒开源源码·多语言盲盒源码
项目題供诗6 小时前
微信小程序黑马优购(项目)(七)
微信小程序·小程序
2501_915918417 小时前
HTTPS 端口深度解析,443 并不是唯一入口,理解 TLS 流量行为与抓包策略
网络协议·http·ios·小程序·https·uni-app·iphone
2501_916008897 小时前
iOS 开发者工具全景图,构建从编码、调试到性能诊断的多层级工程化工具体系
android·ios·小程序·https·uni-app·iphone·webview
走,带你去玩8 小时前
uniapp live-pusher + 腾讯云直播
前端·javascript·uni-app
qq_12498707538 小时前
基于微信小程序的家电维修平台的设计与实现(源码+论文+部署+安装)
微信小程序·小程序·毕业设计
2501_915921439 小时前
从需求到上架,现代 iOS 开发流程的工程化方法论
android·ios·小程序·https·uni-app·iphone·webview
oxygen-120410 小时前
uniapp 锚点跳转
uni-app
脾气有点小暴10 小时前
UniApp实现刷新当前页面
开发语言·前端·javascript·vue.js·uni-app
济南壹软网络科技有限公司10 小时前
掘金国际盲盒电商:UniApp + ThinkPHP6 构建的全球化技术基石
uni-app·开源·盲盒源码·国际盲盒