xuruiqian
2025-02-25 e85ab165147c37b571ea67ce6f6ae9ae55a96070
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import axios from 'axios';
import * as elementplus from 'element-plus'
import {useBasicInfoStore} from "@/store/basicInfo";
 
import i18n from "../../public/assets/i18n/i18n";
import router from "../router/index";
 
axios.interceptors.request.use(
    config => {
        config.headers.Authorization = localStorage.getItem('Authorization');
        return config;
    },
    error => {
        console.error(error);
        elementplus.ElNotification({
            title: 'Error',
            message: error.message,
            position: 'top-right',
            duration: 5000,
            type: 'error'
        });
        return Promise.reject(error);
    }
);
 
axios.interceptors.response.use(
    success => {
        //console.debug(success);
        return success;
 
        if (success.data.code == null) {
            elementplus.ElNotification({
                title: 'Warning',
                message: `${i18n.t('message.UnknownSystemMessage')}`,
                position: 'top-right',
                duration: 5000,
                type: 'warning'
            });
        } else if (success.data.code && success.data.code == 200) {
            elementplus.ElNotification({
                title: 'Success',
                message: success.data.message,
                position: 'top-right',
                duration: 5000,
                type: 'success'
            });
        } else if (success.data.code == 500) {
            elementplus.ElNotification({
                title: 'Error',
                message: success.data.message,
                position: 'top-right',
                duration: 5000,
                type: 'error'
            });
        } else if (success.data.code == 401 || success.data.code == 403) {
            elementplus.ElNotification({
                title: 'Error',
                message: success.data.message,
                position: 'top-right',
                duration: 5000,
                type: 'error'
            });
        }
        return success;
    },
    error => {
        if (error.response == undefined) {
            elementplus.ElNotification({
                title: 'Error',
                message: error.message,
                position: 'top-right',
                duration: 5000,
                type: 'error'
            });
        } else if (error.response.status == 504 || error.response.status == 404) {
            elementplus.ElNotification({
                title: 'Error',
                message: `${i18n.t('message.NetworkError')}`,
                position: 'top-right',
                duration: 5000,
                type: 'error'
            });
        } else if (error.response.status == 500) {
            elementplus.ElNotification({
                title: 'Error',
                message: error.response.data.msg,
                position: 'top-right',
                duration: 5000,
                type: 'error'
            });
        } else if (error.response.status == 403) {
            elementplus.ElNotification({
                title: 'Error',
                message: `${i18n.t('message.AccessIsDenied')}`,
                position: 'top-right',
                duration: 5000,
                type: 'error'
            });
        } else if (error.response.status == 401) {
            router.replace('/admin/login');
            const store = useBasicInfoStore();
 
            store.removeSessionData();
        } else {
            elementplus.ElNotification({
                title: 'Error',
                message: error,
                position: 'top-right',
                duration: 5000,
                type: 'error'
            });
        }
 
        return error;
    }
);
 
export const postKeyValueRequest = (url, params) => {
    return axios({
        method: 'post',
        url: `${url}`,
        data: params,
        transformRequest: [function (data) {
            let ret = '';
            for (let i in data) {
                ret += encodeURIComponent(i) + '=' + encodeURIComponent(data[i]) + '&'
            }
            return ret;
        }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
        }
    });
};
export const postFileRequest = (url, formdata) => {
    return axios({
        method: 'post',
        url: `${url}`,
        data: formdata,
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    })
};
export const postRequest = (url, params) => {
    return axios({
        method: 'post',
        url: `${url}`,
        data: JSON.stringify(params),
        headers: {
            'Content-Type': 'application/json;charset=UTF-8'
        }
    })
};
export const postExportRequest = (url, params) => {
    return axios({
        method: 'post',
        url: `${url}`,
        data: JSON.stringify(params),
        headers: {
            'Content-Type': 'application/json;charset=UTF-8'
        },
        responseType: 'blob',
    })
};
export const putRequest = (url, params) => {
    return axios({
        method: 'put',
        url: `${url}`,
        data: params
    })
};
export const getRequest = (url, params) => {
    return axios({
        method: 'get',
        url: `${url}`,
        params: params,
    })
};
export const deleteRequest = (url, params) => {
    return axios({
        method: 'delete',
        url: `${url}`,
        params: params
    })
};