|
| 1 | +import axios from 'axios' |
| 2 | +import { MessageBox, Message } from 'element-ui' |
| 3 | +import store from '@/store' |
| 4 | +import { getToken, getRefreshToken } from '@/utils/auth' |
| 5 | +console.log('......',process.env); |
| 6 | + |
| 7 | +console.log('anotherRequest......', process.env.VUE_APP_ANOTHER_BASE_URL + process.env.VUE_APP_ANOTHER_BASE_API); |
| 8 | +// create an axios instance |
| 9 | +const service = axios.create({ |
| 10 | + baseURL: process.env.VUE_APP_ANOTHER_BASE_URL + process.env.VUE_APP_ANOTHER_BASE_API, // url = base url + request url |
| 11 | + // withCredentials: true, // send cookies when cross-domain requests |
| 12 | + timeout: 5000 // request timeout |
| 13 | +}) |
| 14 | + |
| 15 | +// request interceptor |
| 16 | +service.interceptors.request.use( |
| 17 | + config => { |
| 18 | + // Do something before request is sent |
| 19 | + if (store.getters.token) { |
| 20 | + // 让每个请求携带token-- ['Authorization']为自定义key 请根据实际情况自行修改 |
| 21 | + config.headers['Authorization'] = 'Bearer ' + getToken() |
| 22 | + // console.log('getToken', getToken()) |
| 23 | + } |
| 24 | + // 监听 是否 /sys/user/refreshtoken 是则重置token为刷新token |
| 25 | + const url = config.url |
| 26 | + if (url.split('/').pop() === 'refreshtoken') { |
| 27 | + // console.log('config.url', config.url, getRefreshToken()) |
| 28 | + config.headers['Authorization'] = 'Bearer ' + getRefreshToken() |
| 29 | + } |
| 30 | + |
| 31 | + return config |
| 32 | + }, |
| 33 | + error => { |
| 34 | + // Do something with request error |
| 35 | + console.log(error) // for debug |
| 36 | + Promise.reject(error) |
| 37 | + } |
| 38 | +) |
| 39 | + |
| 40 | +// response interceptor |
| 41 | +service.interceptors.response.use( |
| 42 | + /** |
| 43 | + * If you want to get http information such as headers or status |
| 44 | + * Please return response => response |
| 45 | + */ |
| 46 | + |
| 47 | + /** |
| 48 | + * 下面的注释为通过在response里,自定义code来标示请求状态 |
| 49 | + * 当code返回如下情况则说明权限有问题,登出并返回到登录页 |
| 50 | + * 如想通过 xmlhttprequest 来状态码标识 逻辑可写在下面error中 |
| 51 | + * 以下代码均为样例,请结合自生需求加以修改,若不需要,则可删除 |
| 52 | + */ |
| 53 | + response => { |
| 54 | + const res = response.data |
| 55 | + // console.log('response interceptor', response) |
| 56 | + if (res.code !== 20000) { |
| 57 | + Message({ |
| 58 | + message: res.message, |
| 59 | + type: 'error', |
| 60 | + duration: 5 * 1000 |
| 61 | + }) |
| 62 | + |
| 63 | + // 50008:非法的token; 50012:其他客户端登录了; 50014:Token 过期了; 50015: refresh_token过期 |
| 64 | + if (res.code === 50008 || res.code === 50012 || res.code === 50015) { |
| 65 | + // 请自行在引入 MessageBox |
| 66 | + // import { Message, MessageBox } from 'element-ui' |
| 67 | + console.log(' refresh_token过期 超时......') |
| 68 | + MessageBox.confirm('你已被登出,可以取消继续留在该页面,或者重新登录', '确定登出', { |
| 69 | + confirmButtonText: '重新登录', |
| 70 | + cancelButtonText: '取消', |
| 71 | + type: 'warning' |
| 72 | + }).then(() => { |
| 73 | + store.dispatch('user/FedLogOut').then(() => { |
| 74 | + location.reload() // 为了重新实例化vue-router对象 避免bug |
| 75 | + }) |
| 76 | + }) |
| 77 | + } |
| 78 | + return Promise.reject(new Error(res.message || 'Error')) |
| 79 | + } else { |
| 80 | + return response.data |
| 81 | + } |
| 82 | + }, |
| 83 | + error => { |
| 84 | + // Error对象可能log出来并不是你想象的那种以对象的样子出现 |
| 85 | + console.log(error.response) // console.log(error) 401 再判断 error.response.data.code |
| 86 | + // // let config = error.response.config |
| 87 | + // console.log('err' + error) // for debug |
| 88 | + |
| 89 | + // Message({ |
| 90 | + // message: error.message, |
| 91 | + // type: 'error', |
| 92 | + // duration: 5 * 1000 |
| 93 | + // }) |
| 94 | + |
| 95 | + // 拦截网络连接非 200 及 401 响应的错误, eg. Status Code: 500 Internal Server Error |
| 96 | + if (error.response.status !== 200 && error.response.status !== 401) { |
| 97 | + Message({ |
| 98 | + message: 'Status Code: ' + error.response.status + ' ' + error.response.statusText, |
| 99 | + type: 'error', |
| 100 | + duration: 3 * 1000 |
| 101 | + }) |
| 102 | + return |
| 103 | + } |
| 104 | + |
| 105 | + if (error.response.status === 401 && error.response.data.code === 50014) { |
| 106 | + // Message({ |
| 107 | + // message: 'access_token过期,自动续期', |
| 108 | + // type: 'error', |
| 109 | + // duration: 3* 1000 |
| 110 | + // }) |
| 111 | + return againRequest(error) |
| 112 | + } |
| 113 | + |
| 114 | + if (error.response.status === 401 && error.response.data.code === 50015) { |
| 115 | + // Message({ |
| 116 | + // message: 'refresh_token过期,重定向登录', |
| 117 | + // type: 'error', |
| 118 | + // duration: 3* 1000 |
| 119 | + // }) |
| 120 | + console.log('refresh_token过期 超时......') |
| 121 | + MessageBox.confirm('你已被登出,可以取消继续留在该页面,或者重新登录', '确定登出', { |
| 122 | + confirmButtonText: '重新登录', |
| 123 | + cancelButtonText: '取消', |
| 124 | + type: 'warning' |
| 125 | + }).then(() => { |
| 126 | + store.dispatch('user/FedLogOut').then(() => { |
| 127 | + location.reload() // 为了重新实例化vue-router对象 避免bug |
| 128 | + }) |
| 129 | + }) |
| 130 | + } |
| 131 | + |
| 132 | + return Promise.reject(error) |
| 133 | + } |
| 134 | +) // response 拦截结束 |
| 135 | + |
| 136 | +async function againRequest(error) { |
| 137 | + await store.dispatch('user/handleCheckRefreshToken') |
| 138 | + |
| 139 | + const config = error.response.config |
| 140 | + config.headers['Authorization'] = 'Bearer ' + getToken() |
| 141 | + |
| 142 | + const res = await axios.request(config) |
| 143 | + // console.log('againRequest...............................', res) |
| 144 | + return res.data // 以error.response.config重新请求返回的数据包是在函数内是 被封装在data里面 |
| 145 | +} |
| 146 | + |
| 147 | +export default service |
0 commit comments