董国庆
2026-07-14 11fa99f45663fc39cda7918aa97e134c8e337de7
pages/advoad-city-search/advoad-message/advoad-message.js
@@ -1,6 +1,7 @@
// pages/advoad-city-search/advoad-message/advoad-message.js
const app = getApp()
var WxParse  = require("../../../utils/wxParse/wxParse")
const GdMap = require('../../../utils/amap-wx')
const {
  queryLocation,
@@ -8,8 +9,11 @@
  orderCrossCity,
  queryBalance,
  queryByType,
  isElectronicLedger
  isElectronicLedger,
  areaMonitoringexport
} = require('../../../utils/ajax')
const ADVOAD_START_LOCATION_STORAGE_KEY = 'advoadStartSelectedLocation'
const ADVOAD_END_LOCATION_STORAGE_KEY = 'advoadEndSelectedLocation'
Page({
  /**
@@ -47,7 +51,157 @@
    canOrder:true,//禁止下单
    wxParse:false,//乘车须知
    isAllIn:false,//是否包车 显示选择座位
    peopleArr:0
    canChooseSeat:true,//是否展示选座
    peopleArr:0,
    lineDestinationId:'',
    skipStartLocationCacheSync:false
  },
  getAvailablePeopleCount(data = this.data){
    const self = data.self || {}
    const numberText = String(self.number || '')
    const [usedSeatText, totalSeatText] = numberText.split('/')
    const usedSeat = Number(usedSeatText || 0)
    const totalSeat = Number(totalSeatText || 0)
    const availableSeat = totalSeat - usedSeat
    return availableSeat > 0 ? availableSeat : 0
  },
  getEffectivePeopleId(data = this.data){
    if(Number(data.radioVlaue) == 2){
      return this.getAvailablePeopleCount(data)
    }
    return data.peopleId
  },
  isSeatSelectionRequired(data = this.data){
    return data.radioVlaue != 2 && data.canChooseSeat
  },
  getStoredRouteLocation(where){
    const storageKey = where == 'startLocation'
      ? ADVOAD_START_LOCATION_STORAGE_KEY
      : ADVOAD_END_LOCATION_STORAGE_KEY
    return wx.getStorageSync(storageKey)
  },
  getCurrentLocationDetail(){
    return new Promise(async (resolve) => {
      try{
        const locationRes = await new Promise((locationResolve, locationReject) => {
          wx.getLocation({
            type: 'gcj02',
            success: locationResolve,
            fail: locationReject
          })
        })
        const latitude = locationRes.latitude
        const longitude = locationRes.longitude
        const myAmapFun = new GdMap.AMapWX({
          key: 'cea7923a93987b959ab1d2f8ab44d497'
        })
        const regeoRes = await new Promise((regeoResolve, regeoReject) => {
          myAmapFun.getRegeo({
            location: `${longitude},${latitude}`,
            success: regeoResolve,
            fail: regeoReject
          })
        })
        const item = regeoRes && regeoRes[0]
        const regeocodeData = item && item.regeocodeData
        const addressComponent = (regeocodeData && regeocodeData.addressComponent) || {}
        const pois = (regeocodeData && regeocodeData.pois) || []
        const landmarkName = (item && item.name) || (pois[0] && pois[0].name) || '当前位置'
        const formattedAddress = (regeocodeData && regeocodeData.formatted_address) || landmarkName
        const adcode = addressComponent.adcode || wx.getStorageSync('adcode') || ''
        const userLocation = `${longitude},${latitude}`
        wx.setStorageSync('latitude', latitude)
        wx.setStorageSync('longitude', longitude)
        wx.setStorageSync('adcode', adcode)
        wx.setStorageSync('formatted_address', formattedAddress)
        wx.setStorageSync('userLocation', userLocation)
        resolve({
          latitude,
          longitude,
          adcode,
          landmarkName,
          formattedAddress,
          location: userLocation
        })
      }catch(error){
        resolve(null)
      }
    })
  },
  async tryAutoFillCurrentStartLocation(){
    const self = this.data.self || {}
    if(!self.abovadStartId){
      return
    }
    if(
      this.data.startLocation != '请选择上车点' &&
      Object.keys(this.data.startInfo || {}).length !== 0
    ){
      return
    }
    const currentLocation = await this.getCurrentLocationDetail()
    if(!currentLocation || !currentLocation.adcode){
      return
    }
    try{
      const res = await areaMonitoringexport(
        currentLocation.adcode,
        currentLocation.location,
        self.abovadStartId
      )
      const data = res.data || {}
      if(data.code == 200 && data.data == 1){
        this.setData({
          startLocation: currentLocation.formattedAddress || currentLocation.landmarkName || '当前位置',
          startInfo: {
            startLat: currentLocation.latitude,
            startLon: currentLocation.longitude
          }
        },()=>{
          this.queryOrderMoney()
        })
      }
    }catch(error){
    }
  },
  syncStoredRouteLocation(options = {}){
    const {
      syncStart = true,
      syncEnd = true
    } = options
    let self = this.data.self || {}
    if(Object.keys(self).length === 0){
      return
    }
    let updates = {}
    let startLocationCache = this.getStoredRouteLocation('startLocation')
    let endLocationCache = this.getStoredRouteLocation('endLocation')
    if(
      syncStart &&
      startLocationCache &&
      String(startLocationCache.siteId) == String(self.abovadStartId) &&
      startLocationCache.locationName &&
      startLocationCache.locationInfo
    ){
      updates.startLocation = startLocationCache.locationName
      updates.startInfo = startLocationCache.locationInfo
    }
    if(
      syncEnd &&
      endLocationCache &&
      String(endLocationCache.siteId) == String(self.abovadEndId) &&
      endLocationCache.locationName &&
      endLocationCache.locationInfo
    ){
      updates.endLocation = endLocationCache.locationName
      updates.endInfo = endLocationCache.locationInfo
      updates.lineDestinationId = endLocationCache.lineDestinationId || ''
    }
    if(Object.keys(updates).length > 0){
      this.setData(updates,()=>{
        this.queryOrderMoney()
      })
    }
  },
  closePop(){
    this.setData({
@@ -60,11 +214,12 @@
     if(radioVlaue == 2){
        this.setData({
          isAllIn:true,
          zwNum:"请选择座位",
          zwNum:this.data.canChooseSeat ? "请选择座位" : "",
        })
     }else{
        this.setData({
          isAllIn:false
          isAllIn:false,
          zwNum:this.data.canChooseSeat ? "请选择座位" : ""
        })
     }
     this.setData({
@@ -94,7 +249,7 @@
    this.setData({
      peopleNum:peopleId+'人',
      peopleId,
      zwNum:'请选择座位',
      zwNum:this.data.canChooseSeat ? '请选择座位' : '',
    },()=>{
      this.queryOrderMoney()
    })
@@ -103,7 +258,7 @@
  zwNum(){
    let self  = this.data.self
    let data = this.data
    if(data.peopleNum=='请选择出行人数'){
    if(Number(data.radioVlaue) != 2 && data.peopleNum=='请选择出行人数'){
     app.showToastSuccess('请先选择出行人数',null,'none',1500)
     return false
    }
@@ -199,9 +354,11 @@
  async orderCrossCityAjax (){
    let dataSelf = this.data.self
    let data = this.data
    let peopleId = this.getEffectivePeopleId(data)
    console.log('数据',this.data);
    let userLocation = wx.getStorageSync('userLocation')
    let zwNumLength =    data.zwNum.split(',').length
    let needChooseSeat = this.isSeatSelectionRequired(data)
    let zwNumLength = needChooseSeat && data.zwNum ? data.zwNum.split(',').length : 0
    let mask = '' 
      data.maskList.map( x =>{
        if(x.checked){
@@ -209,12 +366,12 @@
          mask+=','+x.name
        } 
      })
      console.log(data.peopleId,zwNumLength)
      if((data.peopleId!==zwNumLength)&& data.radioVlaue!=2){
      console.log(peopleId,zwNumLength)
      if(needChooseSeat && (peopleId!==zwNumLength)){
        app.showToastSuccess('请正确选择座位',null,'none',1500)
        return false
      }
    let seatNumber = data.zwNum == '请选择座位' ? '' : data.zwNum
    let seatNumber = needChooseSeat && data.zwNum != '请选择座位' ? data.zwNum : ''
    let dataInfo = {
      carId : dataSelf.carId,//车辆id
      distance :data.distance ,//行驶里程数
@@ -224,8 +381,9 @@
      endLon :  data.endInfo.startLon,//终点经度
      lineId  : dataSelf.lineSelf.id||dataSelf.lineId,//线路ID
      lineShiftDriverId  : dataSelf.lineShiftDriverId,//排班ID
      lineDestinationId : data.lineDestinationId || '',
      orderSource  : 3,
      peopleNumber  : data.peopleId,//出行人数
      peopleNumber  : peopleId,//出行人数
      placementAddress : wx.getStorageSync('formatted_address'),//下单详细位置
      placementLat  : userLocation.split(',')[1],//下单经纬
      placementLon   : userLocation.split(',')[0],
@@ -268,8 +426,8 @@
      startLocation: data.startLocation,
      endLocation:data.endLocation,
      driveInfo:dataSelf.name + '-' +dataSelf.carLicensePlate,
      zwNum: people||data.zwNum,
      zwPeople : peopleNum||data.peopleId,
      zwNum: people || seatNumber,
      zwPeople : peopleNum||peopleId,
      orderId: res.data.data.id,
      orderType:3,
      couponId:this.data.couponId,
@@ -312,20 +470,24 @@
      abvoadEndLocation
    } = this.data.self
    wx.navigateTo({
      url: `./electronic-fence/electronic-fence?where=endLocation&abovadEndId=${abovadEndId}&abvoadEndLocation=${abvoadEndLocation}`,
      url: `./electronic-fence/electronic-fence?where=endLocation&abovadStartId=${abovadStartId}&abovadEndId=${abovadEndId}&abvoadEndLocation=${abvoadEndLocation}`,
    })
  },
  //获取预估金额
 async queryOrderMoney(){
    let data = this.data
    let peopleId = this.getEffectivePeopleId(data)
    let totalSeat = data.self.number.split('/')[1]
    if((data.zwNum!=='请选择座位'|| data.radioVlaue==2)&&data.startLocation!=='请选择上车点'&&
    let needChooseSeat = this.isSeatSelectionRequired(data)
    let seatReady = !needChooseSeat || data.zwNum !== '请选择座位'
    let seatNumber = needChooseSeat ? data.zwNum : ''
    if(seatReady&&data.startLocation!=='请选择上车点'&&
    data.endLocation!=='请选择下车点'&&
    Object.keys(data.startInfo).length!==0&&
    Object.keys(data.endInfo).length!==0&&
    data.peopleId!==''&&
    peopleId!==''&&
    data.radioVlaue!==''&&
    data.zwNum!==''
    (!needChooseSeat || data.zwNum!=='')
    ){
      this.setData({
        canOrder:false
@@ -336,11 +498,12 @@
        endLonLat ,
        startLonLat ,
        data.self.lineSelf.id||data.self.lineId ,
        data.peopleId ,
        data.zwNum ,
        peopleId ,
        seatNumber ,
        data.self.serverCarModelId ,
        totalSeat ,
        data.radioVlaue
        data.radioVlaue ,
        data.lineDestinationId || ''
      )
      res.data.code == 200
      ?
@@ -372,7 +535,12 @@
       let  self = JSON.parse(options.self)
       console.log(self)
       this.setData({
         self
         self,
         canChooseSeat:self.chooseSeat !== false,
         zwNum:self.chooseSeat === false ? '' : this.data.zwNum,
         skipStartLocationCacheSync:true
       },()=>{
         this.tryAutoFillCurrentStartLocation()
       })
     }
  },
@@ -389,6 +557,15 @@
   */
  onShow: function () {
       console.log(this.data.startInfo,this.data.endInfo)
       this.syncStoredRouteLocation({
        syncStart: !this.data.skipStartLocationCacheSync
       })
       wx.removeStorageSync(ADVOAD_END_LOCATION_STORAGE_KEY)
       if(this.data.skipStartLocationCacheSync){
        this.setData({
          skipStartLocationCacheSync:false
        })
       }
  },
@@ -426,4 +603,4 @@
  onShareAppMessage: function () {
  }
})
})