You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
823 B

2 years ago
2 years ago
  1. export default ({ app, $axios, store, redirect }, inject) => {
  2. $axios.defaults.timeout = 5000;
  3. // 請求攔截
  4. $axios.onRequest((config) => {
  5. // token 儲存在緩存
  6. console.log("axios.onRequest");
  7. const token = app.$auth.$storage.getUniversal('authtoken');
  8. if(token){
  9. config.headers["authtoken"] = token;
  10. }
  11. })
  12. // 服務器返回異常攔截
  13. $axios.onError((error) => {
  14. const code = parseInt(error && error.response && error.response.status)
  15. if (code === 401) {
  16. app.$auth.$storage.removeUniversal('userPicture')
  17. app.$auth.$storage.removeUniversal('userLastName')
  18. app.$auth.$storage.removeUniversal('jwt')
  19. app.$auth.logout()
  20. }
  21. return error
  22. })
  23. // 接口數據返回攔截
  24. $axios.onResponse((response) => {
  25. return response
  26. })
  27. }