/
axios-plugin.js
executable file
·65 lines (59 loc) · 2.36 KB
/
axios-plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import axios from "axios";
function applyAxiosInterceptor(store, router) {
axios.interceptors.request.use(config => {
return config;
});
axios.interceptors.response.use(
(response) => {
return response;
},
(data) => {
let response = data.response;
if ("ERR_NETWORK" === data.code) {
let payload = {
level: "ERROR",
description: "Сервис не доступен. Проверьте подключение к сети Интернет или повторите действие позднее",
stacktrace: []
};
store.dispatch('setException', payload);
return Promise.reject(response);
}
if (response.status === 401) {
router.replace({name: "login"});
return Promise.reject(response);
}
if (response.status === 403) {
let payload = {
level: "ERROR",
message: "Отказано в доступе",
}
store.dispatch('setNotification', payload);
return Promise.reject(response);
}
let exception = response.data;
if (!!exception) {
let payload = {
level: "ERROR",
description: "Внутренняя ошибка сервиса",
stacktrace: []
};
if (!!exception.message) {
payload.description = exception.message;
}
if (!!exception.stacktrace) {
payload.stacktrace = exception.stacktrace;
}
store.dispatch('setException', payload);
}
return Promise.reject(response);
});
}
export default {
install(Vue, options) {
const axiosHost = process.env.VUE_APP_SERVER === 'remote' ? process.env.VUE_APP_HOST : window.location.host;
axios.defaults.baseURL = process.env.VUE_APP_PROTOCOL + '://' + axiosHost + process.env.VUE_APP_CONTEXT;
axios.defaults.withCredentials = true;
axios.defaults.hostURL = process.env.VUE_APP_PROTOCOL + '://' + axiosHost;
applyAxiosInterceptor(options.store, options.router);
}
}