zhangwencui
7 天以前 7619c19a67c2ac824f803090bab753fc5ea14408
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
<script lang="ts" setup>
import type { CrmCustomerApi } from '#/api/crm/customer';
 
import { computed, ref, watch } from 'vue';
 
import { Select } from 'ant-design-vue';
 
import { getCustomerSimpleList } from '#/api/crm/customer';
 
interface Props {
  value?: number;
  disabled?: boolean;
}
 
const props = withDefaults(defineProps<Props>(), {
  value: undefined,
  disabled: false,
});
 
const emit = defineEmits(['update:value', 'change']);
 
const customerList = ref<CrmCustomerApi.Customer[]>([]);
const loading = ref(false);
 
const options = computed(() => {
  return customerList.value.map((item) => ({
    label: item.name,
    value: item.id,
  }));
});
 
const selectedValue = ref(props.value);
 
watch(
  () => props.value,
  (val) => {
    selectedValue.value = val;
  },
);
 
watch(selectedValue, (val) => {
  emit('update:value', val);
  const customer = customerList.value.find((item) => item.id === val);
  emit('change', customer);
});
 
async function loadCustomers() {
  if (customerList.value.length > 0) return;
  loading.value = true;
  try {
    customerList.value = await getCustomerSimpleList();
  } finally {
    loading.value = false;
  }
}
 
function filterOption(input: string, option: any) {
  return option.label.toLowerCase().includes(input.toLowerCase());
}
 
loadCustomers();
</script>
 
<template>
  <Select
    v-model:value="selectedValue"
    :options="options"
    :loading="loading"
    :disabled="disabled"
    placeholder="请选择客户"
    show-search
    :filter-option="filterOption"
    allow-clear
  />
</template>