import { Config , Inject , Provide } from '@midwayjs/decorator' ; import { BaseService } from '../../base' ; import { AxiosInstance } from 'axios' ; import { AmapConfig , AmapIPAddress , AmapWeatherForecast , AmapWeatherLive , } from './interface' ; export const AMAP_BASEURL = 'https://restapi.amap.com/v3' ; export const WEATHER_API = '/weather/weatherInfo' ; export const IP_ADDRESS_API = '/ip' ; /**
 * 高德Web服务Api调用Service
 */ @ Provide ( ) export class AmapWebService extends BaseService { @ Inject ( ) httpclient : AxiosInstance ; @ Config ( 'amap' ) amapConfig : AmapConfig ; param adcode param extensions async queryWeather ( adcode , extensions = 'base' ) { const result = await this . httpclient ( { url : `${ AMAP_BASEURL }${ WEATHER_API }` , method : 'get' , params : { key : this . amapConfig . key , city : adcode , extensions , output : 'JSON' , } , } ) ; const { data } = result ; if ( data . infocode !== '10000' ) { throw new Error ( data . info ) ; } if ( extensions === 'base' ) { return data . lives [ 0 ] ; } else { return data . forecasts [ 0 ] ; } } param ip async queryIPAddress ( ip ? ) { const result = await this . httpclient ( { url : `${ AMAP_BASEURL }${ IP_ADDRESS_API }` , method : 'get' , params : { key : this . amapConfig . key , ip , output : 'JSON' , } , } ) ; const { data } = result ; if ( data . infocode !== '10000' ) { throw new Error ( data . info ) ; } return { province : data . province , city : data . city , adcode : data . adcode , rectangle : data . rectangle , } ; } }