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
|
| /** axios封装
| * 请求拦截、相应拦截、错误统一处理
| */
| const httprequest = axios.create({
| baseURL: "http://121.196.36.24:8001/api/",
| timeout: 1000*10,
| // headers: {
| // token: "",
| // },
| });
|
| // 请求拦截器
| httprequest.interceptors.request.use(
| function (config) {
| return config;
| },
| function (error) {
| return Promise.reject(error);
| }
| );
|
| //响应拦截
| httprequest.interceptors.response.use(
| // 全局拦截 当code为某数值时 进行拦截
| function (response) {
| const res = response.data;
| //跳转到登录页
| if (res.code === 401) {
| window.location = `${window.logoutUrl}?url=${window.location.origin}`;
| }
| // 当code为300时进行一个提示
| if (res.code === '300') {
| vant.Notify({ type: 'danger', message: res.Message });
| // vant.Toast({ type: 'danger', message: res.Message , position: 'top',});
| }
|
| return response;
| },
| function (error) {
| return Promise.reject(error);
| }
| );
|
| function get(url, params) {
| return new Promise((resolve, reject) => {
| httprequest
| .get(url, { params: params })
| .then((res) => {
| resolve(res.data);
| })
| .catch((err) => {
| reject(err.data);
| });
| });
| }
| // qs.stringify(data)
| function post(url, data) {
| return new Promise((resolve, reject) => {
| httprequest
| .post(url, data)
| .then((res) => {
| resolve(res.data);
| })
| .catch((err) => {
| reject(err.data);
| });
| });
| }
|
|
|