token失效

282 阅读1分钟

响应401

在响应拦截器的错误中处理

1.退出登录

 /**
     * 需求:出现401,重新登录
     * 实现:
     * 1. 判断是401情况
     * 2. 删除之前登录存储的数据
     * 3. 错误提示
     * 4. 跳转登录页(携带出现401页面的地址=》目的是为了重新登录后,回到上次访问的页面)
     */
    if (error.response && error.response.status === 401) {
      // 处理某些页面多个请求多次401重复跳转问题,造成重新登录后不能正确跳转到上次访问页面问题
      if (router.currentRoute.path === '/login') return
      store.dispatch('user/logoutAction')
      // 错误提示
      Message({
        message: error.response.data.message,
        type: 'error',
        duration: 3 * 1000
      })
      router.replace(`/login?redirect=${router.currentRoute.path}`)
    }

2.更新token