index.vue 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <template>
  2. <t-select v-model="selected" filterable v-bind="attrs" @change="onChange">
  3. <t-option
  4. v-for="item in optionList.value"
  5. :key="item.id"
  6. :value="item.id"
  7. :label="item.level"
  8. />
  9. </t-select>
  10. </template>
  11. <script setup name="SelectServiceLevel">
  12. import { onMounted, reactive, ref, useAttrs, watch } from 'vue';
  13. import { serviceLevelListApi } from '@/api/system';
  14. let optionList = reactive([]);
  15. let selected = ref('');
  16. const attrs = useAttrs();
  17. const emit = defineEmits(['update:modelValue', 'change']);
  18. const props = defineProps({
  19. modelValue: { type: [Number, String], default: '' },
  20. type: { type: String, default: '' },
  21. typeRequired: { type: Boolean, default: false },
  22. });
  23. const search = async () => {
  24. if (props.typeRequired && !props.type) return;
  25. optionList.value = [];
  26. let data = { enable: true };
  27. if (props.type) data.type = props.type;
  28. const res = await serviceLevelListApi(data).catch(() => {});
  29. if (!res) return;
  30. optionList.value = res;
  31. };
  32. const onChange = () => {
  33. emit('update:modelValue', selected.value);
  34. emit('change', selected.value);
  35. };
  36. onMounted(() => {
  37. search();
  38. });
  39. watch(
  40. () => props.modelValue,
  41. (val) => {
  42. selected.value = val;
  43. }
  44. );
  45. watch(
  46. () => props.type,
  47. (val, oldval) => {
  48. if (val !== oldval) search();
  49. }
  50. );
  51. </script>