1
1
import axios , { AxiosError } from "axios" ;
2
2
import type { AxiosRequestConfig , AxiosResponse } from "axios" ;
3
- import { ElLoading , ElMessage } from "element-plus" ;
4
3
import { API_URL } from "@/assets/js/global" ;
5
4
import { clearLocal } from "./auth" ;
6
5
@@ -20,34 +19,42 @@ axios.defaults.baseURL = API_URL;
20
19
axios . interceptors . request . use (
21
20
( config : any ) => {
22
21
const { isShowLoading } = config ;
23
- const target = ".card, .el-table-v2__body, #line"
22
+ const target = ".card, .el-table-v2__body, #line" ;
24
23
isShowLoading && ( loading = ElLoading . service ( { lock : true , text : "加载中..." , target } ) ) ;
25
24
return config ;
26
25
} ,
27
- ( error : AxiosError ) => { // 发送请求时出了点问题,比如网络错误 https://segmentfault.com/q/1010000020659252
26
+ ( error : AxiosError ) => {
27
+ // 发送请求时出了点问题,比如网络错误 https://segmentfault.com/q/1010000020659252
28
28
ElMessage . error ( `请求发起错误 -- ${ error . message } ` ) ;
29
29
return Promise . reject ( error ) ;
30
30
}
31
31
) ;
32
32
33
33
// 响应拦截器(全局配置)
34
34
axios . interceptors . response . use (
35
- ( response : AxiosResponse < IResponseData > ) => { // status >= 200 && status < 300 (HTTP 成功)
36
- const { data : { code, msg } , config } = response ;
35
+ ( response : AxiosResponse < IResponseData > ) => {
36
+ // status >= 200 && status < 300 (HTTP 成功)
37
+ const {
38
+ data : { code, msg } ,
39
+ config,
40
+ } = response ;
37
41
const { isShowLoading, isShowFailToast, isThrowError } = config as IRequestOption ; // 请求配置
38
42
39
43
isShowLoading && loading ?. close ( ) ; // 关闭Loading遮罩层
40
44
41
- if ( code == 0 ) { // 业务成功 (后端定义的成功)
45
+ if ( code == 0 ) {
46
+ // 业务成功 (后端定义的成功)
42
47
// ElMessage.success("请求成功!");
43
- } else { // 业务失败 (后端定义的失败)
48
+ } else {
49
+ // 业务失败 (后端定义的失败)
44
50
isShowFailToast && ElMessage . error ( msg ) ;
45
51
if ( isThrowError ) throw new Error ( `后端返回的错误信息-- ${ msg } ` ) ; // 抛出错误, 阻止程序向下执行 (默认配置)
46
52
}
47
53
48
54
return response ;
49
55
} ,
50
- ( error : AxiosError ) => { // HTTP 失败
56
+ ( error : AxiosError ) => {
57
+ // HTTP 失败
51
58
const { response, config } = error ;
52
59
const { url, isShowLoading, isShowFailToast, isThrowError } = config as IRequestOption ;
53
60
@@ -56,16 +63,21 @@ axios.interceptors.response.use(
56
63
let errMsg = "" ; // 错误信息
57
64
let stateMsg : "error" | "warning" = "error" ; // 状态码
58
65
59
- if ( response ) { // 请求成功发出且服务器也响应了状态码,但状态代码超出了 2xx 的范围
66
+ if ( response ) {
67
+ // 请求成功发出且服务器也响应了状态码,但状态代码超出了 2xx 的范围
60
68
const { status, data } = response as AxiosResponse ;
61
69
errMsg = data . msg || `url:${ ( url || "" ) . toString ( ) } ,statusCode:${ status } ` ;
62
70
63
- if ( status == 401 ) { // 跳转登录
71
+ if ( status == 401 ) {
72
+ // 跳转登录
64
73
stateMsg = "warning" ;
65
- clearLocal ( )
66
- setTimeout ( ( ) => { window . location . href = "/login" } , 2000 ) ;
74
+ clearLocal ( ) ;
75
+ setTimeout ( ( ) => {
76
+ window . location . href = "/login" ;
77
+ } , 2000 ) ;
67
78
}
68
- } else { // 请求已经成功发起,但没有收到响应
79
+ } else {
80
+ // 请求已经成功发起,但没有收到响应
69
81
errMsg = "请求超时或服务器异常,请检查网络或联系管理员!" ;
70
82
}
71
83
@@ -111,7 +123,8 @@ export interface IRequestOption extends Partial<AxiosRequestConfig<IRequestData>
111
123
112
124
// 封装请求类
113
125
class Http {
114
- defaultOptions : IRequestOption = { // 自定义配置项默认值
126
+ defaultOptions : IRequestOption = {
127
+ // 自定义配置项默认值
115
128
isShowLoading : false ,
116
129
isShowFailToast : true ,
117
130
isThrowError : true ,
0 commit comments