import axios , { AxiosResponse } from 'axios' ; import { CACHE , UA } from '@/config' ; import { HttpError , HttpStatusCode } from '@/models' ; import queryString from 'query-string' ; author CaoMeiYouRen
 *  date export param { string } url param { * } query param { * } data param { * } method param { * } headers returns export async function ajax ( url , query = { } , data = { } , method = 'GET' , headers = { } , charset ? ) { try { headers = Object . assign ( { 'User-Agent' : UA , Referer : url } , headers ) ; const result = await axios ( url , { method , headers , params : query , data , timeout : 10000 , } ) ; return result ; } catch ( error ) { let e = { } ; if ( error . toJSON ) { e = error . toJSON ( ) ; } else { e = error ; } if ( ! ( e instanceof HttpError ) ) { e . status = error ?. response ?. status || HttpStatusCode . INTERNAL_SERVER_ERROR ; e . data = error ?. response ?. data ; } console . log ( e ) ; return e ; } } class AjaxConfig { url : string ; query ? : any = { } ; data ? : any = { } ; method ? : any = 'GET' ; headers ? : any = { } ; charset ? : string ; } author CaoMeiYouRen
 *  date export param { AjaxConfig } config returns { Promise < AxiosResponse < any > > } export async function ajax2 ( config = new AjaxConfig ( ) ) { try { const { url , query , data , method , charset } = config ; const headers = Object . assign ( { 'User-Agent' : UA , Referer : url } , config . headers ) ; const result = await axios ( url , { method , headers , params : query , data , timeout : 10000 , transformRequest ( _data ) { if ( headers [ 'Content-Type' ] === 'application/x-www-form-urlencoded' ) { _data = queryString . stringify ( _data ) ; } return _data ; } , } ) ; return result ; } catch ( error ) { let e = { } ; if ( error . toJSON ) { e = error . toJSON ( ) ; } else { e = error ; } if ( ! ( e instanceof HttpError ) ) { e . status = error ?. response ?. status || HttpStatusCode . INTERNAL_SERVER_ERROR ; e . data = error ?. response ?. data ; } console . log ( e ) ; return e ; } } author CaoMeiYouRen
 *  date export returns { Promise < string > } export async function getPublicIP ( ) { try { const res = await axios . get ( 'https://ipv4.icanhazip.com/' ) ; let ip = res . data ; ip = ip . replace ( /\n/g , '' ) ; return ip ; } catch ( error ) { console . log ( error ) ; return '' ; } }