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
import axios from 'axios'
import qs from 'qs'
// axios.defaults.baseURL = 'http://192.168.1.101:8300' // 本地荣
// axios.defaults.baseURL = 'http://192.168.1.102:8300' // 本地钱
axios.defaults.baseURL = 'http://139.196.213.18:8300' // beta环境
let loadingInstance // 创建Loading 的实例
axios.defaults.headers['Authorization'] = `Bearer ${localStorage.getItem('token')}` // 设置请求头为 Authorization
// 配置发送请求前的拦截器可以设置token信息
axios.interceptors.request.use((config) => {
// loading开始loadingInstance.start()
return config
}, (error) => {
// 出错,也要loading结束
loadingInstance.close()
return Promise.reject(error)
})
const $http = {
post: (url) => {
return new Promise((resolve, reject) => {
axios.post(url).then((res) => {
return resolve(res)
}).catch((error) => {
return reject(error)
})
})
},
get: (url) => {
return new Promise((resolve, reject) => {
axios.get(url).then((res) => {
return resolve(res)
}).catch((error) => {
return reject(error)
})
})
}
}
export { $http }
// // 配置响应拦截器
// axios.interceptors.response.use(res => {
// //loading结束
// loadingInstance.close();
// //这里面写所需要的代码
// if(res.data.code =='401'){
// //全局登陆过滤,当判读token失效或者没有登录时 返回登陆页面
// return false;
// };
// return Promise.resolve(res);
// }, error => {
// loadingInstance.close();
// return Promise.reject(error);
// }
// return Promise.reject(error);
// };
// return axios;
// )