gaoluyang
2026-06-24 712aa51536236d43e87273e4ce45ac5691dffad8
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
107
108
109
110
111
112
113
114
115
116
117
<!-- 设备选择器组件 -->
<script setup lang="ts">
import { ref, watch } from 'vue';
 
import { DEVICE_SELECTOR_OPTIONS, DICT_TYPE } from '..\..\..\..\..\..\packages\constants\src';
 
import { Select } from 'ant-design-vue';
 
import { getDeviceListByProductId } from '#/api/iot/device/device';
import { DictTag } from '#/components/dict-tag';
 
/** 设备选择器组件 */
defineOptions({ name: 'DeviceSelector' });
 
const props = defineProps<{
  modelValue?: number;
  productId?: number;
}>();
 
const emit = defineEmits<{
  (e: 'update:modelValue', value?: number): void;
  (e: 'change', value?: number): void;
}>();
 
const deviceLoading = ref(false); // 设备加载状态
const deviceList = ref<any[]>([]); // 设备列表
 
/** 处理选择变化事件 */
function handleChange(value: any) {
  emit('update:modelValue', value as number | undefined);
  emit('change', value as number | undefined);
}
 
/**
 * 获取设备列表
 */
async function getDeviceList() {
  if (!props.productId) {
    deviceList.value = [];
    return;
  }
 
  try {
    deviceLoading.value = true;
    const data = await getDeviceListByProductId(props.productId);
    deviceList.value = [DEVICE_SELECTOR_OPTIONS.ALL_DEVICES, ...(data || [])];
  } catch (error) {
    console.error('获取设备列表失败 :', error);
    deviceList.value = [DEVICE_SELECTOR_OPTIONS.ALL_DEVICES];
  } finally {
    deviceLoading.value = false;
  }
}
 
// 监听产品变化
watch(
  () => props.productId,
  async (newProductId, oldProductId) => {
    if (!newProductId) {
      deviceList.value = [];
      if (props.modelValue !== undefined && props.modelValue !== null) {
        emit('update:modelValue', undefined);
        emit('change', undefined);
      }
      return;
    }
    await getDeviceList();
    // 切换到新 productId 时,旧 deviceId 不在新列表里则清空
    if (
      oldProductId !== undefined &&
      oldProductId !== newProductId &&
      props.modelValue !== undefined &&
      props.modelValue !== null &&
      !deviceList.value.some((d: any) => d.id === props.modelValue)
    ) {
      emit('update:modelValue', undefined);
      emit('change', undefined);
    }
  },
  { immediate: true },
);
</script>
 
<template>
  <Select
    :value="modelValue"
    @change="handleChange"
    placeholder="请选择设备"
    show-search
    allow-clear
    class="w-full"
    option-label-prop="label"
    :loading="deviceLoading"
    :disabled="!productId"
  >
    <Select.Option
      v-for="device in deviceList"
      :key="device.id"
      :label="device.deviceName"
      :value="device.id"
    >
      <div class="py-[4px] flex w-full items-center justify-between">
        <div class="flex-1">
          <div class="text-[14px] font-medium mb-[2px] text-foreground">
            {{ device.deviceName }}
          </div>
          <div class="text-[12px] text-muted-foreground">
            {{ device.deviceKey }}
          </div>
        </div>
        <div class="gap-[4px] flex items-center" v-if="device.id > 0">
          <DictTag :type="DICT_TYPE.IOT_DEVICE_STATE" :value="device.state" />
        </div>
      </div>
    </Select.Option>
  </Select>
</template>