赞
踩
获取微信小程序系统信息(版本、端、系统、导航高度等等)???
- function getCapsulePosition(
- systemInfo,
- ios,
- ) {
- let rect;
- try {
- rect = wx.getMenuButtonBoundingClientRect()
- ? wx.getMenuButtonBoundingClientRect()
- : ({});
- if (rect == null) {
- throw new Error('erroer');
- }
- // 取值为0的情况 有可能width不为0 top为0的情况
- if (!rect.width || !rect.top || !rect.left || !rect.height) {
- throw new Error('erroer');
- }
- } catch (e) {
- let gap = 0; // 胶囊按钮上下间距 使导航内容居中
- let width = 96; // 胶囊的宽度
- if (systemInfo.platform === 'android') {
- gap = 8;
- width = 96;
- } else if (systemInfo.platform === 'devtools') {
- if (ios) {
- gap = 5.5; // 开发工具中ios手机
- } else {
- gap = 7.5; // 开发工具中android和其他手机
- }
- } else {
- gap = 4;
- width = 88;
- }
- rect = {
- // 获取不到胶囊信息就自定义重置一个
- bottom: systemInfo.statusBarHeight + gap + 32,
- height: 32,
- left: systemInfo.windowWidth - width - 10,
- right: systemInfo.windowWidth - 10,
- top: systemInfo.statusBarHeight + gap,
- width,
- };
- }
-
- return rect;
- }
-
- export const getSystemInfo = () =>{
- let navBarExtendHeight = 0;
- if (wx.getSystemInfoSync()) {
- const systemInfo = wx.getSystemInfoSync()
- const ios = !!(systemInfo.system.toLowerCase().search('ios') + 1);
- const rect = getCapsulePosition(systemInfo, ios);
- let navBarHeight = 0;
- if (!systemInfo.statusBarHeight) {
- // 开启wifi和打电话下
- systemInfo.statusBarHeight = systemInfo.screenHeight - systemInfo.windowHeight - 20;
- navBarHeight = (function(): number {
- const gap = rect.top - systemInfo.statusBarHeight;
- return 2 * gap + rect.height;
- })();
-
- systemInfo.statusBarHeight = 0;
- navBarExtendHeight = 0;
- } else {
- navBarHeight = (function(): number {
- const gap = rect.top - systemInfo.statusBarHeight;
- return systemInfo.statusBarHeight + 2 * gap + rect.height;
- })();
- if (ios) {
- navBarExtendHeight = 4;
- } else {
- navBarExtendHeight = 0;
- }
- }
-
- return {
- ...systemInfo,
- navBarExtendHeight,
- navBarHeight,
- capsulePosition:rect,
- ios:ios
- };
- } else {
- const rect = getCapsulePosition({}, false);
- const height = (function(): number {
- const gap = rect.top - 30;
- return 48 + 2 * gap + rect.height;
- })();
- return {
- ios: false,
- capsulePosition: rect,
- navBarHeight: height,
- navBarExtendHeight: 0,
- };
- }
- }
通过getSystemInfo方法便可以获得当前微信小程序的信息。在原生微信小程序API的wx.getSystemInfoSync()获得信息的基础上做了适当的删减,查看实际需要的信息;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。