SelectShippingTemplate.vue 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <div class="shipping-template-component">
  3. <!-- 选择模板 -->
  4. <el-select
  5. v-model="selectedTemplateId"
  6. placeholder="请选择运费模板"
  7. style="width: 100%"
  8. >
  9. <el-option
  10. v-for="template in templateList"
  11. :key="template.id"
  12. :label="template.name"
  13. :value="template.id"
  14. />
  15. </el-select>
  16. </div>
  17. </template>
  18. <script setup lang="ts">
  19. import { ref, onMounted, watch } from 'vue'
  20. import { ElMessage } from 'element-plus'
  21. import { getShippingTemplateList } from '../api/index.js'
  22. // 定义组件props
  23. const props = defineProps({
  24. // 业务类型
  25. bizType: {
  26. type: String,
  27. default: ''
  28. },
  29. // 业务ID
  30. bizId: {
  31. type: [String, Number],
  32. default: ''
  33. },
  34. // 当前模板ID(用于v-model绑定)
  35. modelValue: {
  36. type: [String, Number],
  37. default: ''
  38. }
  39. })
  40. // 定义组件事件
  41. const emit = defineEmits(['update:modelValue', 'select', 'cancel'])
  42. // 数据相关
  43. const templateList = ref([])
  44. const selectedTemplateId = ref(null)
  45. // 监听modelValue变化,更新选中值
  46. watch(() => props.modelValue, (newVal) => {
  47. selectedTemplateId.value = newVal
  48. })
  49. // 组件挂载时获取模板列表
  50. onMounted(() => {
  51. // 初始化选中值
  52. selectedTemplateId.value = props.modelValue
  53. fetchTemplateList()
  54. })
  55. // 监听bizType和bizId的变化,重新获取模板列表
  56. watch([() => props.bizType, () => props.bizId], () => {
  57. fetchTemplateList()
  58. })
  59. // 监听selectedTemplateId变化,更新外部v-model绑定的值
  60. watch(selectedTemplateId, (newVal) => {
  61. emit('update:modelValue', newVal)
  62. })
  63. // 获取模板列表
  64. const fetchTemplateList = async () => {
  65. try {
  66. const response = await getShippingTemplateList(props.bizType, props.bizId)
  67. templateList.value = response.data || []
  68. // 如果没有传递modelValue,则使用列表中selected为true的项作为当前模板
  69. if (!props.modelValue && templateList.value.length > 0) {
  70. const selectedTemplate = templateList.value.find(template => template.selected)
  71. if (selectedTemplate) {
  72. selectedTemplateId.value = selectedTemplate.id
  73. }
  74. }
  75. } catch (error) {
  76. console.error('获取运费模板列表失败:', error)
  77. ElMessage.error('获取运费模板列表失败')
  78. templateList.value = []
  79. }
  80. }
  81. </script>
  82. <style scoped lang="scss">
  83. .shipping-template-component {
  84. width: 100%;
  85. }
  86. </style>