1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| request.interceptors.response.use(function (response) { return response }, async function (error) { if (error.response.status === 401) { if (store.state.user && store.state.user.refresh_token) { try { const result = await axios({ url: 'http://ttapi.research.itcast.cn/app/v1_0/authorizations', method: 'PUT', headers: { Authorization: `Bearer ${store.state.user.refresh_token}` } })
store.commit('updateToken', result.data.data.token) console.log('更新token ok')
return request(error.config) } catch (e) { console.log('更新token,refresh_token 失败') store.commit('setUser', null) router.push({ path: '/login', query: { from: router.currentRoute.fullPath } }) } } else { console.log('没有refresh token,准备去重新登陆吧') router.push({ path: '/login', query: { from: router.currentRoute.fullPath } }) } console.log('响应拦截器') console.dir(error) } else { return Promise.reject(error) } })
|