LLMSelector.vue 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <script setup lang="ts">
  2. import { fetchModel } from '@/api/modules/model'
  3. // 定义组件属性
  4. interface Props {
  5. modelValue?: string
  6. disabled?: boolean
  7. placeholder?: string
  8. style?: string | object
  9. className?: string
  10. }
  11. // 定义组件事件
  12. interface Emits {
  13. (e: 'update:modelValue', value: string | undefined): void
  14. }
  15. // 设置默认属性
  16. const props = withDefaults(defineProps<Props>(), {
  17. disabled: false,
  18. placeholder: '请选择大模型',
  19. style: 'width: 220px;'
  20. })
  21. // 定义事件触发器
  22. const emit = defineEmits<Emits>()
  23. // 创建本地响应式变量用于v-model绑定
  24. const localValue = ref(props.modelValue)
  25. // 监听props中的modelValue变化,同步到本地变量
  26. watch(
  27. () => props.modelValue,
  28. (newValue) => {
  29. localValue.value = newValue
  30. },
  31. { immediate: true }
  32. )
  33. // 音色选项数据
  34. const options = ref<Array<{value: string, name: string}>>([])
  35. // 获取音色数据
  36. const fetchData = async () => {
  37. try {
  38. const params = {
  39. page: 1,
  40. size: 100,
  41. }
  42. const res = await fetchModel()
  43. if (res.code === 0) {
  44. // 将API返回的数据转换为select需要的格式
  45. options.value = res.data.content.map((item: any) => ({
  46. value: item.id,
  47. name: item.name
  48. }))
  49. } else {
  50. console.error('获取模型失败:', res.msg)
  51. }
  52. } catch (error) {
  53. console.error('获取模型失败:', error)
  54. }
  55. }
  56. // 组件挂载时获取数据
  57. onMounted(() => {
  58. fetchData()
  59. })
  60. </script>
  61. <template>
  62. <el-select
  63. v-model="localValue"
  64. :placeholder="placeholder"
  65. :style="style"
  66. :disabled="disabled"
  67. :class="className"
  68. @update:model-value="(value) => emit('update:modelValue', value)"
  69. >
  70. <el-option v-for="item in options" :key="item.value" :label="item.name" :value="item.value" />
  71. </el-select>
  72. </template>