axios.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. console.log(123)
  28. Message.error(error.response?.data?.msg)
  29. }
  30. })
  31. $axios.onResponse((res) => {
  32. if(res?.config?.url === '/auth/getImageCode') {
  33. return res
  34. }else if(res.data && res?.data?.code !== 200) {
  35. Message.error(res.data.msg)
  36. }else {
  37. return res.data
  38. }
  39. })
  40. }