init
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
|
||||
import axios from 'axios';
|
||||
import {ElMessage} from "element-plus";
|
||||
const basic_url = "http://localhost:8081";
|
||||
|
||||
const axiosInstance = axios.create({
|
||||
baseURL: basic_url,
|
||||
timeout: 5000
|
||||
});
|
||||
|
||||
// 添加错误处理程序
|
||||
const handleError = (error: any) => {
|
||||
console.log(error)
|
||||
if (error.response) {
|
||||
ElMessage.error("请求失败,服务器返回:",error.response.data);
|
||||
} else if (error.request) {
|
||||
ElMessage.error("请求未收到响应,请检查接口运行状态");
|
||||
} else {
|
||||
ElMessage.error("请求配置错误", error.message);
|
||||
}
|
||||
throw new Error("网络不通畅,请检查您的网络连接或服务器状态");
|
||||
};
|
||||
|
||||
const get = (url: string,data:{}) => {
|
||||
console.log("开始请求",basic_url+url,"参数:",data)
|
||||
return axiosInstance.post(url, data,{
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
.then((res) => {
|
||||
const result = res.data;
|
||||
if (result.code !== 200) {
|
||||
ElMessage.error(result.message || '请求失败');
|
||||
return Promise.reject(result);
|
||||
}
|
||||
return result;
|
||||
})
|
||||
.catch(handleError);
|
||||
}
|
||||
|
||||
const post = (url: string, data:any)=>{
|
||||
console.log("开始请求",basic_url+url,"参数:",data)
|
||||
return axiosInstance.post(url,data).then((res) => res.data).catch(handleError);
|
||||
}
|
||||
|
||||
const put = (url: string, data:any)=>{
|
||||
console.log("开始请求",basic_url+url,"参数:",data)
|
||||
return axiosInstance.put(url,data).then((res) => res.data).catch(handleError);
|
||||
}
|
||||
|
||||
export default {get,post,put}
|
||||
Reference in New Issue
Block a user