feat 完善封装的请求函数
This commit is contained in:
+55
-32
@@ -1,6 +1,6 @@
|
|||||||
|
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import {ElMessage} from "element-plus";
|
import { ElMessage } from "element-plus";
|
||||||
|
|
||||||
const basic_url = "http://localhost:8081";
|
const basic_url = "http://localhost:8081";
|
||||||
|
|
||||||
const axiosInstance = axios.create({
|
const axiosInstance = axios.create({
|
||||||
@@ -8,45 +8,68 @@ const axiosInstance = axios.create({
|
|||||||
timeout: 5000
|
timeout: 5000
|
||||||
});
|
});
|
||||||
|
|
||||||
// 添加错误处理程序
|
|
||||||
const handleError = (error: any) => {
|
const handleError = (error: any) => {
|
||||||
console.log(error)
|
console.log(error);
|
||||||
if (error.response) {
|
if (error.response) {
|
||||||
ElMessage.error("请求失败,服务器返回:",error.response.data);
|
ElMessage.error(`请求失败,服务器返回: ${error.response.data}`);
|
||||||
} else if (error.request) {
|
} else if (error.request) {
|
||||||
ElMessage.error("请求未收到响应,请检查接口运行状态");
|
ElMessage.error("请求未收到响应,请检查接口运行状态");
|
||||||
} else {
|
} else {
|
||||||
ElMessage.error("请求配置错误", error.message);
|
ElMessage.error(`请求配置错误: ${error.message}`);
|
||||||
}
|
}
|
||||||
throw new Error("网络不通畅,请检查您的网络连接或服务器状态");
|
throw new Error("网络不通畅,请检查您的网络连接或服务器状态");
|
||||||
};
|
};
|
||||||
|
|
||||||
const get = (url: string,data:{}) => {
|
const validateResponse = (res: any) => {
|
||||||
console.log("开始请求",basic_url+url,"参数:",data)
|
const result = res.data;
|
||||||
return axiosInstance.post(url, data,{
|
if (result.code !== 200) {
|
||||||
headers: {
|
ElMessage.error(result.message || '请求失败');
|
||||||
'Content-Type': 'application/json'
|
return Promise.reject(result);
|
||||||
}
|
}
|
||||||
})
|
return result;
|
||||||
.then((res) => {
|
};
|
||||||
const result = res.data;
|
|
||||||
if (result.code !== 200) {
|
const get = (url: string, params: {}) => {
|
||||||
ElMessage.error(result.message || '请求失败');
|
console.log("开始GET请求", basic_url + url, "参数:", params);
|
||||||
return Promise.reject(result);
|
return axiosInstance.get(url, { params })
|
||||||
}
|
.then(validateResponse)
|
||||||
return result;
|
|
||||||
})
|
|
||||||
.catch(handleError);
|
.catch(handleError);
|
||||||
}
|
};
|
||||||
|
|
||||||
const post = (url: string, data:any)=>{
|
const post = (url: string, data: any) => {
|
||||||
console.log("开始请求",basic_url+url,"参数:",data)
|
console.log("开始POST请求", basic_url + url, "参数:", data);
|
||||||
return axiosInstance.post(url,data).then((res) => res.data).catch(handleError);
|
return axiosInstance.post(url, data)
|
||||||
}
|
.then(validateResponse)
|
||||||
|
.catch(handleError);
|
||||||
|
};
|
||||||
|
|
||||||
const put = (url: string, data:any)=>{
|
const put = (url: string, data: any) => {
|
||||||
console.log("开始请求",basic_url+url,"参数:",data)
|
console.log("开始PUT请求", basic_url + url, "参数:", data);
|
||||||
return axiosInstance.put(url,data).then((res) => res.data).catch(handleError);
|
return axiosInstance.put(url, data)
|
||||||
}
|
.then(validateResponse)
|
||||||
|
.catch(handleError);
|
||||||
|
};
|
||||||
|
|
||||||
export default {get,post,put}
|
const del = (url: string, params: {}) => {
|
||||||
|
console.log("开始DELETE请求", basic_url + url, "参数:", params);
|
||||||
|
return axiosInstance.delete(url, { params })
|
||||||
|
.then(validateResponse)
|
||||||
|
.catch(handleError);
|
||||||
|
};
|
||||||
|
|
||||||
|
const requestNotImplemented = (method: string) => {
|
||||||
|
ElMessage.error(`请求方法 ${method} 未实现`);
|
||||||
|
throw new Error(`请求方法 ${method} 未实现`);
|
||||||
|
};
|
||||||
|
|
||||||
|
const request = (method: string, url: string, paramsOrData: any) => {
|
||||||
|
switch (method.toLowerCase()) {
|
||||||
|
case 'get': return get(url, paramsOrData);
|
||||||
|
case 'post': return post(url, paramsOrData);
|
||||||
|
case 'put': return put(url, paramsOrData);
|
||||||
|
case 'delete': return del(url, paramsOrData);
|
||||||
|
default: return requestNotImplemented(method);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default { get, post, put, del, request };
|
||||||
|
|||||||
Reference in New Issue
Block a user