wlzboy
2025-11-01 8b005a808d6ab8fae1480ed57bdfd68af2dafcd4
app/pages/task/create-emergency.vue
@@ -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" 
@@ -443,7 +443,7 @@
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 { calculateDistance, baiduDistanceByAddress } from "@/api/map"
import { searchHospitals } from "@/api/hospital"
import { listUser } from "@/api/system/user"
import { searchIcd10 } from "@/api/icd10"
@@ -560,6 +560,9 @@
    }
  },
  onLoad(options) {
    // 设置默认转运时间为当前时间
    this.setDefaultTransferTime()
    // 先加载车辆列表,然后加载绑定车辆信息
    this.getAvailableVehicles().then(() => {
      this.getUserBoundVehicleInfo()
@@ -902,15 +905,12 @@
      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()
      }
    },
    
@@ -986,13 +986,12 @@
      this.showHospitalInResults = false
      this.hospitalInResults = []
      
      // 如果有GPS坐标,保存下来
      this.addressCoordinates.hospitalInAddress = null
      // 保存转入医院的城市信息
      this.taskForm.hospitalIn.city = hospital.hopsCity || ''
      
      // 如果两个医院都已选择,自动计算距离
      if (this.taskForm.hospitalOut.address) {
        // 这里可以调用地址解析和距离计算
        // 暂时留空,由用户手动输入距离
        this.calculateHospitalDistance()
      }
    },
    
@@ -1271,6 +1270,20 @@
      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('请选择任务车辆')
@@ -1284,6 +1297,11 @@
      
      if (!this.selectedDocumentType) {
        this.$modal.showToast('请选择单据类型')
        return false
      }
      if (!this.taskForm.transferTime) {
        this.$modal.showToast('请选择转运时间')
        return false
      }
      
@@ -1447,6 +1465,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('距离计算失败,请手动输入')
        })
    }
  }
}