const {
  getAddressByLonLat
} = require('../api/api')

/**
 * 
 * 
 * 获取用户当前地理位置
 * 
 * @param {*} e 
 */
function getLocationAddress(e) {
  return new Promise(async (resolve, reject) => {
    wx.showLoading({
      title: '获取定位中',
    })
    wx.getLocation({
      type: 'gcj02',
      isHighAccuracy: true,
      success(resLocation) {
        getAddressByLonLat({
          lonLat: resLocation.longitude + "," + resLocation.latitude
        }).then((res) => {
          wx.hideLoading()
          if (res.code == 200) {
            var e = {
              res, //接口返回信息
              resLocation, //经纬度信息
            }
            resolve(e)
          } else {
            wx.showToast({
              title: '获取定位失败',
              icon: 'none'
            })
          }
        })
      },
      fail(res) {
        wx.hideLoading()
        wx.showModal({
          title: '提示',
          content: '请前往设置开启小程序定位授权',
          cancelText: "取消",
          confirmText: "确定",
          success(res) {
            if (res.confirm) {
              wx.openSetting({
                withSubscriptions: true,
              })
            }
          }
        })
      }
    })
  })
}

module.exports = {
  getLocationAddress
}