get-system-info.uvue 3.1 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  type Item = {
    label : string,
    value : string,
  }
  export default {
    data() {
      return {
        title: 'getSystemInfo',
        items: [] as Item[],
      }
    },
    onUnload: function () {
    },
    methods: {
      getSystemInfo: function () {
        uni.getSystemInfo({
          success: (res) => {
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
            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 已提交
72 73 74 75 76
          },
        })
      },
      getSystemInfoSync: function () {
        this.items = [] as Item[];
DCloud_iOS_WZT's avatar
DCloud_iOS_WZT 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
        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 已提交
95 96 97 98 99 100 101
      },
      //自动化测试例专用
      jest_getSystemInfo() : GetSystemInfoResult {
        return uni.getSystemInfoSync();
      },
    }
  }
102 103 104
</script>

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