62 lines
1.4 KiB
JavaScript
62 lines
1.4 KiB
JavaScript
import axios from "axios";
|
|
|
|
|
|
const client = axios.create({
|
|
baseURL: getBaseUrl()
|
|
});
|
|
|
|
export function getBaseHost() {
|
|
if (process.env.NODE_ENV === 'production') {
|
|
return location.host;
|
|
} else {
|
|
return '//devapi-local.tryfly.eu.org'
|
|
}
|
|
}
|
|
|
|
export function getBaseUrl() {
|
|
let host = getBaseHost();
|
|
if (process.env.NODE_ENV === 'production') {
|
|
return location.protocol + '//' + host;
|
|
} else {
|
|
return location.protocol + '//' + host;
|
|
}
|
|
}
|
|
|
|
class HttpError extends Error {
|
|
constructor(message, code) {
|
|
super(message)
|
|
this.code = code
|
|
this.name = 'HttpError'
|
|
}
|
|
}
|
|
|
|
client.interceptors.request.use(req => {
|
|
let accessToken = sessionStorage.getItem('G_PSID1');
|
|
if (accessToken) {
|
|
req.headers.Authorization = accessToken;
|
|
}
|
|
return req;
|
|
})
|
|
|
|
client.interceptors.response.use(res => {
|
|
if (res.status != 200) {
|
|
return Promise.reject(new Error(`http response ${res.status}:${res.statusText}`))
|
|
}
|
|
if (typeof res.data === 'object' && res.data.hasOwnProperty('code')) {
|
|
if (res.data['code'] === 0) {
|
|
return res.data['data']
|
|
} else {
|
|
return Promise.reject(new HttpError(res.data['reason'], res.data['code']))
|
|
}
|
|
}
|
|
if (typeof res.data === 'object' && res.data.hasOwnProperty('errno')) {
|
|
if (res.data['errno'] == 0) {
|
|
return res.data['result']
|
|
} else {
|
|
return Promise.reject(new HttpError(res.data['errmsg'], res.data['errno']))
|
|
}
|
|
}
|
|
return res;
|
|
})
|
|
|
|
export default client; |