swan.getSystemInfo

    解释:获取系统信息

    Object object

    success返回参数说明

    扫码体验

    请使用百度APP扫码

    swan.getSystemInfo - 图4

    代码示例1 - 属性全集 :

    • 在 swan 文件中
    • 在 js 文件中
    1. data: {
    2. infoList: [{
    3. label: '手机品牌',
    4. key: 'brand',
    5. value: ''
    6. }, {
    7. label: '手机型号',
    8. key: 'model',
    9. value: ''
    10. }, {
    11. label: '使用窗口宽',
    12. key: 'windowWidth',
    13. value: ''
    14. }, {
    15. label: '使用窗口高',
    16. key: 'windowHeight',
    17. value: ''
    18. },{
    19. label: '状态栏高',
    20. key: 'statusBarHeight',
    21. value: ''
    22. },{
    23. label: '导航栏高',
    24. key: 'navigationBarHeight',
    25. value: ''
    26. }, {
    27. label: '宿主版本',
    28. key: 'version',
    29. value: ''
    30. }, {
    31. label: '操作系统',
    32. key: 'system',
    33. value: ''
    34. }, {
    35. label: '客户端平台',
    36. value: ''
    37. }, {
    38. label: '屏幕宽度',
    39. key: 'screenWidth',
    40. value: ''
    41. }, {
    42. label: '屏幕高度',
    43. key: 'screenHeight',
    44. }, {
    45. label: '用户字体',
    46. key: 'fontSizeSetting',
    47. value: ''
    48. }, {
    49. label: '基础库版本',
    50. key: 'SDKVersion',
    51. value: ''
    52. }, {
    53. label: '宿主平台',
    54. key: 'host',
    55. value: ''
    56. }, {
    57. label: '宿主平台版本',
    58. key: 'swanNativeVersion',
    59. value: ''
    60. }, {
    61. label: '屏幕密度',
    62. key: 'devicePixelRatio',
    63. value: ''
    64. }, {
    65. label: 'DPI',
    66. key: 'pixelRatio',
    67. value: ''
    68. }, {
    69. label: '宿主语言',
    70. key: 'language',
    71. value: ''
    72. }]
    73. },
    74. onLoad(e) {
    75. },
    76. getSystemInfo(e) {
    77. swan.getSystemInfo({
    78. success: res => {
    79. console.log('res', res);
    80. // 更新数据
    81. this.updateInfoList(res);
    82. },
    83. fail: err => {
    84. swan.showToast({
    85. });
    86. }
    87. },
    88. updateInfoList(res) {
    89. let infoList = this.getData('infoList');
    90. for (let i = 0; i < infoList.length; ++i) {
    91. if (res[infoList[i].key] === '') {
    92. infoList[i].value = '暂无';
    93. } else {
    94. infoList[i].value = res[infoList[i].key];
    95. }
    96. }
    97. this.setData('infoList', infoList);
    98. }
    99. });

    代码示例2: 开发者一般在模拟顶部导航栏时用到statusBarHeight属性 :

    • 在 swan 文件中(代码链接中为自定义组件写法,可进行多页面复用)
    • 在 js 文件中
    1. // 也可用同步写法
    2. swan.getSystemInfo({
    3. success: res => {
    4. console.log('getSystemInfo success', res);
    5. this.setData({
    6. 'statusBarHeight': res.statusBarHeight
    7. });
    8. },
    9. fail: err => {
    10. console.log('getSystemInfo fail', err);
    11. }
    12. });

    • 在 swan 文件中
    • 在 js 文件中
    1. Page({
    2. data: { },
    3. getSystemInfo(e) {
    4. swan.getSystemInfo({
    5. success: res => {
    6. console.log('res', res);
    7. this.setData('screenHeight', res.screenHeight)
    8. this.setData('windowHeight', res.windowHeight)
    9. this.setData('safeArea',
    10. res.safeArea.height)
    11. },
    12. fail: err => {
    13. swan.showToast({
    14. title: '获取失败'
    15. });
    16. }
    17. });
    18. }

    代码示例4: 适配iphoneX等机型 :

    或参见百度通用

    在开发者工具中预览效果

    • 在 js 文件中

    Android