You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
163 lines
4.6 KiB
163 lines
4.6 KiB
import axios from 'axios'; |
|
import QS from 'qs'; |
|
// import store from '../store/index' |
|
import { Message } from 'element-ui' |
|
import router from '@/router/index' |
|
import Setting from '@/setting' |
|
|
|
const service = axios.create({ |
|
baseURL: Setting.apiBaseURL, |
|
timeout: 5000 |
|
}) |
|
|
|
// post请求头 |
|
service.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'; |
|
|
|
// 请求拦截器 |
|
// service.interceptors.request.use(config => { |
|
// if (sessionStorage.getItem('token')) { |
|
// // 存在将token写入 request header |
|
// config.headers.Authorization = `${sessionStorage.getItem('token')}`; |
|
// } |
|
// return config; |
|
// }, err => { |
|
// Message.error({ |
|
// message: '退出登陆', |
|
// onClose: function () { |
|
// router.push({name: 'login'}); |
|
// } |
|
// }) |
|
// return Promise.reject(err); |
|
// }) |
|
|
|
// 响应拦截器 |
|
service.interceptors.response.use( |
|
response => { |
|
const res = response.data |
|
if (res.success) { |
|
return Promise.resolve(res).catch(e => {}) |
|
} else { |
|
switch (res.code) { |
|
case 201: |
|
Message.error(res.message) |
|
break; |
|
default: |
|
Promise.reject(res).catch(e => {}) |
|
} |
|
return Promise.reject(res).catch(e => {}) |
|
} |
|
}, |
|
// 服务器状态码不是200的情况 |
|
error => { |
|
if (error.res.status) { |
|
switch (error.res.status) { |
|
// 401: 未登录 |
|
// 未登录则跳转登录页面,并携带当前页面的路径 |
|
// 在登录成功后返回当前页面,这一步需要在登录页操作。 |
|
case 500: |
|
router.replace({ |
|
path: '/login', |
|
// query: { redirect: router.currentRoute.fullPath } |
|
}); |
|
break; |
|
// 403 token过期 |
|
// 登录过期对用户进行提示 |
|
// 清除本地token和清空vuex中token对象 |
|
// 跳转登录页面 |
|
case 403: |
|
// Toast({ |
|
// message: '登录过期,请重新登录', |
|
// duration: 1000, |
|
// forbidClick: true |
|
// }); |
|
// 清除token |
|
// store.commit('loginSuccess', null); |
|
// 跳转登录页面,并将要浏览的页面fullPath传过去,登录成功后跳转需要访问的页面 |
|
setTimeout(() => { |
|
router.replace({ |
|
path: '/login', |
|
query: { |
|
redirect: router.currentRoute.fullPath |
|
} |
|
}); |
|
}, 1000); |
|
break; |
|
// 404请求不存在 |
|
case 404: |
|
router.replace({ |
|
path: '/404', |
|
}); |
|
break; |
|
// 其他错误,直接抛出错误提示 |
|
default: |
|
Promise.reject(res) |
|
} |
|
return Promise.reject(error.res); |
|
} |
|
} |
|
); |
|
|
|
function get(url, params){ |
|
return new Promise((resolve, reject) =>{ |
|
service.get(url, {params: params}) |
|
.then(res => { |
|
if(res){ |
|
resolve(res) |
|
}else{ |
|
reject() |
|
} |
|
}).catch(err => { |
|
reject(err.data) |
|
}) |
|
}) |
|
} |
|
|
|
function post(url, params){ |
|
return new Promise((resolve, reject) =>{ |
|
service.post(url,params) |
|
.then(res => { |
|
if(res){ |
|
resolve(res) |
|
}else{ |
|
reject() |
|
} |
|
}).catch(err => { |
|
reject(err.data) |
|
}) |
|
}) |
|
} |
|
|
|
function del(url, params){ |
|
return new Promise((resolve, reject) =>{ |
|
service.delete(url, { |
|
params |
|
}) |
|
.then(res => { |
|
if(res){ |
|
resolve(res) |
|
}else{ |
|
reject() |
|
} |
|
}).catch(err => { |
|
reject(err.data) |
|
}) |
|
}) |
|
} |
|
|
|
function put(url, params){ |
|
return new Promise((resolve, reject) =>{ |
|
service.put(url, { |
|
params |
|
}) |
|
.then(res => { |
|
if(res){ |
|
resolve(res) |
|
}else{ |
|
reject() |
|
} |
|
}).catch(err => { |
|
reject(err.data) |
|
}) |
|
}) |
|
} |
|
export { get,post,del,put } |