• vue之axios拦截


    1 state: {
    2     token: db.get('USER_TOKEN')
    3   }

    二、axios设置

    // 统一配置
    let FT = axios.create({
      baseURL: '',
      responseType: 'json',
      validateStatus (status) {
        // 200 外的状态码都认定为失败
        return status === 200
      }
    })
    
    // 拦截请求
    FT.interceptors.request.use((config) => {
      let expireTime = store.state.account.expireTime
      let now = moment().format('YYYYMMDDHHmmss')
      // 让token早10秒种过期,提升“请重新登录”弹窗体验
      if (now - expireTime >= -10) {
        Modal.error({
          title: '登录已过期',
          content: '很抱歉,登录已过期,请重新登录',
          okText: '重新登录',
          mask: false,
          onOk: () => {
            return new Promise((resolve, reject) => {
              db.clear()
              location.reload()
            })
          }
        })
      }
      // 有 token就带上
      if (store.state.account.token) {
        config.headers.Authentication = store.state.account.token
      }
      return config
    }, (error) => {
      return Promise.reject(error)
    })
    
    // 拦截响应
    FT.interceptors.response.use((config) => {
      return config
    }, (error) => {
      if (error.response) {
        let errorMessage = error.response.data === null ? '系统内部异常,请联系网站管理员' : error.response.data.message
        switch (error.response.status) {
          case 404:
            notification.error({
              message: '系统提示',
              description: '很抱歉,资源未找到',
              duration: 4
            })
            break
          case 403:
          case 401:
            notification.warn({
              message: '系统提示',
              description: '很抱歉,您无法访问该资源,可能是因为没有相应权限或者登录已失效',
              duration: 4
            })
            break
          default:
            notification.error({
              message: '系统提示',
              description: errorMessage,
              duration: 4
            })
            break
        }
      }
      return Promise.reject(error)
    })
  • 相关阅读:
    [Javascript] Closure Cove, 1
    [Backbone]7. Collection Views, Custom Events
    [Backbone]6. Collections.
    Immediately-Invoked Puzzler
    [Javascipt] Immediately-Invoker 2
    [Javascript] Using map() function instead of for loop
    [Javascript] Funciton Expression
    [Backbone]5. Model & View, toggle between Models and Views -- 2
    JS-jQuery-EasyUI:百科
    笔记-Java-Spring MVC:JAVA之常用的一些Spring MVC的路由写法以及参数传递方式
  • 原文地址:https://www.cnblogs.com/yangshuzhong/p/11412113.html
Copyright © 2020-2023  润新知