get-window-info.uvue 3.1 KB
Newer Older
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
1
<template>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
2 3 4 5 6 7
  <page-head :title="title"></page-head>
  <view class="uni-common-mt">
    <view class="uni-list">
      <view class="uni-list-cell" v-for="(item, _) in items" style="align-items: center">
        <view class="uni-pd">
          <view class="uni-label" style="width: 180px">{{ item.label }}</view>
DCloud-WZF's avatar
DCloud-WZF 已提交
8
        </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
9
        <view class="uni-list-cell-db">
雪洛's avatar
雪洛 已提交
10
          <text class="uni-list-cell-db-text">{{ item.value == '' ? '未获取' : item.value }}</text>
DCloud-WZF's avatar
DCloud-WZF 已提交
11 12 13
        </view>
      </view>
    </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
14 15 16 17
    <view class="uni-padding-wrap">
      <view class="uni-btn-v">
        <button type="primary" @tap="getWindowInfo">获取窗口信息</button>
      </view>
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
18 19 20 21 22 23 24
      <!-- #ifdef APP-ANDROID -->
      <view class="uni-btn-v">
        <navigator url="/pages/API/get-window-info/window-area">
          <button type="primary">窗口各区域示例</button>
        </navigator>
      </view>
      <!-- #endif -->
DCloud-yinjiacheng's avatar
DCloud-yinjiacheng 已提交
25
    </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
26
  </view>
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
27 28
</template>
<script>
29
  import { setStatusBarHeight, setSafeArea } from '@/store/index.uts'
30
  // #ifdef APP-ANDROID
31
  import type { SafeArea } from '@/store/index.uts'
32
  // #endif
DCloud-WZF's avatar
DCloud-WZF 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45

  type Item = {
    label : string,
    value : string,
  }
  export default {
    data() {
      return {
        title: 'getWindowInfo',
        items: [] as Item[],
      }
    },
    onUnload: function () {
46 47 48
    },
    onReady() {
      this.getWindowInfo()
DCloud-WZF's avatar
DCloud-WZF 已提交
49 50 51 52
    },
    methods: {
      getWindowInfo: function () {
        const res = uni.getWindowInfo();
53
        // 获取状态栏高度, 供截图对比使用
54
        setStatusBarHeight(res.statusBarHeight);
55
        // 获取安全区信息,供截图使用
56 57 58 59 60 61 62 63
        // #ifdef APP-ANDROID
        setSafeArea({
          top: res.safeArea.top,
          left: res.safeArea.left,
          right: res.safeArea.right,
          bottom: res.safeArea.bottom,
          width: res.safeArea.width,
          height: res.safeArea.height,
64
        } as SafeArea);
65 66 67 68 69 70 71 72 73 74 75
        // #endif
        // #ifdef APP-IOS
        setSafeArea({
          top: res.safeArea.top,
          left: res.safeArea.left,
          right: res.safeArea.right,
          bottom: res.safeArea.bottom,
          width: res.safeArea.width,
          height: res.safeArea.height,
        });
        // #endif
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        this.items = [] as Item[];

        const res_str = JSON.stringify(res);
        const res_obj = JSON.parseObject(res_str);
        const res_map = res_obj!.toMap();
        let keys = [] as string[]
        res_map.forEach((_, key) => {
          keys.push(key);
        });
        keys.sort().forEach(key => {
          const value = res[key];
          if (value != null) {
            const item = {
              label: key,
              value: "" + ((typeof value == "object") ? JSON.stringify(value) : value)
            } as Item;
            this.items.push(item);
          }
94
        });
DCloud-WZF's avatar
DCloud-WZF 已提交
95 96 97 98
      },
      //自动化测试例专用
      jest_getWindowInfo() : GetWindowInfoResult {
        return uni.getWindowInfo();
99
      },
DCloud-WZF's avatar
DCloud-WZF 已提交
100 101
    }
  }
taohebin@dcloud.io's avatar
taohebin@dcloud.io 已提交
102 103 104
</script>

<style>
DCloud-WZF's avatar
DCloud-WZF 已提交
105
  .uni-pd {
H
hdx 已提交
106
    padding-left: 15px;
DCloud-WZF's avatar
DCloud-WZF 已提交
107 108
  }
</style>