get-system-info.uvue 3.7 KB
Newer Older
1
<template>
DCloud-WZF's avatar
DCloud-WZF 已提交
2 3
  <!-- #ifdef APP -->
  <scroll-view style="flex: 1">
H
hdx 已提交
4
  <!-- #endif -->
DCloud-WZF's avatar
DCloud-WZF 已提交
5 6 7 8
    <view>
      <page-head :title="title"></page-head>
      <view class="uni-common-mt">
        <view class="uni-list">
H
hdx 已提交
9
          <view class="uni-list-cell" v-for="(item, _) in items" style="align-items: center">
DCloud-WZF's avatar
DCloud-WZF 已提交
10 11 12 13 14 15
            <view class="uni-pd">
              <view class="uni-label" style="width: 180px">{{
                item.label
              }}</view>
            </view>
            <view class="uni-list-cell-db">
雪洛's avatar
雪洛 已提交
16
              <text class="uni-list-cell-db-text">{{ item.value == '' ? '未获取' : item.value }}</text>
DCloud-WZF's avatar
DCloud-WZF 已提交
17 18 19 20 21
            </view>
          </view>
        </view>
        <view class="uni-padding-wrap">
          <view class="uni-btn-v">
22 23
            <button type="primary" @tap="getSystemInfoSync">
              同步获取设备系统信息
H
hdx 已提交
24 25 26
            </button>
            <button type="primary" @tap="getSystemInfo" style="margin-top: 20px;">
              异步获取设备系统信息
DCloud-WZF's avatar
DCloud-WZF 已提交
27 28 29 30 31
            </button>
          </view>
        </view>
      </view>
    </view>
H
hdx 已提交
32
  <!-- #ifdef APP -->
DCloud-WZF's avatar
DCloud-WZF 已提交
33 34
  </scroll-view>
  <!-- #endif -->
35
</template>
36
<script>
H
hdx 已提交
37 38 39
  type Item = {
    label : string,
    value : string,
40 41 42
  }

  let globalScreenHeight = 0
43 44
  try {
    globalScreenHeight = uni.getWindowInfo().screenHeight
45 46 47 48 49
  } catch (e) {
    // 兼容本地测试
    console.error(e)
  }

H
hdx 已提交
50 51 52 53
  export default {
    data() {
      return {
        title: 'getSystemInfo',
54 55 56
        items: [] as Item[],
        screenHeightAtReady: 0,
        jest_result: false,
H
hdx 已提交
57 58 59
      }
    },
    onUnload: function () {
60 61 62 63
    },
    onReady() {
      this.screenHeightAtReady = uni.getSystemInfoSync().screenHeight
      console.log(`全局获取屏幕高度: ${globalScreenHeight}  onReady内获取屏幕高度: ${this.screenHeightAtReady}`);
H
hdx 已提交
64 65 66 67 68
    },
    methods: {
      getSystemInfo: function () {
        uni.getSystemInfo({
          success: (res) => {
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
            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);
              }
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
86
            });
H
hdx 已提交
87 88 89 90 91
          },
        })
      },
      getSystemInfoSync: function () {
        this.items = [] as Item[];
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        const res = uni.getSystemInfoSync()
        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);
          }
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
109
        });
H
hdx 已提交
110 111 112 113
      },
      //自动化测试例专用
      jest_getSystemInfo() : GetSystemInfoResult {
        return uni.getSystemInfoSync();
114 115 116
      },
      jest_getScreenHeight_at_different_stages() {
        this.jest_result = (globalScreenHeight == this.screenHeightAtReady)
117
      }
H
hdx 已提交
118 119
    }
  }
120 121 122
</script>

<style>
H
hdx 已提交
123 124 125
  .uni-pd {
    padding-left: 15px;
  }
126
</style>