riku
2025-02-28 3d6addd2c0817b30bd328605cb048ca9698742a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<template>
  <FYImageSelectDialog
    v-loading="loading"
    title="设备图片"
    :typeList="typeList"
    :typeImgMap="typeImgMap"
  ></FYImageSelectDialog>
</template>
<script setup>
import { ref, computed, onMounted } from 'vue';
import deviceApi from '@/api/fysp/deviceApi';
import { useCloned } from '@vueuse/core';
import { $fysp } from '@/api/index.js';
const loading = ref(true)
const props = defineProps({
  // 展示模式
  mode: {
    type: Number,
    default: 0
  },
  subtask: {
    type: Array,
    default: () => []
  }
});
const typeList = computed(() => {
  if (props.mode == 0) {
    return [
      { typeId: 0, typeName: '监控设备' },
      { typeId: 1, typeName: '治理设备' },
      { typeId: 2, typeName: '生产设备' }
    ];
  } else if (props.mode == 1) {
    return [{ typeId: 1, typeName: '整改' }];
  } else {
    return [{ typeId: 1, typeName: '未指定' }];
  }
});
const typeImgMap = ref(new Map());
// 标准化属性名
function convertKeys(obj) {
  // 将一个js对象中所有di,wi,pi开头的属性全部改成去掉这些前缀并且重新变为驼峰式命名
  const newObj = {};
  for (const key in obj) {
    let newKey = key;
    if (key.startsWith('di')) {
      newKey = key.substring(2);
    } else if (key.startsWith('wi')) {
      newKey = key.substring(2);
    } else if (key.startsWith('pi')) {
      newKey = key.substring(2);
    }
    newKey = newKey.charAt(0).toLowerCase() + newKey.slice(1);
    newObj[newKey] = obj[key];
  }
  return newObj;
}
// 保存状态信息
function saveStatus(device, status) {
  var _picUrl = $fysp.imgUrl + status.dlPicUrl;
  device.url = _picUrl;
}
function getDeviceImgList() {
  let deviceImgMap = typeImgMap.value;
  for (const deviceTopTypeElement of typeList.value) {
    const topTypeId = deviceTopTypeElement.typeId;
    deviceImgMap.set(topTypeId, []);
    deviceApi.fetchDevices(props.subtask.sceneId, topTypeId).then((result) => {
      loading.value = true;
      // 标准化属性名
      for (let i = 0; i < result.data.length; i++) {
        var element = convertKeys(result.data[i]);
        // 获取设备状态信息
        let data = {
          deviceId: element.id,
          sceneId: element.sceneGuid,
          deviceTypeId: topTypeId
        };
        deviceApi.fetchDeviceStatus(data).then((status) => {
          var statusData = status.data;
          if (statusData) {
            if (statusData.length == 0) {
              return;
            }
            element = convertKeys(result.data[i]);
            for (let j = 0; j < statusData.length; j++) {
              // 复制出一个设备对象
              var newDevice = useCloned(element).cloned.value;
              const statusItem = statusData[j];
              // 设备对象添加一个属性列表属性用来保存设备状态
              saveStatus(newDevice, statusItem);
              newDevice.dlLocation = statusItem.dlLocation;
              newDevice.topTypeId = topTypeId;
 
              deviceImgMap.get(topTypeId).push(newDevice);
            }
          }
        }).finally(() => (loading.value = false));
      }
    });
  }
}
onMounted(() => {
  getDeviceImgList();
});
</script>