get-system-info.uvue 3.6 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 style="width: 100%;">{{ 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
  }
  const globalScreenHeight = uni.getSystemInfoSync().screenHeight
H
hdx 已提交
42 43 44 45
  export default {
    data() {
      return {
        title: 'getSystemInfo',
46 47 48
        items: [] as Item[],
        screenHeightAtReady: 0,
        jest_result: false,
H
hdx 已提交
49 50 51
      }
    },
    onUnload: function () {
52 53 54 55 56
    },

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

<style>
H
hdx 已提交
116 117 118 119
  .uni-pd {
    padding-left: 15px;
  }
</style>