axios.js 1.1 KB

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