vue interception of axios

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

Two, axios set

// unified configuration 
the let the FT = axios.create ({ 
  the baseURL: '' , 
  responseType: 'JSON' , 
  validateStatus (Status) { 
    // 200 is outside of status codes are identified as failed 
    return Status 200 is === 
  } 
}) 

// interception request 
FT.interceptors.request.use ((config) => { 
  the let expireTime = store.state.account.expireTime 
  the let now = Moment (). the format ( 'YYYYMMDDHHmmss' )
   // make early token expires 10 seconds, to enhance "Please log in again" pop experience 
  IF (now - expireTime> = -10 ) { 
    Modal.error ({ 
      title: "login has expired ' ,
      Content: 'Sorry, login has expired, please log in again' , 
      okText: 'log in again' , 
      mask: false , 
      the OnOK: () => {
         return  new new Promise ((Resolve, Reject) => { 
          db.clear () 
          location.reload () 
        }) 
      } 
    }) 
  } 
  // have token to bring 
  IF (store.state.account.token) { 
    config.headers.Authentication = store.state.account.token 
  } 
  return config 
}, (error) => {
   returnPromise.reject (error) 
}) 

// intercepted response 
FT.interceptors.response.use ((config) => {
   return config 
}, (error) => {
   IF (error.response) { 
    the let errorMessage = error.response. === the Data null ? 'internal system abnormalities, please contact the webmaster' : error.response.data.message
     Switch (error.response.status) {
       Case 404 : 
        notification.error ({ 
          the Message: 'prompted' , 
          the Description : 'We're sorry, the resource is not found' , 
          DURATION: . 4 
        }) 
        BREAK
      Case 403 :
       Case 401 : 
        notification.warn ({ 
          the Message: 'prompted' , 
          the Description: 'Sorry, you can not access the resources, you might not have the appropriate permissions or log has failed' , 
          DURATION: 4 
        }) 
        BREAK 
      default : 
        notification.error ({ 
          Message: 'prompted' , 
          Description: errorMessage, 
          DURATION: . 4 
        }) 
        BREAK 
    } 
  } 
  return Promise.reject (error) 
})

 

Guess you like

Origin www.cnblogs.com/yangshuzhong/p/11412113.html