get-device-info.uvue 2.3 KB
Newer Older
1
<template>
DCloud-WZF's avatar
DCloud-WZF 已提交
2
  <!-- #ifdef APP -->
H
hdx 已提交
3
  <scroll-view class="page-scroll-view">
DCloud-WZF's avatar
DCloud-WZF 已提交
4
  <!-- #endif -->
DCloud-WZF's avatar
DCloud-WZF 已提交
5 6 7 8 9
    <view>
      <page-head :title="title"></page-head>
      <view class="uni-common-mt">
        <view class="uni-list">
          <view class="uni-list">
DCloud-WZF's avatar
DCloud-WZF 已提交
10
            <view class="uni-list-cell" v-for="(item, _) in items" style="align-items: center">
DCloud-WZF's avatar
DCloud-WZF 已提交
11 12 13 14 15 16
              <view class="uni-pd">
                <view class="uni-label" style="width: 180px">{{
                  item.label
                }}</view>
              </view>
              <view class="uni-list-cell-db">
雪洛's avatar
雪洛 已提交
17
                <text style="width: 100%;">{{ item.value == '' ? '未获取' : item.value }}</text>
DCloud-WZF's avatar
DCloud-WZF 已提交
18 19 20 21 22 23 24 25 26 27 28
              </view>
            </view>
          </view>
        </view>
        <view class="uni-padding-wrap">
          <view class="uni-btn-v">
            <button type="primary" @tap="getDeviceInfo">获取设备信息</button>
          </view>
        </view>
      </view>
    </view>
DCloud-WZF's avatar
DCloud-WZF 已提交
29
  <!-- #ifdef APP -->
DCloud-WZF's avatar
DCloud-WZF 已提交
30 31
  </scroll-view>
  <!-- #endif -->
32
</template>
DCloud-WZF's avatar
DCloud-WZF 已提交
33
<script>
DCloud-WZF's avatar
DCloud-WZF 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
  import { setDevicePixelRatio } from '@/store/index.uts'

  type Item = {
    label : string,
    value : string,
  }
  export default {
    data() {
      return {
        title: 'getDeviceInfo',
        items: [] as Item[],
      }
    },
    onUnload: function () {
    },
    methods: {
      getDeviceInfo: function () {
        const res = uni.getDeviceInfo();
        // 获取像素比, 供截图对比使用
        setDevicePixelRatio(res.devicePixelRatio !== null ? parseFloat(res.devicePixelRatio!) : 1)
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 72
        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);
          }
        });
73
      }
DCloud-WZF's avatar
DCloud-WZF 已提交
74 75
    }
  }
76 77 78
</script>

<style>
DCloud-WZF's avatar
DCloud-WZF 已提交
79
  .uni-pd {
H
hdx 已提交
80
    padding-left: 15px;
DCloud-WZF's avatar
DCloud-WZF 已提交
81 82
  }
</style>