马宇豪
2024-08-01 81e6577d11920a45284936f591bd8d076006a919
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
import axios from "axios";//原生的axios
import Cookies from 'js-cookie';
import { Session } from '@/util/storage';
import {message} from "ant-design-vue";
import { loginOut } from "@/api/login";
import JSONBig from 'json-bigint';
//用来拦截用的
axios.defaults.headers.post["Content-Type"] = "application/json;charset=utf-8";
//创建一个单例
 
const { baseUrl } = require('../../config/env.' + process.env.NODE_ENV)
const http= axios.create({
    baseURL: baseUrl,
    timeout: 50000,//响应时间
    transformResponse: [
        function (data) {
            // 对 data 进行任意转换处理
            try {
                return JSONBig.parse(data)
            } catch (err) {
                return data
            }
        }
    ]
    // headers:{"Content-Type":"application/json;charset=utf-8"},
})
 
//拦截器  -请求拦截
http.interceptors.request.use(config=>{
    // 部分接口需要token
    let token = Cookies.get('resTk')
    let uid = Cookies.get('resUid')
    if(token){
        config.headers.tk = token;
        config.headers.uid = uid
        // config.headers ={
        // 'token':token
        // }
    }
    return config;
},err=>{
    return Promise.reject(err)
})
 
//拦截器  -响应拦截
http.interceptors.response.use(
    (response) => {
        // 对响应数据做点什么
        if (response.data.code && response.data.code === 401) {
            // message.error('用户不存在')
            setTimeout(()=>{
                loginOut()
                    .then(() => {
                        Session.clear();
                        window.location.href = '/';
                    });
            },2000)
        } else if (response.data.code && response.data.code === 405) {
            message.error('token失效')
            setTimeout(()=>{
            loginOut()
                .then(() => {
                    Session.clear();
                    window.location.href = '/';
                });
            },2000)
        }
        return Promise.resolve(response);
    },
    (error) => {
        // 对响应错误做点什么
        if (error.message.indexOf('timeout') != -1) {
            message.error('网络超时')
            // setTimeout(() => {
            //     Session.clear()
            //     window.location.href = '/'
            // }, 1000)
        } else if (error.message == 'Network Error') {
            message.error('网络连接错误')
            // setTimeout(() => {
            //     Session.clear()
            //     window.location.href = '/'
            // }, 1000)
        } else {
            if (error.response.data) message.error(error.response.data.error);
            else message.error('接口路径找不到');
        }
        return Promise.reject(error);
    }
);
 
//整体导出
export default http;