index.js 2.63 KB
// pages/crowdfunding/components/navigation/index.js
import {
    iosTimeFormat
  } from '~/utils/format'
  Component({
    /**
     * 组件的属性列表
     */
    properties: {
      detail: {
        type: Object,
        value: {},
        observer: function (o) {
          this.init(o)
        }
      }
    },
  
    /**
     * 组件的初始数据
     */
    data: {
      addressInfo: {}
    },
  
    /**
     * 组件的方法列表
     */
    methods: {
      // 初始化
      init(data) {
        console.log(data)
        if (!data.startTime) {
          return false
        }
        data.s_time = new Date(iosTimeFormat(data.startTime)).format('yyyy年M月d日')
        data.e_time = new Date(iosTimeFormat(data.endTime)).format('yyyy年M月d日')
        this.setData({
          addressInfo: data
        })
      },
  
      // // 兼容ios时间
      // iosTimeFormat(time) {
      //   if (!time) {
      //     return ''
      //   }
      //   return time.replace(/-/g, '/')
      // },
  
      // 获取地址
      getLocation() {
        const appEnter = wx.getEnterOptionsSync()
        if (appEnter.scene === 1154) {
          wx.$eventbus.emit('showTimeLineToast')
          return
        }
        let _this = this
        let o = _this.data.addressInfo
  
        if (o.distance) {
          wx.openLocation({
            latitude: o.activityAddressLat,
            longitude: o.activityAddressLng
          })
          return false
        }
  
        if (!o.activityAddressLat || !o.activityAddressLng) {
          return false;
        }
  
        wx.getLocation({
          type: 'gcj02',
          success: function (res) {
            console.log(res)
            let distance = _this.getDistance(o.activityAddressLat, o.activityAddressLng, res.latitude, res.longitude)
            console.log(distance)
            o.distance = (distance / 1000).toFixed(2)
            _this.setData({
              addressInfo: o
            })
          }
        })
      },
  
      // 计算两个gps点之间距离
      getDistance(latFrom, lngFrom, latTo, lngTo) {
        var EARTH_RADIUS = 6378136.49
  
        function rad(d) {
          return (d * Math.PI) / 180.0
        }
        var radLatFrom = rad(latFrom)
        var radLatTo = rad(latTo)
        var a = radLatFrom - radLatTo
        var b = rad(lngFrom) - rad(lngTo)
        var distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLatFrom) * Math.cos(radLatTo) * Math.pow(Math.sin(b / 2), 2)))
        distance = distance * EARTH_RADIUS
        distance = Math.round(distance * 10000) / 10000
        return parseFloat(distance.toFixed(0))
      }
    }
  })