Blame view
src/utils/request.js
3.16 KB
80a28914e init |
1 2 3 4 5 6 |
import axios from 'axios' import { MessageBox, Message } from 'element-ui' import store from '@/store' import { getToken } from '@/utils/auth' // create an axios instance |
a795fa7b1 auto commit the c... |
7 |
// 创建axios实例 |
80a28914e init |
8 9 10 11 12 13 14 |
const service = axios.create({ baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url // withCredentials: true, // send cookies when cross-domain requests timeout: 5000 // request timeout }) // request interceptor |
a795fa7b1 auto commit the c... |
15 |
// request拦截器 |
80a28914e init |
16 17 18 |
service.interceptors.request.use( config => { // do something before request is sent |
a795fa7b1 auto commit the c... |
19 |
console.log('do something before request is sent') |
80a28914e init |
20 |
if (store.getters.token) { |
a795fa7b1 auto commit the c... |
21 |
console.log('[X-Token] is a custom headers key') |
80a28914e init |
22 23 24 25 26 27 28 29 |
// let each request carry token // ['X-Token'] is a custom headers key // please modify it according to the actual situation config.headers['X-Token'] = getToken() } return config }, error => { |
a795fa7b1 auto commit the c... |
30 |
console.log('do something with request error') |
80a28914e init |
31 32 33 34 35 36 37 |
// do something with request error console.log(error) // for debug return Promise.reject(error) } ) // response interceptor |
a795fa7b1 auto commit the c... |
38 |
// respone拦截器 |
80a28914e init |
39 40 41 42 43 44 45 46 47 48 49 50 51 |
service.interceptors.response.use( /** * If you want to get http information such as headers or status * Please return response => response */ /** * Determine the request status by custom code * Here is just an example * You can also judge the status by HTTP Status Code */ response => { const res = response.data |
a795fa7b1 auto commit the c... |
52 53 54 55 56 |
/** * 下面的注释为通过response自定义code来标示请求状态,当code返回如下情况为权限有问题,登出并返回到登录页 * 如通过xmlhttprequest 状态码标识 逻辑可写在下面error中 */ console.log('if the custom code is not 20000, it is judged as an error.') |
80a28914e init |
57 58 59 60 61 62 63 64 65 |
// if the custom code is not 20000, it is judged as an error. if (res.code !== 20000) { Message({ message: res.message || 'Error', type: 'error', duration: 5 * 1000 }) // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired; |
a795fa7b1 auto commit the c... |
66 |
// 50008:非法的token; 50012:其他客户端登录了; 50014:Token 过期了; |
80a28914e init |
67 68 |
if (res.code === 50008 || res.code === 50012 || res.code === 50014) { // to re-login |
a795fa7b1 auto commit the c... |
69 70 71 72 73 74 |
// MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', { MessageBox.confirm('你已被登出,可以取消继续留在该页面,或者重新登录', '确定登出', { // confirmButtonText: 'Re-Login', confirmButtonText: '重新登录', // cancelButtonText: 'Cancel', cancelButtonText: '取消', |
80a28914e init |
75 76 77 |
type: 'warning' }).then(() => { store.dispatch('user/resetToken').then(() => { |
a795fa7b1 auto commit the c... |
78 |
location.reload()// 为了重新实例化vue-router对象 避免bug |
80a28914e init |
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
}) }) } return Promise.reject(new Error(res.message || 'Error')) } else { return res } }, error => { console.log('err' + error) // for debug Message({ message: error.message, type: 'error', duration: 5 * 1000 }) return Promise.reject(error) } ) export default service |