get-device-info.uvue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <!-- #ifdef APP -->
  3. <scroll-view class="page-scroll-view">
  4. <!-- #endif -->
  5. <view>
  6. <page-head :title="title"></page-head>
  7. <view class="uni-common-mt">
  8. <view class="uni-list">
  9. <view class="uni-list">
  10. <view class="uni-list-cell" v-for="(item, _) in items" style="align-items: center">
  11. <view class="uni-pd">
  12. <view class="uni-label" style="width: 180px">{{
  13. item.label
  14. }}</view>
  15. </view>
  16. <view class="uni-list-cell-db">
  17. <text class="uni-list-cell-db-text">{{
  18. item.value == "" ? "未获取" : item.value
  19. }}</text>
  20. </view>
  21. </view>
  22. </view>
  23. </view>
  24. <view class="uni-padding-wrap">
  25. <view class="uni-btn-v">
  26. <button type="primary" @tap="getDeviceInfo">获取设备信息</button>
  27. </view>
  28. </view>
  29. </view>
  30. </view>
  31. <!-- #ifdef APP -->
  32. </scroll-view>
  33. <!-- #endif -->
  34. </template>
  35. <script>
  36. import { setDevicePixelRatio } from '@/store/index.uts'
  37. type Item = {
  38. label : string,
  39. value : string,
  40. }
  41. export default {
  42. data() {
  43. return {
  44. title: 'getDeviceInfo',
  45. items: [] as Item[],
  46. }
  47. },
  48. onUnload: function () {
  49. },
  50. methods: {
  51. getDeviceInfo: function () {
  52. const res = uni.getDeviceInfo();
  53. // 获取像素比, 供截图对比使用
  54. setDevicePixelRatio(res.devicePixelRatio !== null ? res.devicePixelRatio! : 1)
  55. this.items = [] as Item[];
  56. const res_str = JSON.stringify(res);
  57. const res_obj = JSON.parseObject(res_str);
  58. const res_map = res_obj!.toMap();
  59. let keys = [] as string[]
  60. res_map.forEach((_, key) => {
  61. keys.push(key);
  62. });
  63. keys.sort().forEach(key => {
  64. const value = res[key];
  65. if (value != null) {
  66. const item = {
  67. label: key,
  68. value: "" + ((typeof value == "object") ? JSON.stringify(value) : value)
  69. } as Item;
  70. this.items.push(item);
  71. }
  72. });
  73. }
  74. }
  75. }
  76. </script>
  77. <style>
  78. .uni-pd {
  79. padding-left: 15px;
  80. }
  81. </style>