axios.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * @Description:
  3. * @Author: 欧阳承珺
  4. * @LastEditors: 欧阳承珺
  5. * @Date: 2022-10-19 13:58:11
  6. * @LastEditTime: 2022-11-03 15:36:24
  7. */
  8. import { Message } from 'element-ui';
  9. export default function({store, app:{ $axios }, redirect} ) {
  10. $axios.onRequest(config => {
  11. $axios.setToken(store.state.token, 'Bearer')
  12. })
  13. $axios.onError(error => {
  14. const code = parseInt(error.response && error.response.status)
  15. if(code === 401) {
  16. redirect('/')
  17. Message.error('登录过期,请重新登录')
  18. store.commit('OPEN_LOGIN',false)
  19. store.commit('DEL_TOKEN','')
  20. localStorage.removeItem('token')
  21. localStorage.removeItem('userInfo')
  22. return Promise.resolve(false)
  23. }
  24. if (code !== 200) {
  25. Message.error(error.response?.data?.msg)
  26. }
  27. })
  28. $axios.onResponse((res) => {
  29. if(res?.config?.url === '/auth/getImageCode') {
  30. return res
  31. }else if(res.data && res?.data?.code !== 200) {
  32. Message.error(res.data.msg)
  33. }else {
  34. return res.data
  35. }
  36. })
  37. }