ZZJ
2022-06-01 f3bf27d9bc7c8c6484be4f37edcc57df5490ecbe
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import axios from "axios"
import { Notification } from "element-ui"
import router from "@/router"
 
const Axios = axios.create({
  responseType: "json",
  withCredentials: true // 是否允许带cookie这些
})
 
/* //POST传参序列化(添加请求拦截器) */
Axios.interceptors.request.use(
  (config) => {
    // config.data = JSON.stringify(config.data)
    // let Public = {};
    // if (
    //   config.method === "post" ||
    //   config.method === "put" ||
    //   config.method === "delete"
    // ) {
    //   // 序列化
    //   // config.data = qs.stringify(config.data);
    //   // config.data = qs.stringify(Object.assign(Public, config.data));
    // } else {
    //   config.method = "get";
    //   config.params = Object.assign(Public, config.data);
    // }
    // config.headers = {
    //   "X-Requested-With": "XMLHttpRequest",
    //   Accept: "application/json",
    //   "Content-Type": "application/json; charset=UTF-8"
    // };
 
    // 若是有做鉴权token , 就给头部带上token
    let token = sessionStorage.getItem("loginedInfo") && JSON.parse(sessionStorage.getItem("loginedInfo")).access_token
    if (token != undefined) {
      config.headers.Authorization = token
    }
    return config
  },
  (error) => {
    /*  Message({
      showClose: true,
      message: "提交出错,请联系管理员!",
      type: "error"
    }); */
    return Promise.reject(error)
  }
)
 
/* //返回状态判断(添加响应拦截器) */
Axios.interceptors.response.use(
  res => {
    /* //对响应数据做些事 */
    if (res.data && res.statusText !== 'OK' && res.status !== 200) {
      Notification({
        title: '',
        message: res.data.message,
        type: 'error'
      })
      return Promise.reject(res.data)
    }
 
          var disposition = res.headers["content-disposition"];
          if(disposition) {
             var fileName = disposition.substring(disposition.indexOf("=") + 1);
             console.log(res);
             
             return {
               fileName:fileName,
               body:res
             }
          }
 
    return res.data ? res.data : {}
 
   
 
    /* //return res.data ? res.data : {}; */
  },
  error => {
    console.log('响应错误');
    console.log(error);
    let errJson: any = {
      success: false
    }
    if(error && error.response&&error.response.status == 404) {
      return
    }
    // 下面是接口回调的satus ,因为我做了一些错误页面,所以都会指向对应的报错页面
    if (error && error.response) {
      switch (error.response.status) {
        case 400:
          errJson.status = error.response.status
          errJson.message = '请求错误(400)'
          break
        case 401:
          errJson.status = error.response.status
          errJson.message = '未授权,请重新登录(401)'
          router.push({
             path: '/'
           })
  
          break
        case 403:
          errJson.status = error.response.status
          errJson.message = '拒绝访问(403)'
          break
        case 404:
          errJson.status = error.response.status
          errJson.message = '找不到请求路径(404)'
          break
        case 408:
          errJson.status = error.response.status
          errJson.message = '请求超时(408)'
          break
        case 500:
          errJson = error.response.data
          break
        
        default:
          errJson.status = error.response.status
          errJson.message = `连接出错(${error.response.status})!`
      }
 
    } else {
      errJson.message = '连接服务器失败!'
     
      // router.push({
      //   path: '/login'
      // });
    }
    
    if(error.response&&error.response.data&&!error.response.data.success ) {
      errJson.message =error.response.data.msg
    }
 
    if(error.response.status == 401) {
      return
    }
    
     Notification({
        title: '错误',
        showClose: true,
        message:errJson.message,
        type: 'error'
      })
    // 返回 response 里的错误信息
    return Promise.reject(errJson)
  }
)
export default Axios