axios封装
2020-06-30 本文已影响0人
Roct
Js
var axiosAyd = axios.create({
baseURL: baseURL,
timeout: 60000,
headers: {
'content-type': "application/x-www-form-urlencoded;application/json;charset=UTF-8",
},
transformResponse: [function (data) {
return data;
}]
});
axiosAyd.interceptors.request.use(config => {
if (Cookies.get("access_token") && Cookies.get("access_token") != undefined) {
config.headers.common['Access-Token'] = Cookies.get("access_token") ;
config.headers.common['App-Type'] = 'H5';
}
return config
}, err => {
return Promise.reject(err);
})
class HTTP {
request({
url,
data = {},
method = 'post'
}) {
return new Promise((resolve, reject) => {
this._request(url, resolve, reject, data, method)
})
}
_request(url, resolve, reject, data = {}, method = 'post') {
axiosAyd({
method: method,
url: url,
params: data
}).then((res) => {
if (res.status === 200) {
if (res.data.status === -103) { // token过期 重新登录
reject("token is unavailable")
} else {
resolve(res.data)
}
} else {
reject(res.data.msg)
}
}, error => {
reject(error)
})
}
TS
import axios from 'axios';
import state from '@/store/state';
// import API from "./API.ts";
// import tools from '@/utils/tools';
interface GetParamType {
url: string;
params?: Record<string, any>;
}
type RESOLVE = (res: any) => void;
type REJECT = (res: string) => void;
const baseURL = process.env.VUE_APP_URL;
const request = axios.create({
baseURL: baseURL,
timeout: 60000,
headers: {
'content-type': 'application/x-www-form-urlencoded;application/json;charset=UTF-8'
},
transformResponse: [
function(data: any) {
// console.log("data:", data)
data = JSON.parse(data);
return data;
}
],
transformRequest: [
(data: any): string => {
return JSON.stringify(data);
}
]
});
request.interceptors.request.use(
(config: any) => {
config.headers.common['Authorization'] = state.token;
return config;
},
err => {
return Promise.reject(err);
}
);
class HTTP {
get(param: GetParamType) {
return new Promise((resolve: RESOLVE, reject: REJECT) => {
this._request(
{
url: param.url,
params: param.params,
method: 'get'
},
resolve,
reject
);
});
}
post(param: GetParamType) {
return new Promise((resolve: RESOLVE, reject: REJECT) => {
this._request(
{
url: param.url,
data: param.params,
method: 'post'
},
resolve,
reject
);
});
}
_request(config: any, resolve: RESOLVE, reject: REJECT) {
request(config)
.then(res => {
console.log(';;;url:' + config.url + 'res::', res);
if (res.status === 200) {
if (res.data.code === 200) {
resolve(res.data.data);
} else {
reject(res.data.msg);
}
} else {
reject(res.data.msg);
}
})
.catch((error: Error) => {
reject(error.message);
});
}
//
// // 上传
// uploadImagefile(url: string, param: Object, imgUrl: string) {
// var formdata = new FormData();
// let file = tools.dataURLtoFile(imgUrl)
// formdata.append('img', file, file.name);
// let config = {
// headers: {
// 'Content-Type': 'multipart/form-data'
// }
// };
// return this._request({
// url: url,
// data: formdata,
// params: param,
// config: config
// })
// }
}
const Http = new HTTP();
export default Http;