first commit

This commit is contained in:
编码猿
2024-09-27 02:06:13 +08:00
commit 852d94fbb9
36760 changed files with 3274413 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
import axios from 'axios'
import config from '@config'
class Http {
static instance;
// 单例模式
static getInstance() {
if (this.instance instanceof this) {
return this.instance;
} else {
this.instance = new this;
return this.instance;
}
}
constructor() {
this.SetBaseUrl();
this.interceptors();
}
async get (url, params = {}, header = {}) {
return await axios.get(url, {
params: params,
headers: Object.assign(
header,
{
token: localStorage.getItem("token")
}
)
})
}
async post (url, params = {}, header = {}) {
return await axios.post(url, params, {
headers: Object.assign(
header,
{
token: localStorage.getItem("token")
}
)
})
}
interceptors() {
/**
* 请求拦截器
*/
axios.interceptors.request.use(function (config) {
return config;
}, function (error) {
// 对请求错误做些什么
return Promise.reject(error);
});
/**
* 响应拦截器
* 在这里处理返回数据的状态码
*/
axios.interceptors.response.use(async (response) => {
// 对响应数据做点什么
return response;
}, function (error) {
// 对响应错误做点什么
return Promise.reject(error);
});
}
/**
* 设置请求基地址
*/
SetBaseUrl() {
axios.defaults.baseURL = config.check();
}
}
export default Http