| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- /* eslint-disable import/no-mutable-exports */
- // 获取屏幕边界到安全区域距离
- let systemInfo
- let safeAreaInsets
- const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
- // #ifdef MP-WEIXIN
- // 微信小程序使用新的API
- systemInfo = uni.getWindowInfo()
- safeAreaInsets = systemInfo.safeArea
- ? {
- top: systemInfo.safeArea.top,
- right: systemInfo.windowWidth - systemInfo.safeArea.right,
- bottom: systemInfo.windowHeight - systemInfo.safeArea.bottom,
- left: systemInfo.safeArea.left,
- }
- : null
- // #endif
- // #ifndef MP-WEIXIN
- // 其他平台继续使用uni API
- systemInfo = uni.getSystemInfoSync()
- safeAreaInsets = systemInfo.safeAreaInsets
- // #endif
- console.log('systemInfo', systemInfo)
- // 微信里面打印
- // pixelRatio: 3
- // safeArea: {top: 47, left: 0, right: 390, bottom: 810, width: 390, …}
- // safeAreaInsets: {top: 47, left: 0, right: 0, bottom: 34}
- // screenHeight: 844
- // screenTop: 91
- // screenWidth: 390
- // statusBarHeight: 47
- // windowBottom: 0
- // windowHeight: 753
- // windowTop: 0
- // windowWidth: 390
- /**
- * 将 rpx 转换为 px
- * @param rpx rpx值
- * @returns px值
- */
- export function rpxToPx(rpx: number): number {
- // rpx基准是750rpx等于屏幕宽度
- const screenWidth = systemInfo?.windowWidth || 375 // 默认375px作为 fallback
- return (rpx * screenWidth) / 750
- }
- /**
- * 将 px 转换为 rpx
- * @param px px值
- * @returns rpx值
- */
- export function pxToRpx(px: number): number {
- // rpx基准是750rpx等于屏幕宽度
- const screenWidth = systemInfo?.windowWidth || 375 // 默认375px作为 fallback
- return (px * 750) / screenWidth
- }
- export { menuButtonInfo, safeAreaInsets, systemInfo }
|