1
import store from "@/store" ;
胡菁
authored
about a month ago
2
import router from "@/router" ;
3
4
5
import { getRefreshToken , getToken } from "@/utils/auth" ;
import axios from "axios" ;
import { Message } from "element-ui" ;
6
7
8
// create an axios instance
const service = axios . create ({
胡菁
authored
about a month ago
9
10
11
12
13
14
15
baseURL :
process . env . NODE_ENV === "development"
? window . appConfig . baseUrlOffLine
: window . appConfig . baseUrlOnLine , // process.env.VUE_APP_BASE_API, // url = base url + request url
// baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
// withCredentials: true, // send cookies when cross-domain requests
timeout : 5000 , // request timeout
16
});
17
18
19
// request interceptor
service . interceptors . request . use (
胡菁
authored
about a month ago
20
21
22
23
24
25
( config ) => {
// do something before request is sent
// console.log(config);
// if (config.url.indexOf("/user/") > -1) {
// config.baseURL = process.env.VUE_APP_BASE_API;
// }
26
胡菁
authored
about a month ago
27
28
29
30
31
32
33
34
35
36
37
38
// console.log(store.getters.token)
if ( store . getters . token ) {
config . headers [ "Authorization" ] = "Bearer " + getToken ();
config . headers [ "x-accesstoken" ] = getRefreshToken ();
var lang = localStorage . getItem ( "lang" );
var language = "zh" ;
if ( lang === "zh" ) {
language = "zh-CN" ;
} else if ( lang === "en" ) {
language = "en-US" ;
}
config . headers [ "Accept-Language" ] = language ;
39
}
胡菁
authored
about a month ago
40
41
42
43
44
45
46
return config ;
},
( error ) => {
// do something with request error
console . log ( error ); // for debug
return Promise . reject ( error );
}
47
);
48
49
50
// response interceptor
service . interceptors . response . use (
胡菁
authored
about a month ago
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
( response ) => {
const res = response . data ;
if ( res . code !== "Success" ) {
if ( res . msg === "" || res . msg === null || res . msg === undefined ) {
return ;
}
Message ({
message : res . msg || "Error" ,
type : "error" ,
duration : 5 * 1000 ,
});
return Promise . reject ( new Error ( res . msg || "Error" ));
} else {
return res ;
}
},
( error ) => {
// console.log(error.response)
let errorMessage = "请求错误" ;
if ( error . response ) {
if ( error . response . data . code === "403" ) {
Message ({
message : "该用户没有权限" ,
type : "error" ,
duration : 5 * 1000 ,
});
return ;
}
if ( error . response . data . code === "401" ) {
80
Message ({
胡菁
authored
about a month ago
81
82
83
message : "用户超时,请重新登录" ,
type : "error" ,
duration : 5 * 1000 ,
84
});
胡菁
authored
about a month ago
85
86
87
88
89
90
91
92
93
94
95
96
97
store . dispatch ( "user/logout" );
router . push ( ` / login ? redirect = $ { router . fullPath } ` );
// location.reload();
return ;
}
errorMessage = `请求失败: $ { error . response . status } - $ {
error . response . data ?. msg || error . response . statusText
} ` ;
// console.log(errorMessage);
} else if ( error . request ) {
errorMessage = "服务器未响应,请检查网络连接" ;
} else {
errorMessage = error . message || "未知错误" ;
98
}
胡菁
authored
about a month ago
99
100
101
102
103
104
105
Message ({
message : errorMessage ,
type : "error" ,
duration : 5 * 1000 ,
});
return Promise . reject ( error );
}
106
);
107
108
export default service ;