首先引入axios
import axios from 'axios'
创建各种请求器
const axiosServer = axios.create({
baseURL: window.ApiBaseURL,
headers: {
'Content-Type': 'application/json-patch+json'
},
withCredentials: true,
})
const axiosServer1 = axios.create({
baseURL: window.ApiBaseURL,
headers: {
'Content-Type': 'application/json-patch+json'
},
responseType: "blob",
withCredentials: true,
})
const axiosServerFile = axios.create({
baseURL: window.ApiFileURL,
headers: {
'Content-Type': 'multipart/form-data'
},
withCredentials: true,
})
const axiosServer3 = axios.create({
baseURL: window.ApiBaseURL,
headers: {
'Content-Type': 'application/json; application/octet-stream'
},
responseType: "blob",
withCredentials: true,
})
设置一下请求拦截器
axiosServer1.interceptors.request.use(
function(config) {
config.headers.Token = store.state.token
return config
},
function(error) {
return Promise.reject(error)
}
)
axiosServer.interceptors.request.use(
function(config) {
config.headers.Token = store.state.token
return config
},
function(error) {
return Promise.reject(error)
}
)
axiosServer3.interceptors.request.use(
function(config) {
config.headers.Token = store.state.token
return config
},
function(error) {
return Promise.reject(error)
}
)
响应拦截器
axiosServer.interceptors.response.use(
response => {
if (response.data.data && response.data.data.code == 401) {
window.opener = null
console.log(response.data.data.msg)
return setTimeout(() => {
window.location.href = 'login.html'
}, 2000)
}
if (
response.data.type ==
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
) {
return response
} else if (!response.data.ok) {
return console.log(response.data.message)
}
return response
},
error => {
if (error.response.status === 401) {
} else if (error.response.status === 500) {
return Promise.reject(error.response.data)
} else if (error.response.status === 403) {
console.log('没有权限')
}
return Promise.reject(error.response.data)
}
)
封装的请求代码
export function get(url, params) {
return new Promise((resolve, reject) => {
axiosServer
.get(url, {
params: params
})
.then(res => {
resolve(res.data)
})
.catch(err => {
reject(err.data)
})
})
}
export function UploadPost(url, params) {
return new Promise((resolve, reject) => {
axiosServerFile({
method: 'post',
url: url,
data: params,
headers: {
'Content-Type': 'multipart/form-data'
}
})
.then(res => {
resolve(res.data)
})
.catch(err => {
reject(err.data)
})
})
}
export function downPost(url, params) {
return new Promise((resolve, reject) => {
axiosServerFile({
method: 'post',
url: url,
data: params,
responseType: 'blob'
})
.then(res => {
resolve(res.data)
})
.catch(err => {
reject(err.data)
})
})
}
export function exportGet3(url, params) {
return new Promise((resolve, reject) => {
axiosServer3({
method: 'get',
url: url,
params: params,
responseType: 'blob'
})
.then(res => {
resolve(res.data)
})
.catch(err => {
reject(err.data)
})
})
}
export function post(url, params) {
return new Promise((resolve, reject) => {
axiosServer
.post(url, params)
.then(res => {
resolve(res.data)
})
.catch(err => {
reject(err.data)
})
})
}
export function exportPost(url, params) {
return new Promise((resolve, reject) => {
axiosServer({
method: 'post',
url: url,
data: params,
responseType: 'blob'
})
.then(res => {
resolve(res.data)
})
.catch(err => {
reject(err.data)
})
})
}
export function exportGet(url, params) {
return new Promise((resolve, reject) => {
axiosServer({
method: 'get',
url: url,
params: params,
responseType: 'blob'
})
.then(res => {
resolve(res.data)
})
.catch(err => {
reject(err.data)
})
})
}
export function exportGet1(url, params) {
return new Promise((resolve, reject) => {
axiosServer1
.get(url, {
params: params,
})
.then(res => {
resolve(res.data)
})
.catch(err => {
reject(err.data)
})
})
}
export function exportGetFile(url, params) {
return new Promise((resolve, reject) => {
axiosServerFile({
method: 'get',
url: url,
params: params
})
.then(res => {
resolve(res.data)
})
.catch(err => {
reject(err.data)
})
})
}
export function exportGetFileBlob(url, params) {
return new Promise((resolve, reject) => {
axiosServerFile({
method: 'get',
url: url,
params: params,
responseType: 'blob'
})
.then(res => {
resolve(res.data)
})
.catch(err => {
reject(err.data)
})
})
}
export function delete2(url, params) {
return new Promise((resolve, reject) => {
axiosServer
.delete(url, params)
.then(res => {
resolve(res.data)
})
.catch(err => {
reject(err.data)
})
})
}
export function Delete(url, params) {
return new Promise((resolve, reject) => {
axiosServerFile
.delete(url, params)
.then(res => {
resolve(res.data)
})
.catch(err => {
reject(err)
})
})
}
以上封装方法还可以进一步优化,欢迎各位补充
下面是几个jquery的封装
function encodeQuery(obj) {
var arr = [];
if (obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
var value = obj[key];
if (value != null)
arr.push(encodeURIComponent(key) + "=" + encodeURIComponent(value));
}
}
}
return arr.join("&");
}
$.ajaxSetup({
headers: {
'Token': getToken(),
},
xhrFields: { responseType: "blob" },
});
function get(url) {
return new Promise(function(resolve, reject) {
$.get(ApiBaseURL+url, function(data) {
resolve(data);
}).fail(function(error) {
reject(error);
});
});
}
function post(url, data) {
return new Promise(function(resolve, reject) {
$.post(ApiBaseURL+url, data, function(response) {
resolve(response);
}).fail(function(error) {
reject(error);
});
});
}
var type1={
code1:params=>{
return get("api/code1/code1?"+encodeQuery(params))
},
code2:params=>{
return get("api/code2/code2?"+encodeQuery(params))
},
}
type1.code1({
pararms1:'123',
pararms2:'456'
}).then((res)=>{
console.log(res)
})