66 * @Company :
77 * @Date : 2019-08-16 16:57:42
88 * @LastEditors : 水痕
9- * @LastEditTime : 2019-08-21 12 :23:41
9+ * @LastEditTime : 2019-08-21 16 :23:28
1010 */
1111
1212import axios from 'axios' ;
@@ -32,6 +32,10 @@ class AxiosRequest {
3232 ) ;
3333 }
3434
35+ /**
36+ * 成功请求的方法
37+ * @param config
38+ */
3539 private request ( config : { [ propsName : string ] : any } ) {
3640 // 配置请求头
3741 config . headers [ 'X-Origin' ] = 'admin-web' ;
@@ -49,18 +53,35 @@ class AxiosRequest {
4953 return config ;
5054 }
5155
56+ /**
57+ * 失败请求的方法
58+ * @param rejection
59+ */
5260 private requestError ( rejection : { data : any } ) {
5361 return this . useOrigin ( rejection )
5462 ? Promise . reject ( rejection )
5563 : Promise . reject ( rejection . data ) ;
5664 }
5765
58- private response ( res : any ) {
59- return this . isPlainRequest ( res . config . url ) || this . useOrigin ( res )
60- ? res
61- : res . data ;
66+ /**
67+ * 成功响应的方法
68+ * @param response
69+ */
70+ private response ( response : any ) {
71+ if ( response . status === 200 || response . status === 201 ) {
72+ return this . isPlainRequest ( response . config . url ) ||
73+ this . useOrigin ( response )
74+ ? Promise . resolve ( response )
75+ : Promise . resolve ( response . data ) ;
76+ } else {
77+ return Promise . reject ( response ) ;
78+ }
6279 }
6380
81+ /**
82+ * 响应失败的方法(根据自己的业务逻辑写)
83+ * @param error
84+ */
6485 private responseError ( error : any ) {
6586 if ( error . response && error . response . status ) {
6687 let $path ;
@@ -82,6 +103,7 @@ class AxiosRequest {
82103 // 未登录则跳转登录页面,并携带当前页面的路径
83104 // 在登录成功后返回当前页面,这一步需要在登录页操作。
84105 case 401 :
106+ console . log ( '你没有登录,请先登录' ) ;
85107 routers . replace ( {
86108 path : '/login' ,
87109 query : {
0 commit comments