wlzboy
2025-12-26 c10b1e130ccbc94e6481a43e8e2d35cfc8fcf83b
app/pagesTask/components/DepartmentSelector.vue
@@ -1,26 +1,24 @@
<template>
  <view class="form-item">
    <view class="form-label" :class="{ required: required }">{{ label }}</view>
    <!-- 调试信息 -->
    <view v-if="false" style="font-size: 24rpx; color: #999; margin-bottom: 10rpx;">
      isHome: {{ isHome }}, options.length: {{ departmentOptions.length }}
    </view>
    <picker 
      v-if="!isHome && departmentOptions.length > 0"
      v-if="!isHome"
      mode="selector" 
      :range="departmentOptions" 
      range-key="text" 
      :value="selectedIndex"
      @change="onDepartmentChange"
      :disabled="departmentOptions.length === 0"
    >
      <view class="form-input picker-input">
      <view class="form-input picker-input" :class="{ disabled: departmentOptions.length === 0 }">
        {{ displayText }}
        <uni-icons type="arrowright" size="16" color="#999"></uni-icons>
      </view>
    </picker>
    <input
      v-else-if="!isHome"
      class="form-input"
      placeholder="请输入科室"
      :value="value"
      @input="onDepartmentInput"
    />
    <view v-else class="form-input picker-input disabled">
      其它
    </view>
@@ -72,6 +70,9 @@
  },
  computed: {
    displayText() {
      if (this.departmentOptions.length === 0) {
        return '加载中...'
      }
      if (this.selectedIndex >= 0 && this.selectedIndex < this.departmentOptions.length) {
        return this.departmentOptions[this.selectedIndex].text
      }
@@ -82,6 +83,7 @@
    value: {
      immediate: true,
      handler(newVal) {
        console.log('DepartmentSelector: value changed:', newVal)
        if (newVal) {
          this.updateSelectedIndex(newVal)
        } else {
@@ -92,6 +94,7 @@
    departmentId: {
      immediate: true,
      handler(newVal) {
        console.log('DepartmentSelector: departmentId changed:', newVal)
        if (newVal) {
          this.updateSelectedIndexById(newVal)
        }
@@ -100,29 +103,55 @@
    departmentOptions: {
      immediate: true,
      handler() {
        if (this.value) {
          this.updateSelectedIndex(this.value)
        } else if (this.departmentId) {
        console.log('DepartmentSelector: departmentOptions changed, length=', this.departmentOptions.length)
        console.log('DepartmentSelector: 当前显示条件 - isHome:', this.isHome, 'departmentOptions.length:', this.departmentOptions.length, '显示picker:', !this.isHome && this.departmentOptions.length > 0)
        // 优先使用科室ID进行匹配,如果没有科室ID再使用科室名称
        if (this.departmentId) {
          this.updateSelectedIndexById(this.departmentId)
        } else if (this.value) {
          this.updateSelectedIndex(this.value)
        }
      }
    },
    isHome: {
      immediate: true,
      handler(newVal) {
        console.log('DepartmentSelector: isHome changed:', newVal)
        // 当变为"家中"时,自动选择"其它"科室
        if (newVal && this.departmentOptions.length > 0) {
          this.autoSelectOtherDepartment()
        }
      }
    }
  },
  mounted() {
    console.log('DepartmentSelector: 组件已挂载, isHome=', this.isHome, 'value=', this.value, 'departmentId=', this.departmentId)
    this.loadDepartments()
  },
  methods: {
    // 加载科室数据
    loadDepartments() {
      console.log('DepartmentSelector: 开始加载科室数据')
      getHospitalDepartments().then(response => {
        console.log('DepartmentSelector: 科室接口响应:', response)
        const list = response.data || []
        console.log('DepartmentSelector: 科室数据条数:', list.length)
        if (list.length > 0) {
          console.log('DepartmentSelector: 科室数据示例:', list.slice(0, 3))
        }
        this.departmentOptions = list.map(item => ({
          id: item.vID,
          text: item.vtext,
          dictValue: item.vtext
        }))
        console.log('DepartmentSelector: 处理后的科室选项:', this.departmentOptions.length)
        // 如果当前是"家中",自动选择"其它"
        if (this.isHome) {
          this.autoSelectOtherDepartment()
        }
      }).catch(error => {
        console.error('加载科室数据失败:', error)
        console.error('DepartmentSelector: 加载科室数据失败:', error)
        this.departmentOptions = []
      })
    },
@@ -184,6 +213,27 @@
        department: department,
        departmentId: null
      })
    },
    // 自动选择"其它"科室(当isHome为true时)
    autoSelectOtherDepartment() {
      // 查找"其它"科室
      const otherDept = this.departmentOptions.find(d => d.text === '其它')
      if (otherDept) {
        console.log('DepartmentSelector: 自动选择"其它"科室, ID:', otherDept.id)
        // 更新选中索引
        const index = this.departmentOptions.findIndex(d => d.id === otherDept.id)
        this.selectedIndex = index
        // 触发change事件,传递departmentId
        this.$emit('input', otherDept.text)
        this.$emit('change', {
          department: otherDept.text,
          departmentId: otherDept.id
        })
      } else {
        console.warn('DepartmentSelector: 未找到"其它"科室')
      }
    }
  }
}
@@ -226,5 +276,4 @@
    }
  }
}
</style>
</style>