file module import { HttpService , Injectable } from '@nestjs/common' ; import * as geoip from 'geoip-lite' ; import * as APP_CONFIG from '../../app.config' ; export type IP = string ; export interface IIPDetail { city : string ; country : string ; } @ Injectable ( ) export class IpService { constructor ( private readonly httpService ) { } private queryIpByAliyun ( ip ) { return this . httpService . axiosRef . request ( { headers : { Authorization : `APPCODE ${ APP_CONFIG . ALIYUN . ip }` } , url : `https://api01.aliyun.venuscn.com/ip?ip=${ ip }` , } ) . then ( response => { if ( response . data && response . data . ret === 200 ) { return Promise . resolve ( response . data . data ) ; } else { return Promise . reject ( response . data ) ; } } ) ; } private queryIpByGeo ( ip ) { return geoip . lookup ( ip ) ; } query ( ip ) { return this . queryIpByAliyun ( ip ) . then ( ( { city , country } ) => ( { city , country } ) ) . catch ( error => Promise . resolve ( this . queryIpByGeo ( ip ) ) ) ; } }