当前位置:   article > 正文

获取微信小程序系统信息_小程序 getsysteminfo

小程序 getsysteminfo

问题

获取微信小程序系统信息(版本、端、系统、导航高度等等)???

  1. function getCapsulePosition(
  2. systemInfo,
  3. ios,
  4. ) {
  5. let rect;
  6. try {
  7. rect = wx.getMenuButtonBoundingClientRect()
  8. ? wx.getMenuButtonBoundingClientRect()
  9. : ({});
  10. if (rect == null) {
  11. throw new Error('erroer');
  12. }
  13. // 取值为0的情况 有可能width不为0 top为0的情况
  14. if (!rect.width || !rect.top || !rect.left || !rect.height) {
  15. throw new Error('erroer');
  16. }
  17. } catch (e) {
  18. let gap = 0; // 胶囊按钮上下间距 使导航内容居中
  19. let width = 96; // 胶囊的宽度
  20. if (systemInfo.platform === 'android') {
  21. gap = 8;
  22. width = 96;
  23. } else if (systemInfo.platform === 'devtools') {
  24. if (ios) {
  25. gap = 5.5; // 开发工具中ios手机
  26. } else {
  27. gap = 7.5; // 开发工具中android和其他手机
  28. }
  29. } else {
  30. gap = 4;
  31. width = 88;
  32. }
  33. rect = {
  34. // 获取不到胶囊信息就自定义重置一个
  35. bottom: systemInfo.statusBarHeight + gap + 32,
  36. height: 32,
  37. left: systemInfo.windowWidth - width - 10,
  38. right: systemInfo.windowWidth - 10,
  39. top: systemInfo.statusBarHeight + gap,
  40. width,
  41. };
  42. }
  43. return rect;
  44. }
  45. export const getSystemInfo = () =>{
  46. let navBarExtendHeight = 0;
  47. if (wx.getSystemInfoSync()) {
  48. const systemInfo = wx.getSystemInfoSync()
  49. const ios = !!(systemInfo.system.toLowerCase().search('ios') + 1);
  50. const rect = getCapsulePosition(systemInfo, ios);
  51. let navBarHeight = 0;
  52. if (!systemInfo.statusBarHeight) {
  53. // 开启wifi和打电话下
  54. systemInfo.statusBarHeight = systemInfo.screenHeight - systemInfo.windowHeight - 20;
  55. navBarHeight = (function(): number {
  56. const gap = rect.top - systemInfo.statusBarHeight;
  57. return 2 * gap + rect.height;
  58. })();
  59. systemInfo.statusBarHeight = 0;
  60. navBarExtendHeight = 0;
  61. } else {
  62. navBarHeight = (function(): number {
  63. const gap = rect.top - systemInfo.statusBarHeight;
  64. return systemInfo.statusBarHeight + 2 * gap + rect.height;
  65. })();
  66. if (ios) {
  67. navBarExtendHeight = 4;
  68. } else {
  69. navBarExtendHeight = 0;
  70. }
  71. }
  72. return {
  73. ...systemInfo,
  74. navBarExtendHeight,
  75. navBarHeight,
  76. capsulePosition:rect,
  77. ios:ios
  78. };
  79. } else {
  80. const rect = getCapsulePosition({}, false);
  81. const height = (function(): number {
  82. const gap = rect.top - 30;
  83. return 48 + 2 * gap + rect.height;
  84. })();
  85. return {
  86. ios: false,
  87. capsulePosition: rect,
  88. navBarHeight: height,
  89. navBarExtendHeight: 0,
  90. };
  91. }
  92. }

通过getSystemInfo方法便可以获得当前微信小程序的信息。在原生微信小程序APIwx.getSystemInfoSync()获得信息的基础上做了适当的删减,查看实际需要的信息;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/684856
推荐阅读
相关标签
  

闽ICP备14008679号