index.vue 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <el-form-item :label="label" :prop="prop"
  3. :rules="required ? [{ required: true, message: label + '不能为空', trigger: 'change' }] : undefined">
  4. <div style="width: 100%;display:flex;gap:8px">
  5. <el-select v-model="currentScope" placeholder="范围" style="width:110px;flex-shrink:0" size="default"
  6. v-if="showScope">
  7. <el-option label="全部" value="all" />
  8. <el-option label="水利机构" value="water" />
  9. <el-option label="非水利机构" value="uwat" />
  10. </el-select>
  11. <el-select :model-value="modelValue" filterable remote clearable reserve-keyword :remote-method="remoteSearch"
  12. :loading="loading" :disabled="disabled" :placeholder="placeholder" style="width: 100%"
  13. @update:model-value="handleChange">
  14. <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
  15. </el-select>
  16. </div>
  17. </el-form-item>
  18. </template>
  19. <script setup>
  20. import { ref, watch } from 'vue'
  21. import { listAttOrgBase, getAttOrgBase } from '@/api/slaj/hidd'
  22. import { listAttOrgUwatBase, getAttOrgUwatBase } from '@/api/system/attOrgUwatBase'
  23. const props = defineProps({
  24. modelValue: { type: String, default: undefined },
  25. label: { type: String, default: '组织机构' },
  26. prop: { type: String, default: undefined },
  27. placeholder: { type: String, default: '请输入机构名称搜索' },
  28. required: { type: Boolean, default: false },
  29. disabled: { type: Boolean, default: false },
  30. excludeGuid: { type: String, default: undefined },
  31. // 范围筛选:all=全部, water=水利机构, uwat=非水利机构
  32. scope: { type: String, default: 'all' },
  33. // 是否显示范围选择器
  34. showScope: { type: Boolean, default: false }
  35. })
  36. const emit = defineEmits(['update:modelValue'])
  37. const options = ref([])
  38. const loading = ref(false)
  39. const currentScope = ref(props.scope)
  40. // scope 变化时清空已选值
  41. watch(currentScope, () => {
  42. emit('update:modelValue', undefined)
  43. options.value = []
  44. })
  45. function normalize(rows) {
  46. return (rows || [])
  47. .filter(r => !props.excludeGuid || r.guid !== props.excludeGuid)
  48. .map(r => ({ value: r.guid, label: r.orgName }))
  49. }
  50. function remoteSearch(query) {
  51. if (!query) { options.value = []; return }
  52. loading.value = true
  53. let req
  54. if (currentScope.value === 'uwat') {
  55. req = listAttOrgUwatBase({ orgName: query, pageNum: 1, pageSize: 20 })
  56. .then(res => { options.value = (res.rows || []).filter(r => !props.excludeGuid || r.guid !== props.excludeGuid).map(r => ({ value: r.guid, label: r.orgName })) })
  57. } else {
  58. const params = { orgName: query, pageNum: 1, pageSize: 20 }
  59. if (currentScope.value === 'water') params.orgType = '1'
  60. req = listAttOrgBase(params).then(res => { options.value = normalize(res.rows) })
  61. }
  62. req.finally(() => { loading.value = false })
  63. }
  64. function handleChange(val) { emit('update:modelValue', val) }
  65. // 编辑回显
  66. watch(() => props.modelValue, (val) => {
  67. if (val && !options.value.some(o => o.value === val)) {
  68. const fetch = currentScope.value === 'uwat' ? getAttOrgUwatBase(val) : getAttOrgBase(val)
  69. fetch.then(res => {
  70. const d = res.data
  71. if (d && d.guid && (!props.excludeGuid || d.guid !== props.excludeGuid)) {
  72. options.value = [{ value: d.guid, label: d.orgName }, ...options.value]
  73. }
  74. }).catch(() => { })
  75. }
  76. }, { immediate: true })
  77. </script>