index.js 7.54 KB

const app = getApp()
const restapi = require('../restapi.js');
const mapapi = require('../../../../utils/mapapi.js');
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    test:true,
    mapHeight: 446,
    markers: [],
    latitude:0, //维度
    longitude:0,  //经度
    userId:'',
    address: '',
    title: '',
    distance: '',
    time: '',
    selLat: 0,
    selLng: 0,
    flag: false,
    curTypeId: null,
    hasPosition:false,
  },
  // 动态改变地图高度
  changeHeight() {
    let phoneHeight = wx.getSystemInfoSync().windowHeight
    let query = wx.createSelectorQuery().in(this)
    wx.canIUse('nextTick')
    ?
      wx.nextTick(()=>{
        query.select('#console').boundingClientRect(res => {
          this.setData({
            mapHeight: (phoneHeight - res.height)
          })
        }).exec()
      })
    :
      query.select('#console').boundingClientRect(res => {
        this.setData({
          mapHeight: (phoneHeight - res.height)
        })
      }).exec()
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("options",options)
    let that = this;
    let userIcon = null;
    if (!app.globalData.userInfo) {
      app.userInfoReadyCallback = userInfo => {
        if (userInfo != null) {
          let info = app.globalData.userInfo
          console.log(info);
          userIcon = info.avatarUrl;
          if(!info.avatarUrl){
            userIcon = info.logo;
          }
          that.setData({
            userIcon: userIcon
          })
        }
      }
    }else{
      let info = app.globalData.userInfo
      console.log(info)
      userIcon = info.avatarUrl;
      if (!info.avatarUrl) {
        userIcon = info.logo;
      }
      this.setData({
        userIcon: userIcon
      })
    }
    console.log(options);
    this.setData({
      userId: options.protocolId,
      curTypeId: app.globalData.selectedDevice.typeId
    });
    if(options.source && options.source == 'luck'){
      this.setData({
        luck: true
      })
    }
    this.setData({
      test: true,
      flag: false,
      hasPosition:false
    });
    this.changeHeight();
    this.getSelfLocation();
  },

  getSelfLocation: function(){
    let userId = this.data.userId;
    console.log("getSelfLocation userId",userId)
    let that = this;
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        let flat = res.latitude;
        let flng = res.longitude;
        that.setData({
          selLat: flat,
          selLng: flng,
          latitude: flat,
          longitude: flng
        });
        that.getLoc();
      },
      fail: function () {
        wx.authorize({
          scope: 'scope.userLocation',
          success() {
            that.getSelfLocation();
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  getLoc: function(){
    let userId = this.data.userId;
    console.log(userId)
    let that = this;
    if(userId){
      restapi.getVehiclePosition(userId, function (response) {
        console.log("getVehiclePosition", response);
        if (null == response.data.didiBOxlng || null == response.data.didiBOxlat || response.data.didiBOxlng == '' || response.data.didiBOxlat == '') {
          let flat = that.data.selLat;
          let flng = that.data.selLng;
          let markers = [{
            id: 0,
            longitude: flng,
            latitude: flat,
            iconPath: '/packageSmartPlus/resources/images/location.png',
            width: '76rpx',
            height: '76rpx',
          }];
          that.setData({
            latitude: flat,
            longitude: flng,
            test: false,
            flag: true,
            markers: markers,
          })
          that.changeHeight();
        } else {
          that.setData({
            hasPosition:true
          })
          that.showCarPostion(response.data.didiBOxlng, response.data.didiBOxlat, response.data.showTime);
        }
      },function () {
        app.showMessage("获取车辆位置信息失败!");
      });
    }else{
      let def = {
        didiBOxTime: new Date(),
        didiBOxlat: "22.587337",
        didiBOxlng: "113.991485",
        showTime: "0",
        weapp: true
      }
      that.setData({
        hasPosition:true
      })
      that.showCarPostion(def.didiBOxlng, def.didiBOxlat, def.showTime);
    }
    setTimeout(() => {
      console.log("setTimeout,5秒后", that.data.hasPosition)
      if (!that.data.hasPosition) {
        that.setData({
          flag: true
        })
      } else {
        that.setData({
          flag: false
        })
      }
    }, 5000)
  },


  /**
   * 展示车辆位置
   */
  showCarPostion: function (lng, lat, time) {
    let that = this;
    let markers = [{
      id: 0,
      longitude: lng,
      latitude: lat,
      iconPath: '/packageSmartPlus/resources/images/location.png',
      width: '76rpx',
      height: '76rpx',
    }]
    mapapi.getDetailedAdrress(lng, lat, function (data) {
      let str = "";
      let address = data.address;
      if (data.address_reference && data.address_reference.landmark_l2) {
        str = data.address_reference.landmark_l2.title;
      } else if (data.address_reference && data.address_reference.landmark_l1) {
        str = data.address_reference.landmark_l1.title;
      } else if (data.formatted_addresses && data.formatted_addresses.recommend) {
        str = data.formatted_addresses.recommend;
      } else if (data.formatted_addresses && data.formatted_addresses.rough) {
        str = data.formatted_addresses.rough;
      }
      that.setData({
        address: address,
        title: str,
        time: time,
        latitude: lat,
        longitude: lng,
        markers: markers,
        test: false
      });
      let flat = that.data.selLat;
      let flng = that.data.selLng;
      mapapi.calculateDistan(flng, flat, lng, lat, function (distance) {
        if (distance) {
          //计算距离
          if (distance <= 1000) {
            distance = distance + '米';
          } else {
            distance = (distance / 1000).toFixed(2) + '公里';
          }
          that.setData({
            distance: distance
          })
        }
      });
      setTimeout(_ =>{
        that.changeHeight();
      },1000)
      
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userId = this.data.userId;
    let lng = this.data.longitude;
    let lat = this.data.latitude;
    let stTime = new Date().getTime();
    this.setData({
      latitude: + lat - 0.0025, 
      longitude: lng,
    })
    console.log(userId);
    console.log(lng);
    console.log(lat);
    console.log(stTime);
    setTimeout(()=>{
      this.setData({
        latitude: lat,
        longitude: lng,
      })
    },2000)
    console.log(`carlng=${lng}&carlat=${lat}&stTime=${stTime}&title=${this.data.title}&userId=${userId}`)
    let path = `packageSmartPlus/pages/vehicleTracking/share/share?carlng=${lng}&carlat=${lat}&stTime=${stTime}&title=智能车联网车辆位置分享&userId=${userId}`;
    return {
      title: '车辆位置分享',
      path: path
    }
    
  },
  
  /**
   * 导航
   */
  guideToCar: function(){
    let lat = this.data.latitude;
    let lng = this.data.longitude;
    let title = this.data.title;
    wx.openLocation({
      latitude: parseFloat(lat),
      longitude: parseFloat(lng),
      name:title,
    })
  }
})