index.js 3.47 KB
// packageEquity/pages/road/index/index.js
import mapApi from '~/utils/mapapi/mapapi'
import { IMG_URL } from '~/config';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    IMGURL:IMG_URL,
    scale: 16,
    flag: false,
    editForm: {
      rescueType: 0,
      rescueTypeName: '紧急脱困'
    },
    rescueTypeList: [
      {
        confId: 0,
        confName: '紧急脱困'
      },
      {
        confId: 1,
        confName: '拖车'
      },
      {
        confId: 2,
        confName: '换胎'
      },
      {
        confId: 3,
        confName: '送油'
      },
      {
        confId: 4,
        confName: '搭电'
      },
      {
        confId: 5,
        confName: '事故救援'
      }
    ]
  },
  // onLoad: function (options) {},
  onLoad(options) {
    let { editForm } = this.data
    if (options.confId + 1) {
      editForm.rescueType = options.confId
      editForm.rescueTypeName = options.name
    }
    if(options&&options.source=='GH'){
      this.source = 'GH'
    }
    this.setData({
      editForm
    })
  },
  init() {
    // this.getLocation()
  },
  /**授权位置成功 */
  authSuccess() {
    this.setData({
      showAuth: false,
      flag: true,
      unauthorized: false
    })
    this.getLocation()
  },
  /**授权失败 */
  authFail() {
    this.setData({
      showAuth: true,
      unauthorized: true
    })
  },
  /**授权弹窗取消 */
  authCancel() {
    wx.p.router.back()
  },

  // 切换线索
  handleRescueType(e) {
    var targetItem = e.currentTarget.dataset.item
    this.setData({
      'editForm.rescueType': targetItem.confId,
      'editForm.rescueTypeName': targetItem.confName
    })
  },

  // 下一步
  handleNextStep() {
    let otherParams = {}
    if(this.source=='GH'){
      otherParams.source='GH'
    }
    wx.p.router.go('/packageEquity/pages/road/roadInfo/roadInfo', {
      editForm: this.data.editForm,
      addressObj: this.data.addressObj,
      isActivity: true,
      notIntercept: true,
      ...otherParams
    })
  },

  getLocation() {
    wx.getLocation({
      type: 'gcj02',
      success: res => {
        this.getCarPosition(res.latitude, res.longitude)
      }
    })
  },
  obtain(e) {
    let { latitude, longitude } = e.detail
    this.getCarPosition(latitude, longitude)
  },
  getCarPosition(latitude, longitude) {
    mapApi.detailedAdrress([latitude, longitude]).then(data => {
      this.setData({
        addressObj: data,
        latitude,
        longitude
      })
      this.getControlHeight(map => {
        map.addMarkers({
          markers: [
            {
              id: 1,
              iconPath: IMG_URL + 'equity/' + 'position.png' + `?v=${wx.$version}`,
              latitude,
              longitude,
              width: 28,
              height: 41,
              callout: {
                content: this.data.addressObj.formatted_addresses.recommend,
                color: '#282829',
                bgColor: '#FFFFFF',
                borderRadius: 10,
                padding: 14,
                anchorY: -13,
                borderWidth: 1,
                borderColor: 'rgba(0, 0, 0, 0.1)',
                display: 'ALWAYS'
              }
            }
          ],
          clear: true,
          success(res) {
            console.log(res)
          }
        })
      })
    })
  },

  getControlHeight(callBack) {
   wx.p.wxApi.getViewInfo('.control-box', ({ height }) => {
      this.setData({
        controlHeight: height
      })
      callBack(wx.createMapContext('map'))
    })
  }
})