zhouwx
2025-06-16 7c8c06b5397d22151b02407b1f4f19d771bb15dd
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
import axios from 'axios';
import { ElMessage, ElMessageBox } from 'element-plus';
import JSONbig from 'json-bigint';
import Cookies from 'js-cookie';
import { useLoginApi } from '/@/api/login';
import { Session } from '/@/utils/storage';
 
// var jsonBig = require('json-bigint')({ "storeAsString": true });
// 配置新建一个 axios 实例
const service = axios.create({
    // baseURL: import.meta.env.VITE_API_URL as any,
    timeout: 50000,
    headers: { 'Content-Type': 'application/json' }
});
 
service.interceptors.request.use(
    (config) => {
        // for (let key in config.data) {
        //     if (config.data[key] == '' && config.data[key] !== 0) {
        //         config.data[key] = null;
        //     }
        // }
        if (Cookies.get('token')) {
            (<any>config.headers).common['tk'] = `${Cookies.get('token')}`;
            (<any>config.headers).common['uid'] = `${Cookies.get('uid')}`;
        }
        return config;
    },
    (error) => {
        // 对请求错误做些什么
        return Promise.reject(error);
    }
);
 
service.defaults.transformResponse = [
    function (data) {
    try {
            // 正常同通过return JSON.parse(data)对数据进行转换
            // 但是转化完成的数据可能超出安全数据长度
            // 因此我们定制使用第三包来转化
            // if(data.indexOf('http://') === -1){
            //     return data
            // }else{
        // if(typeof JSON.parse(data) === 'object'){
        //     return JSONbig.parse(data);
        // };
            return JSONbig.parse(data);
        } catch (err) {
            // 转换失败返回一个空对象
        return data
        }
    }
];
 
service.interceptors.response.use(
    (response) => {
        // 对响应数据做点什么
        if (response.data.code && response.data.code === 'A0213') {
            ElMessage.error('用户uid不存在');
            useLoginApi()
                .signOut()
                .then(() => {
                    Session.clear();
                    window.location.href = '/';
                });
        } else if (response.data.code && response.data.code === 405) {
            ElMessage.error('token失效');
            // logOut;
            useLoginApi()
                .signOut()
                .then(() => {
                    Session.clear();
                    window.location.href = '/';
                });
        }
        // if(response.data.code && response.data.code !== '200'){
        return Promise.resolve(response);
        // }
        // Session.clear()
        // window.location.href = '/'
        // return Promise.reject(response)
        // const res = response.data;
        // if (res.code && res.code !== 0) {
        //     // `token` 过期或者账号已在别处登录
        //     if (res.code === 401 || res.code === 4001) {
        //         Session.clear(); // 清除浏览器全部临时缓存
        //         window.location.href = '/'; // 去登录页
        //         ElMessageBox.alert('你已被登出,请重新登录', '提示', {})
        //             .then(() => {})
        //             .catch(() => {});
        // }
        //     return Promise.reject(service.interceptors.response);
        // } else {
        //     return response.data;
        // }
    },
    (error) => {
        // 对响应错误做点什么
        if (error.message.indexOf('timeout') != -1) {
            ElMessage.error('网络超时');
            setTimeout(() => {
                Session.clear();
                window.location.href = '/';
            }, 1000);
        } else if (error.message == 'Network Error') {
            ElMessage.error('网络连接错误');
            setTimeout(() => {
                Session.clear();
                window.location.href = '/';
            }, 1000);
        } else {
            if (error.response.data) ElMessage.error(error.response.data.error);
            else ElMessage.error('接口路径找不到');
        }
        return Promise.reject(error);
    }
);
 
export default service;