Axios封装
/**
* 封装get方法
* @param url
* @param params
* @returns {Promise}
*/
export function fetch(url,params={}){
return new Promise((resolve,reject) => {
axios.get(url,{
params:params
})
.then(response => {
if(response.data.code === 200){
resolve(response.data.data);
}else if(response.data.code === 501){
router.push({
name:'login'//从哪个页面跳转
})
}else if(response.data.code === 503){
// Vue.prototype.$message.error('请勿频繁操作')
}else{
Vue.prototype.$message.error(response.data.msg)
reject()
}
})
.catch(err => {
reject(err);
let message = '请求失败!请检查网络';
if(err.response)message=err.response.data.message;
Vue.prototype.$msgbox({
title:'错误!',
message:message,
type:'error',
})
})
})
}
/**
* 封装post请求
* @param url
* @param data
* @returns {Promise}
*/
export function post(url,data = {}){
return new Promise((resolve,reject) => {
axios.post(url,data)
.then(response => {
if(response.data.code === 200){
resolve(response.data.data);
}else if(response.data.code === 501){
router.push({
name:'login'//从哪个页面跳转
})
}else if(response.data.code === 503){
// Vue.prototype.$message.error('请勿平凡操作')
}else{
Vue.prototype.$message.error(response.data.msg)
}
},err => {
reject(err);
let message = '请求失败!请检查网络';
if(err.response)message=err.response.data.message;
Vue.prototype.$msgbox({
title:'错误!',
message:message,
type:'error',
})
})
})
}
/**
* 封装patch请求
* @param url
* @param data
* @returns {Promise}
*/
export function patch(url,data = {}){
return new Promise((resolve,reject) => {
axios.patch(url,data)
.then(response => {
if(response.data.code === 200){
resolve(response.data.data);
}else if(response.data.code === 501){
router.push({
name:'login'//从哪个页面跳转
})
}else if(response.data.code === 503){
// Vue.prototype.$message.error('请勿平凡操作')
}else{
Vue.prototype.$message.error(response.data.msg)
}
},err => {
reject(err);
let message = '请求失败!请检查网络';
if(err.response)message=err.response.data.message;
Vue.prototype.$msgbox({
title:'错误!',
message:message,
type:'error',
})
})
})
}
/**
* 封装put请求
* @param url
* @param data
* @returns {Promise}
*/
export function put(url,data = {}){
return new Promise((resolve,reject) => {
axios.put(url,data)
.then(response => {
if(response.data.code === 200){
resolve(response.data.data);
}else if(response.data.code === 501){
router.push({
name:'login'//从哪个页面跳转
})
}else if(response.data.code === 503){
// Vue.prototype.$message.error('请勿平凡操作')
}else{
Vue.prototype.$message.error(response.data.msg)
}
},err => {
reject(err);
let message = '请求失败!请检查网络';
if(err.response)message=err.response.data.message;
Vue.prototype.$msgbox({
title:'错误!',
message:message,
type:'error',
})
})
})
}
export function del(url,data = {}){
return new Promise((resolve,reject) => {
axios.delete(url,data)
.then(response => {
if(response.data.code === 200){
resolve(response.data.data);
}else if(response.data.code === 501){
router.push({
name:'login'//从哪个页面跳转
})
}else if(response.data.code === 503){
// Vue.prototype.$message.error('请勿平凡操作')
}else{
Vue.prototype.$message.error(response.data.msg)
}
},err => {
reject(err);
let message = '请求失败!请检查网络';
if(err.response)message=err.response.data.message;
Vue.prototype.$msgbox({
title:'错误!',
message:message,
type:'error',
})
})
})
}