get-system-info.uvue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view style="flex: 1">
  4. <!-- #endif -->
  5. <view>
  6. <page-head :title="title"></page-head>
  7. <view class="uni-common-mt">
  8. <view class="uni-list">
  9. <view class="uni-list-cell" v-for="(item, _) in items" style="align-items: center">
  10. <view class="uni-pd">
  11. <view class="uni-label" style="width: 180px">{{
  12. item.label
  13. }}</view>
  14. </view>
  15. <view class="uni-list-cell-db">
  16. <text class="uni-list-cell-db-text">{{ item.value == '' ? '未获取' : item.value }}</text>
  17. </view>
  18. </view>
  19. </view>
  20. <view class="uni-padding-wrap">
  21. <view class="uni-btn-v">
  22. <button type="primary" @tap="getSystemInfoSync">
  23. 同步获取设备系统信息
  24. </button>
  25. <button type="primary" @tap="getSystemInfo" style="margin-top: 20px;">
  26. 异步获取设备系统信息
  27. </button>
  28. </view>
  29. </view>
  30. </view>
  31. </view>
  32. <!-- #ifdef APP -->
  33. </scroll-view>
  34. <!-- #endif -->
  35. </template>
  36. <script>
  37. type Item = {
  38. label : string,
  39. value : string,
  40. }
  41. let globalScreenHeight = 0
  42. try {
  43. globalScreenHeight = uni.getWindowInfo().screenHeight
  44. } catch (e) {
  45. // 兼容本地测试
  46. console.error(e)
  47. }
  48. export default {
  49. data() {
  50. return {
  51. title: 'getSystemInfo',
  52. items: [] as Item[],
  53. screenHeightAtReady: 0,
  54. jest_result: false,
  55. }
  56. },
  57. onUnload: function () {
  58. },
  59. onReady() {
  60. this.screenHeightAtReady = uni.getSystemInfoSync().screenHeight
  61. console.log(`全局获取屏幕高度: ${globalScreenHeight} onReady内获取屏幕高度: ${this.screenHeightAtReady}`);
  62. },
  63. methods: {
  64. getSystemInfo: function () {
  65. uni.getSystemInfo({
  66. success: (res) => {
  67. this.items = [] as Item[];
  68. const res_str = JSON.stringify(res);
  69. const res_obj = JSON.parseObject(res_str);
  70. const res_map = res_obj!.toMap();
  71. let keys = [] as string[]
  72. res_map.forEach((_, key) => {
  73. keys.push(key);
  74. });
  75. keys.sort().forEach(key => {
  76. const value = res[key];
  77. if (value != null) {
  78. const item = {
  79. label: key,
  80. value: "" + ((typeof value == "object") ? JSON.stringify(value) : value)
  81. } as Item;
  82. this.items.push(item);
  83. }
  84. });
  85. },
  86. })
  87. },
  88. getSystemInfoSync: function () {
  89. this.items = [] as Item[];
  90. const res = uni.getSystemInfoSync()
  91. const res_str = JSON.stringify(res);
  92. const res_obj = JSON.parseObject(res_str);
  93. const res_map = res_obj!.toMap();
  94. let keys = [] as string[]
  95. res_map.forEach((_, key) => {
  96. keys.push(key);
  97. });
  98. keys.sort().forEach(key => {
  99. const value = res[key];
  100. if (value != null) {
  101. const item = {
  102. label: key,
  103. value: "" + ((typeof value == "object") ? JSON.stringify(value) : value)
  104. } as Item;
  105. this.items.push(item);
  106. }
  107. });
  108. },
  109. //自动化测试例专用
  110. jest_getSystemInfo() : GetSystemInfoResult {
  111. return uni.getSystemInfoSync();
  112. },
  113. jest_getScreenHeight_at_different_stages() {
  114. this.jest_result = (globalScreenHeight == this.screenHeightAtReady)
  115. }
  116. }
  117. }
  118. </script>
  119. <style>
  120. .uni-pd {
  121. padding-left: 15px;
  122. }
  123. </style>