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
<script lang="ts" setup>
import type { IotProductApi } from '#/api/iot/product/product';
 
import { onMounted, ref } from 'vue';
 
import { Select } from 'ant-design-vue';
 
import { getSimpleProductList } from '#/api/iot/product/product';
 
/** 产品下拉选择器组件 */
defineOptions({ name: 'ProductSelect' });
 
const props = defineProps<{
  deviceType?: number; // 设备类型过滤
  modelValue?: number;
}>();
 
const emit = defineEmits<{
  (e: 'update:modelValue', value?: number): void;
  (e: 'change', value?: number): void;
}>();
 
const loading = ref(false);
const productList = ref<IotProductApi.Product[]>([]);
 
/** 处理选择变化 */
function handleChange(value: any) {
  emit('update:modelValue', value as number | undefined);
  emit('change', value as number | undefined);
}
 
/** 获取产品列表 */
async function getProductList() {
  try {
    loading.value = true;
    productList.value = (await getSimpleProductList(props.deviceType)) || [];
  } finally {
    loading.value = false;
  }
}
 
onMounted(() => {
  getProductList();
});
</script>
 
<template>
  <Select
    :value="modelValue"
    :options="
      productList.map((product) => ({
        label: product.name,
        value: product.id,
      }))
    "
    :loading="loading"
    placeholder="请选择产品"
    allow-clear
    class="w-full"
    option-filter-prop="label"
    show-search
    @change="handleChange"
  />
</template>