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
import axios from "axios";
import { Toast } from "vant";
let loading = null;
const service = axios.create({
baseURL: "/shop-mall",
timeout: 5000
});
service.interceptors.request.use(
config => {
const token = localStorage.getItem("token");
config.headers["token"] = token;
if (!config.loading) {
loading = Toast.loading({
forbidClick: true,
message: "加载中..."
});
}
return config;
},
error => {
if (loading) Toast.clear();
this.$toast.fail(error.response.data.error);
}
);
service.interceptors.response.use(
response => {
if (loading) Toast.clear();
const res = response.data;
return res;
},
error => {
if (loading) Toast.clear();
return Promise.reject(error);
}
);
export default service;