|
|
|
@ -5,15 +5,22 @@ import { ElMessage } from 'element-plus'
|
|
|
|
|
// axios请求配置
|
|
|
|
|
const config = {
|
|
|
|
|
// baseURL:'http://localhost:8089',
|
|
|
|
|
baseURL:'/api',
|
|
|
|
|
baseURL: '/api',
|
|
|
|
|
|
|
|
|
|
timeout:10000,
|
|
|
|
|
timeout: 10000,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Http{
|
|
|
|
|
// 返回值数据类型
|
|
|
|
|
export interface Result<T = any> {
|
|
|
|
|
code: number;
|
|
|
|
|
msg: string;
|
|
|
|
|
data: T;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Http {
|
|
|
|
|
private instance: AxiosInstance;
|
|
|
|
|
// 初始化axios
|
|
|
|
|
constructor(config: AxiosRequestConfig){
|
|
|
|
|
constructor(config: AxiosRequestConfig) {
|
|
|
|
|
// 实例化,通过create方法创建axios实例
|
|
|
|
|
this.instance = axios.create(config)
|
|
|
|
|
|
|
|
|
@ -23,16 +30,16 @@ class Http{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 拦截器
|
|
|
|
|
private interceptors(){
|
|
|
|
|
private interceptors() {
|
|
|
|
|
// 请求发送之前的拦截器,通常用来配置、携带token
|
|
|
|
|
this.instance.interceptors.request.use((config: InternalAxiosRequestConfig)=>{
|
|
|
|
|
this.instance.interceptors.request.use((config: InternalAxiosRequestConfig) => {
|
|
|
|
|
// 设置token到请求头部
|
|
|
|
|
let token = "";
|
|
|
|
|
if(token){
|
|
|
|
|
if (token) {
|
|
|
|
|
config.headers!['token'] = token;
|
|
|
|
|
}
|
|
|
|
|
return config;
|
|
|
|
|
}, (error: any) =>{
|
|
|
|
|
}, (error: any) => {
|
|
|
|
|
error.data = {}
|
|
|
|
|
error.data.msg = '服务器异常,请联系管理员!'
|
|
|
|
|
return error;
|
|
|
|
@ -41,18 +48,18 @@ class Http{
|
|
|
|
|
// 请求返回之后的拦截器
|
|
|
|
|
this.instance.interceptors.response.use((res: AxiosResponse) => {
|
|
|
|
|
// 如果返回状态码不为200,说明返回错误
|
|
|
|
|
if(res.data.code != 200){
|
|
|
|
|
if (res.data.code != 200) {
|
|
|
|
|
// 如果msg不存在,返回“服务器出错!”
|
|
|
|
|
ElMessage.error(res.data.msg || '服务器出错!')
|
|
|
|
|
return Promise.reject(res.data.msg || '服务器出错!')
|
|
|
|
|
}else{
|
|
|
|
|
} else {
|
|
|
|
|
return res.data
|
|
|
|
|
}
|
|
|
|
|
},(error) => {
|
|
|
|
|
}, (error) => {
|
|
|
|
|
console.log('进入错误')
|
|
|
|
|
error.data = {}
|
|
|
|
|
if(error && error.response) {
|
|
|
|
|
switch(error.response.status){
|
|
|
|
|
if (error && error.response) {
|
|
|
|
|
switch (error.response.status) {
|
|
|
|
|
case 400:
|
|
|
|
|
error.data.msg = '错误请求'
|
|
|
|
|
ElMessage.error(error.data.msg)
|
|
|
|
@ -114,22 +121,22 @@ class Http{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get请求
|
|
|
|
|
get(url:string,params?:object): Promise<any>{
|
|
|
|
|
return this.instance.get(url,{params})
|
|
|
|
|
get<T = Result> (url: string, params?: object): Promise<T> {
|
|
|
|
|
return this.instance.get(url, { params })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// post请求
|
|
|
|
|
post(url:string,data?:object): Promise<any>{
|
|
|
|
|
return this.instance.post(url,data)
|
|
|
|
|
post<T = Result> (url: string, data?: object): Promise<T> {
|
|
|
|
|
return this.instance.post(url, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// put请求
|
|
|
|
|
put(url:string,data?:object): Promise<any>{
|
|
|
|
|
return this.instance.put(url,data)
|
|
|
|
|
put<T = Result> (url: string, data?: object): Promise<T> {
|
|
|
|
|
return this.instance.put(url, data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// delete请求
|
|
|
|
|
delete(url:string): Promise<any>{
|
|
|
|
|
delete<T = Result> (url: string): Promise<T> {
|
|
|
|
|
return this.instance.delete(url)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|