wlzboy
2025-11-05 2576c71ce6a7d0465fcbc5ebc7507da9472c41a3
app/pages/task/create-emergency.vue
@@ -18,7 +18,7 @@
        </picker>
      </view>
        <view class="form-item">
        <view class="form-label">归属机构</view>
        <view class="form-label required">归属机构</view>
        <picker mode="selector" :range="organizations" @change="onOrganizationChange">
          <view class="form-input picker-input">
            {{ selectedOrganization || '请选择归属机构' }}
@@ -78,7 +78,7 @@
    
      
      <view class="form-item">
        <view class="form-label">转运时间</view>
        <view class="form-label required">转运时间</view>
        <uni-datetime-picker 
          v-model="taskForm.transferTime" 
          type="datetime" 
@@ -274,7 +274,7 @@
      </view>
      
      <view class="form-item">
        <view class="form-label">转运公里数</view>
        <view class="form-label required">转运公里数</view>
        <input 
          class="form-input" 
          type="digit" 
@@ -284,7 +284,7 @@
      </view>
      
      <view class="form-item">
        <view class="form-label">成交价</view>
        <view class="form-label required">成交价</view>
        <input 
          class="form-input" 
          type="digit" 
@@ -443,14 +443,14 @@
import uniPopup from '@/uni_modules/uni-popup/components/uni-popup/uni-popup.vue'
import { addTask } from "@/api/task"
import { listAvailableVehicles, getUserBoundVehicle } from "@/api/vehicle"
import { calculateDistance } from "@/api/map"
import { searchHospitals } from "@/api/hospital"
import { calculateDistance, baiduDistanceByAddress } from "@/api/map"
import { searchHospitals, getFrequentOutHospitals, getFrequentInHospitals } from "@/api/hospital"
import { listUser } from "@/api/system/user"
import { searchIcd10 } from "@/api/icd10"
import { getDicts } from "@/api/dict"
import { getServiceOrdAreaTypes, getServiceOrderTypes, getHospitalDepartments } from "@/api/dictionary"
import { listBranchCompany } from "@/api/system/dept"
import { listBranchCompany, getDept } from "@/api/system/dept"
import MapSelector from '@/components/map-selector.vue'
export default {
@@ -465,6 +465,7 @@
      selectedVehicleId: null,
      selectedOrganization: '',
      selectedOrganizationId: null, // 归属机构ID(部门ID)
      selectedOrganizationServiceOrderClass: '', // 归属机构的服务单编码
      selectedRegion: '', // 从归属机构中提取的地域信息(如:广州、深圳等)
      selectedEmergencyTaskType: '', // 选中的任务类型文本
      selectedEmergencyTaskTypeId: null, // 选中的任务类型ID
@@ -560,6 +561,9 @@
    }
  },
  onLoad(options) {
    // 设置默认转运时间为当前时间
    this.setDefaultTransferTime()
    // 先加载车辆列表,然后加载绑定车辆信息
    this.getAvailableVehicles().then(() => {
      this.getUserBoundVehicleInfo()
@@ -615,17 +619,21 @@
    },
    
    getAvailableVehicles() {
      const deptId = this.currentUser.deptId
      return listAvailableVehicles(deptId, 'EMERGENCY').then(response => {
        const vehicleList = response.data || response.rows || []
      // 根据用户有权限管理的分公司,查询所有可用车辆
      return listAvailableVehicles(null, 'EMERGENCY').then(response => {
        const vehicleList = response.data || []
        this.vehicleOptions = vehicleList.map(vehicle => ({
          id: vehicle.vehicleId,
          name: vehicle.vehicleNo,
          type: vehicle.vehicleType,
          status: vehicle.status
          status: vehicle.status,
          deptNames: vehicle.deptNames || [] // 车辆归属的多个分公司
        }))
        // 只显示车牌号,不显示分公司
        this.vehicles = this.vehicleOptions.map(v => v.name)
      }).catch(() => {
        console.log('加载可用车辆数量:', this.vehicles.length)
      }).catch(error => {
        console.error('加载车辆列表失败:', error)
        this.vehicles = []
      })
    },
@@ -641,8 +649,9 @@
      const selected = this.organizationOptions[index]
      this.selectedOrganization = selected.deptName
      this.selectedOrganizationId = selected.deptId // 保存部门ID
      // 从归属机构中提取地域关键词(去除"分公司"后缀)
      // 例如:"广州分公司" -> "广州"
      this.selectedOrganizationServiceOrderClass = selected.serviceOrderClass || '' // 保存服务单编码
      // 从归属机构中提取地域关键词(去除“分公司”后缀)
      // 例如:“广州分公司” -> “广州”
      this.selectedRegion = selected.deptName.replace(/分公司$/g, '').trim()
      // 重新加载医院列表(带地域过滤)
      this.loadDefaultHospitals()
@@ -665,9 +674,10 @@
          if (index !== -1) {
            this.selectedOrganization = this.currentUser.branchCompanyName
            this.selectedOrganizationId = this.organizationOptions[index].deptId // 保存部门ID
            this.selectedOrganizationServiceOrderClass = this.organizationOptions[index].serviceOrderClass || '' // 保存服务单编码
            // 提取地域关键词
            this.selectedRegion = this.selectedOrganization.replace(/分公司$/g, '').trim()
            console.log('默认选中归属机构:', this.selectedOrganization, '部门ID:', this.selectedOrganizationId, '地域:', this.selectedRegion)
            console.log('默认选中归属机构:', this.selectedOrganization, '部门ID:', this.selectedOrganizationId, '服务单编码:', this.selectedOrganizationServiceOrderClass, '地域:', this.selectedRegion)
            // 加载医院列表(带地域过滤)
            this.loadDefaultHospitals()
          }
@@ -785,25 +795,147 @@
      this.taskForm.hospitalIn.departmentId = selected.id  // 保存科室ID
    },
    
    // 加载默认区院列表(前100条)
    // 加载默认医院列表(常用医院)
    loadDefaultHospitals() {
      // 传入空字符串或特殊标识获取前100条,同时传入地域过滤
      searchHospitals('', this.selectedRegion).then(response => {
        this.defaultHospitals = response.data || []
        // 同时初始化两个搜索结果为默认数据
        this.hospitalOutResults = [...this.defaultHospitals]
        this.hospitalInResults = [...this.defaultHospitals]
      // 检查是否有服务单编码
      if (!this.selectedOrganizationServiceOrderClass) {
        console.warn('未找到服务单编码,无法加载常用医院')
        // 如果没有服务单编码,降级为普通搜索(按地域过滤)
        this.loadDefaultHospitalsByRegion()
        return
      }
      // 转出医院:加载当前分公司的常用转出医院
      getFrequentOutHospitals(this.selectedOrganizationServiceOrderClass, this.selectedRegion).then(response => {
        this.hospitalOutResults = response.data || []
        console.log('加载常用转出医院:', this.selectedOrganizationServiceOrderClass, '地域:', this.selectedRegion, '数量:', this.hospitalOutResults.length)
        // 如果没有常用医院,降级为普通搜索
        if (this.hospitalOutResults.length === 0) {
          console.log('未找到常用转出医院,降级为地域搜索')
          searchHospitals('', this.selectedRegion).then(res => {
            this.hospitalOutResults = res.data || []
          })
        }
      }).catch(error => {
        console.error('加载默认区院列表失败:', error)
        this.defaultHospitals = []
        console.error('加载常用转出医院失败:', error)
        // 失败后降级为普通搜索
        searchHospitals('', this.selectedRegion).then(res => {
          this.hospitalOutResults = res.data || []
        })
      })
      // 转入医院:加载当前分公司的常用转入医院(本地区域优先)
      getFrequentInHospitals(this.selectedOrganizationServiceOrderClass, '').then(response => {
        const allHospitals = response.data || []
        // 将医院按地域排序:本地区域优先
        this.hospitalInResults = this.sortHospitalsByRegion(allHospitals)
        console.log('加载常用转入医院:', this.selectedOrganizationServiceOrderClass, '数量:', this.hospitalInResults.length)
        // 如果没有常用医院,降级为普通搜索
        if (this.hospitalInResults.length === 0) {
          console.log('未找到常用转入医院,降级为全部医院')
          searchHospitals('', '').then(res => {
            const allHospitals = res.data || []
            this.hospitalInResults = this.sortHospitalsByRegion(allHospitals)
          })
        }
      }).catch(error => {
        console.error('加载常用转入医院失败:', error)
        // 失败后降级为普通搜索
        searchHospitals('', '').then(res => {
          const allHospitals = res.data || []
          this.hospitalInResults = this.sortHospitalsByRegion(allHospitals)
        })
      })
    },
    // 降级加载医院(按地域过滤)
    loadDefaultHospitalsByRegion() {
      // 转出医院:只加载当前区域的医院(带地域过滤)
      searchHospitals('', this.selectedRegion).then(response => {
        this.hospitalOutResults = response.data || []
        console.log('加载转出医院(当前区域):', this.selectedRegion, '数量:', this.hospitalOutResults.length)
      }).catch(error => {
        console.error('加载转出医院列表失败:', error)
        this.hospitalOutResults = []
      })
      // 转入医院:加载所有医院(不带地域过滤,后续会按地域排序)
      searchHospitals('', '').then(response => {
        const allHospitals = response.data || []
        // 将医院按地域排序:本地区域优先
        this.hospitalInResults = this.sortHospitalsByRegion(allHospitals)
        console.log('加载转入医院(全部区域):', '数量:', this.hospitalInResults.length)
      }).catch(error => {
        console.error('加载转入医院列表失败:', error)
        this.hospitalInResults = []
      })
    },
    // 按地域排序医院:本地区域优先
    sortHospitalsByRegion(hospitals) {
      if (!this.selectedRegion || !hospitals || hospitals.length === 0) {
        return hospitals
      }
      const region = this.selectedRegion
      const localHospitals = []
      const otherHospitals = []
      hospitals.forEach(hospital => {
        // 判断医院是否在本地区域(省、市、区任一包含地域关键词)
        const isLocal =
          (hospital.hopsProvince && hospital.hopsProvince.includes(region)) ||
          (hospital.hopsCity && hospital.hopsCity.includes(region)) ||
          (hospital.hopsArea && hospital.hopsArea.includes(region))
        if (isLocal) {
          localHospitals.push(hospital)
        } else {
          otherHospitals.push(hospital)
        }
      })
      // 本地医院在前,其他医院在后
      return [...localHospitals, ...otherHospitals]
    },
    
    // 转出医院输入框获得焦点
    onHospitalOutFocus() {
      // 如果没有搜索关键词,显示默认的100条数据
      // 如果没有搜索关键词,显示常用转出医院
      if (!this.hospitalOutSearchKeyword || this.hospitalOutSearchKeyword.trim() === '') {
        this.hospitalOutResults = [...this.defaultHospitals]
        // 如果已经加载过常用医院,直接显示
        if (this.hospitalOutResults.length > 0) {
          this.showHospitalOutResults = true
          return
        }
        // 否则重新加载常用医院
        if (this.selectedOrganizationServiceOrderClass) {
          getFrequentOutHospitals(this.selectedOrganizationServiceOrderClass, this.selectedRegion).then(response => {
            this.hospitalOutResults = response.data || []
            // 如果没有常用医院,降级为普通搜索
            if (this.hospitalOutResults.length === 0) {
              searchHospitals('', this.selectedRegion).then(res => {
                this.hospitalOutResults = res.data || []
              })
            }
          }).catch(error => {
            console.error('加载常用转出医院失败:', error)
            searchHospitals('', this.selectedRegion).then(res => {
              this.hospitalOutResults = res.data || []
            })
          })
        } else {
          // 没有服务单编码,使用普通搜索
          searchHospitals('', this.selectedRegion).then(response => {
            this.hospitalOutResults = response.data || []
          }).catch(error => {
            console.error('加载转出医院失败:', error)
            this.hospitalOutResults = []
          })
        }
      }
      this.showHospitalOutResults = true
    },
@@ -818,27 +950,33 @@
        clearTimeout(this.searchTimer)
      }
      
      // 如果关键词为空,显示默认100条
      // 如果关键词为空,只显示当前区域的医院
      if (!keyword || keyword.trim() === '') {
        this.hospitalOutResults = [...this.defaultHospitals]
        searchHospitals('', this.selectedRegion).then(response => {
          this.hospitalOutResults = response.data || []
        }).catch(error => {
          console.error('加载转出医院失败:', error)
          this.hospitalOutResults = []
        })
        this.showHospitalOutResults = true
        return
      }
      
      // 有关键词时,去服务端搜索
      // 有关键词时,去服务端搜索(仅限当前区域)
      this.searchTimer = setTimeout(() => {
        this.searchHospitalOut(keyword)
      }, 300)
    },
    
    // 搜索转出医院
    // 搜索转出医院(仅限当前区域)
    searchHospitalOut(keyword) {
      // 传入关键词和地域过滤
      // 传入关键词和地域过滤,只搜索当前区域的医院
      searchHospitals(keyword, this.selectedRegion).then(response => {
        this.hospitalOutResults = response.data || []
        this.showHospitalOutResults = true
        console.log('搜索转出医院:', keyword, '区域:', this.selectedRegion, '结果数:', this.hospitalOutResults.length)
      }).catch(error => {
        console.error('搜索医院失败:', error)
        console.error('搜索转出医院失败:', error)
        this.hospitalOutResults = []
      })
    },
@@ -854,23 +992,56 @@
      this.showHospitalOutResults = false
      this.hospitalOutResults = []
      
      // 如果有GPS坐标,保存下来
      // 注意:HospData表中可能没有GPS坐标,需要根据地址进行地理编码
      // 这里先置为null,后续可以通过地址解析获取
      this.addressCoordinates.hospitalOutAddress = null
      // 保存转出医院的城市信息
      this.taskForm.hospitalOut.city = hospital.hopsCity || ''
      
      // 如果两个医院都已选择,自动计算距离
      if (this.taskForm.hospitalIn.address) {
        // 这里可以调用地址解析和距离计算
        // 暂时留空,由用户手动输入距离
        this.calculateHospitalDistance()
      }
    },
    
    // 转入医院输入框获得焦点
    onHospitalInFocus() {
      // 如果没有搜索关键词,显示默认的100条数据
      // 如果没有搜索关键词,显示常用转入医院
      if (!this.hospitalInSearchKeyword || this.hospitalInSearchKeyword.trim() === '') {
        this.hospitalInResults = [...this.defaultHospitals]
        // 如果已经加载过常用医院,直接显示
        if (this.hospitalInResults.length > 0) {
          this.showHospitalInResults = true
          return
        }
        // 否则重新加载常用医院
        if (this.selectedOrganizationServiceOrderClass) {
          getFrequentInHospitals(this.selectedOrganizationServiceOrderClass, '').then(response => {
            const allHospitals = response.data || []
            // 按地域排序:本地区域优先
            this.hospitalInResults = this.sortHospitalsByRegion(allHospitals)
            // 如果没有常用医院,降级为普通搜索
            if (this.hospitalInResults.length === 0) {
              searchHospitals('', '').then(res => {
                const allHospitals = res.data || []
                this.hospitalInResults = this.sortHospitalsByRegion(allHospitals)
              })
            }
          }).catch(error => {
            console.error('加载常用转入医院失败:', error)
            searchHospitals('', '').then(res => {
              const allHospitals = res.data || []
              this.hospitalInResults = this.sortHospitalsByRegion(allHospitals)
            })
          })
        } else {
          // 没有服务单编码,使用普通搜索
          searchHospitals('', '').then(response => {
            const allHospitals = response.data || []
            // 按地域排序:本地区域优先
            this.hospitalInResults = this.sortHospitalsByRegion(allHospitals)
          }).catch(error => {
            console.error('加载转入医院失败:', error)
            this.hospitalInResults = []
          })
        }
      }
      this.showHospitalInResults = true
    },
@@ -885,27 +1056,37 @@
        clearTimeout(this.searchTimer)
      }
      
      // 如果关键词为空,显示默认100条
      // 如果关键词为空,显示所有医院(本地区域优先)
      if (!keyword || keyword.trim() === '') {
        this.hospitalInResults = [...this.defaultHospitals]
        searchHospitals('', '').then(response => {
          const allHospitals = response.data || []
          // 按地域排序:本地区域优先
          this.hospitalInResults = this.sortHospitalsByRegion(allHospitals)
        }).catch(error => {
          console.error('加载转入医院失败:', error)
          this.hospitalInResults = []
        })
        this.showHospitalInResults = true
        return
      }
      
      // 有关键词时,去服务端搜索
      // 有关键词时,去服务端搜索(不限区域,但结果按地域排序)
      this.searchTimer = setTimeout(() => {
        this.searchHospitalIn(keyword)
      }, 300)
    },
    
    // 搜索转入医院
    // 搜索转入医院(不限区域,但本地区域优先)
    searchHospitalIn(keyword) {
      // 传入关键词和地域过滤
      searchHospitals(keyword, this.selectedRegion).then(response => {
        this.hospitalInResults = response.data || []
      // 传入关键词,不传地域过滤(搜索所有区域)
      searchHospitals(keyword, '').then(response => {
        const allHospitals = response.data || []
        // 按地域排序:本地区域优先
        this.hospitalInResults = this.sortHospitalsByRegion(allHospitals)
        this.showHospitalInResults = true
        console.log('搜索转入医院:', keyword, '结果数:', this.hospitalInResults.length)
      }).catch(error => {
        console.error('搜索医院失败:', error)
        console.error('搜索转入医院失败:', error)
        this.hospitalInResults = []
      })
    },
@@ -921,13 +1102,12 @@
      this.showHospitalInResults = false
      this.hospitalInResults = []
      
      // 如果有GPS坐标,保存下来
      this.addressCoordinates.hospitalInAddress = null
      // 保存转入医院的城市信息
      this.taskForm.hospitalIn.city = hospital.hopsCity || ''
      
      // 如果两个医院都已选择,自动计算距离
      if (this.taskForm.hospitalOut.address) {
        // 这里可以调用地址解析和距离计算
        // 暂时留空,由用户手动输入距离
        this.calculateHospitalDistance()
      }
    },
    
@@ -1206,9 +1386,28 @@
      this.selectedDiseases.splice(index, 1)
    },
    
    // 设置默认转运时间为当前时间
    setDefaultTransferTime() {
      const now = new Date()
      const year = now.getFullYear()
      const month = String(now.getMonth() + 1).padStart(2, '0')
      const day = String(now.getDate()).padStart(2, '0')
      const hours = String(now.getHours()).padStart(2, '0')
      const minutes = String(now.getMinutes()).padStart(2, '0')
      const seconds = String(now.getSeconds()).padStart(2, '0')
      // 格式化为 YYYY-MM-DD HH:mm:ss
      this.taskForm.transferTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
    },
    validateForm() {
      if (!this.selectedVehicleId) {
        this.$modal.showToast('请选择任务车辆')
        return false
      }
      if (!this.selectedOrganizationId) {
        this.$modal.showToast('请选择归属机构')
        return false
      }
      
@@ -1219,6 +1418,11 @@
      
      if (!this.selectedDocumentType) {
        this.$modal.showToast('请选择单据类型')
        return false
      }
      if (!this.taskForm.transferTime) {
        this.$modal.showToast('请选择转运时间')
        return false
      }
      
@@ -1254,6 +1458,16 @@
      
      if (!this.taskForm.hospitalIn.department) {
        this.$modal.showToast('请选择转入医院科室')
        return false
      }
      if (!this.taskForm.transferDistance) {
        this.$modal.showToast('请输入转运公里数')
        return false
      }
      if (!this.taskForm.price) {
        this.$modal.showToast('请输入成交价')
        return false
      }
      
@@ -1382,6 +1596,50 @@
        parts.push(hospital.hospAddress)
      }
      return parts.join('')
    },
    // 自动计算两个医院之间的距离
    calculateHospitalDistance() {
      const fromAddress = this.taskForm.hospitalOut.address
      const fromCity = this.taskForm.hospitalOut.city
      const toAddress = this.taskForm.hospitalIn.address
      const toCity = this.taskForm.hospitalIn.city
      if (!fromAddress || !toAddress) {
        console.log('地址信息不完整,无法计算距离')
        return
      }
      console.log('开始计算距离:', fromAddress, '->', toAddress)
      // 显示加载提示
      uni.showLoading({
        title: '计算距离中...'
      })
      // 调用百度地图API计算距离
      baiduDistanceByAddress(fromAddress, fromCity, toAddress, toCity)
        .then(response => {
          uni.hideLoading()
          if (response.code === 200 && response.data) {
            const distanceInMeters = response.data.distance
            // 转换为公里,保留1位小数
            const distanceInKm = (distanceInMeters / 1000).toFixed(1)
            this.taskForm.transferDistance = distanceInKm
            console.log('距离计算成功:', distanceInKm, 'km')
            // this.$modal.showToast(`距离计算成功: ${distanceInKm}公里`)
          } else {
            console.error('距离计算失败:', response.msg)
            this.$modal.showToast('距离计算失败,请手动输入')
          }
        })
        .catch(error => {
          uni.hideLoading()
          console.error('距离计算失败:', error)
          this.$modal.showToast('距离计算失败,请手动输入')
        })
    }
  }
}