export function getMapAddress() {
  // 高德配置
  const TIANDITU_KEY = '4ce26ecef55ae1ec47910a72a098efc0'
  return new Promise<{ address: string }>((resolve, reject) => {
    uni.getLocation({
      type: 'wgs84',
      altitude: true,
      isHighAccuracy: true,
      success(res) {
        console.log(res, '经纬度')
        const url = `https://api.tianditu.gov.cn/geocoder?postStr={'lon':${res.longitude},'lat':${res.latitude},'ver':2}&type=geocode&tk=${TIANDITU_KEY}`
        uni.request({
          url: url,
          method: 'GET',
          success(res: any) {
            resolve({ address: res?.data?.result?.formatted_address || '' })
            console.log(res, 'address')
          },
          fail(res) {
            resolve({ address: '拒绝定位' })
            console.error('获取地址失败:', res)
            uni.showModal({
              title: '提示',
              content: '请先允许定位！',
              success: function (res) {
                if (res.confirm) {
                  uni.openSetting()
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
        })
      },
      fail(e) {
        console.log('拒绝定位', e)
      }
    })
  })
}


export function getMapLbs() {
  // 高德配置
  const AMAP_KEY = 'be262c006216c542747fce766130cee3'
  return new Promise<{ address: string }>((resolve, reject) => {
    uni.getLocation({
      type: 'gcj02',
      altitude: true,
      isHighAccuracy: true,
      success(res) {
        console.log(res, '经纬度')
        const url = `https://restapi.amap.com/v3/geocode/regeo?location=${res.longitude},${res.latitude}&key=${AMAP_KEY}`
        uni.request({
          url: url,
          method: 'GET',
          success(res: any) {
            console.log(res, 'address')
            resolve(res?.data?.regeocode)
          },
          fail(res) {
            resolve({ address: '拒绝定位' })
            console.error('获取地址失败:', res)
            uni.showModal({
              title: '提示',
              content: '请先允许定位！',
              success: function (res) {
                if (res.confirm) {
                  uni.openSetting()
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
        })
      },
      fail(e) {
        console.log('拒绝定位', e)
      }
    })
  })
}



export function getShowApiAddress() {
  // ShowAPI(易源数据) 逆地理编码
  const SHOWAPI_APP_KEY = 'C59885FE214644168eB4197eFe9a48b9'
  return new Promise<{ formatted_address: string }>((resolve, reject) => {
    uni.getLocation({
      type: 'wgs84',
      altitude: true,
      isHighAccuracy: true,
      success(res) {
        console.log(res, '经纬度')
        const url = `https://route.showapi.com/238-3?appKey=${SHOWAPI_APP_KEY}&lng=${res.longitude}&lat=${res.latitude}&from=5`
        uni.request({
          url: url,
          method: 'GET',
          success(res: any) {
            console.log(res, 'address')
            resolve({ formatted_address: res?.data?.showapi_res_body?.formatted_address || '' })
          },
          fail(res) {
            resolve({ formatted_address: '拒绝定位' })
            console.error('获取地址失败:', res)
            uni.showModal({
              title: '提示',
              content: '请先允许定位！',
              success: function (res) {
                if (res.confirm) {
                  uni.openSetting()
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
        })
      },
      fail(e) {
        console.log('拒绝定位', e)
      }
    })
  })
}
