get-window-info.uvue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <page-head :title="title"></page-head>
  3. <view class="uni-common-mt">
  4. <view class="uni-list">
  5. <view class="uni-list-cell" v-for="(item, _) in items" style="align-items: center">
  6. <view class="uni-pd">
  7. <view class="uni-label" style="width: 180px">{{ item.label }}</view>
  8. </view>
  9. <view class="uni-list-cell-db">
  10. <text class="uni-list-cell-db-text">{{ item.value == '' ? '未获取' : item.value }}</text>
  11. </view>
  12. </view>
  13. </view>
  14. <view class="uni-padding-wrap">
  15. <view class="uni-btn-v">
  16. <button type="primary" @tap="getWindowInfo">获取窗口信息</button>
  17. </view>
  18. <!-- #ifdef APP-ANDROID -->
  19. <view class="uni-btn-v">
  20. <navigator url="/pages/API/get-window-info/window-area">
  21. <button type="primary">窗口各区域示例</button>
  22. </navigator>
  23. </view>
  24. <!-- #endif -->
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. import { setStatusBarHeight, setSafeArea } from '@/store/index.uts'
  30. // #ifdef APP-ANDROID
  31. import type { SafeArea } from '@/store/index.uts'
  32. // #endif
  33. type Item = {
  34. label : string,
  35. value : string,
  36. }
  37. export default {
  38. data() {
  39. return {
  40. title: 'getWindowInfo',
  41. items: [] as Item[],
  42. }
  43. },
  44. onUnload: function () {
  45. },
  46. onReady() {
  47. this.getWindowInfo()
  48. },
  49. methods: {
  50. getWindowInfo: function () {
  51. const res = uni.getWindowInfo();
  52. // 获取状态栏高度, 供截图对比使用
  53. setStatusBarHeight(res.statusBarHeight);
  54. // 获取安全区信息,供截图使用
  55. // #ifdef APP-ANDROID
  56. setSafeArea({
  57. top: res.safeArea.top,
  58. left: res.safeArea.left,
  59. right: res.safeArea.right,
  60. bottom: res.safeArea.bottom,
  61. width: res.safeArea.width,
  62. height: res.safeArea.height,
  63. } as SafeArea);
  64. // #endif
  65. // #ifdef APP-IOS
  66. setSafeArea({
  67. top: res.safeArea.top,
  68. left: res.safeArea.left,
  69. right: res.safeArea.right,
  70. bottom: res.safeArea.bottom,
  71. width: res.safeArea.width,
  72. height: res.safeArea.height,
  73. });
  74. // #endif
  75. this.items = [] as Item[];
  76. const res_str = JSON.stringify(res);
  77. const res_obj = JSON.parseObject(res_str);
  78. const res_map = res_obj!.toMap();
  79. let keys = [] as string[]
  80. res_map.forEach((_, key) => {
  81. keys.push(key);
  82. });
  83. keys.sort().forEach(key => {
  84. const value = res[key];
  85. if (value != null) {
  86. const item = {
  87. label: key,
  88. value: "" + ((typeof value == "object") ? JSON.stringify(value) : value)
  89. } as Item;
  90. this.items.push(item);
  91. }
  92. });
  93. },
  94. }
  95. }
  96. </script>
  97. <style>
  98. .uni-pd {
  99. padding-left: 15px;
  100. }
  101. </style>