factory_web/src/utils/request.js

91 lines
1.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import axios from 'axios';
import { ElNotification } from 'element-plus';
axios.defaults.baseURL = ''
axios.defaults.timeout = 10000
// HTTP request 拦截器
axios.interceptors.request.use(
(config) => {
config.headers['Authorization'] = "SCUI-Demo-Auth"
return config;
},
(error) => {
return Promise.reject(error);
}
);
// HTTP response 拦截器
axios.interceptors.response.use(
(response) => {
return response;
},
(error) => {
if (error.response) {
if (error.response.status == 404) {
ElNotification.error({
title: '请求错误',
message: "Status:404正在请求不存在的服务器记录"
});
} else if (error.response.status == 500) {
ElNotification.error({
title: '请求错误',
message: "Status:500服务器发生错误"
});
} else {
ElNotification.error({
title: '请求错误',
message: `Status:${error.response.status},未知错误!`
});
}
} else {
ElNotification.error({
title: '请求错误',
message: "请求服务器无响应!"
});
}
return Promise.reject(error.response);
}
);
var http = {
/** get 请求
* @param {接口地址} url
* @param {请求参数} params
*/
get: function(url, params) {
return new Promise((resolve, reject) => {
axios.get(url, {
params: params
})
.then((response) => {
resolve(response.data);
})
.catch((error) => {
reject(error);
});
})
},
/** post 请求
* @param {接口地址} url
* @param {请求参数} params
*/
post: function(url, params) {
return new Promise((resolve, reject) => {
axios.post(url, params)
.then((response) => {
resolve(response.data);
})
.catch((error) => {
reject(error);
});
})
}
}
export default http;