request.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import Vue from 'vue'
  2. import axios from 'axios'
  3. import {
  4. MessageBox,
  5. Message
  6. } from 'element-ui'
  7. import store from '@/store'
  8. import router from '@/router'
  9. import {
  10. getToken, removeToken
  11. } from '@/utils/auth'
  12. const baseURL = process.env.VUE_APP_DOMAIN_PREFIX
  13. // create an axios instance
  14. Vue.prototype.axios = axios
  15. axios.defaults.timeout = 30000
  16. const service = axios.create({
  17. baseURL, // url = base url + request url
  18. // withCredentials: true, // send cookies when cross-domain requests
  19. timeout: 30000 // request timeout
  20. })
  21. export const uploadUrl = `${baseURL}/file/upload`
  22. export const token = getToken()
  23. // request interceptor
  24. service.interceptors.request.use(
  25. (config) => {
  26. // console.log(config)
  27. if (store.getters.token) {
  28. config.headers['Authorization-admin'] = getToken()
  29. config.headers['Content-Type'] = 'application/json; charset=UTF-8'
  30. // config.headers['type'] = ' admin'
  31. }
  32. return config
  33. },
  34. (error) => {
  35. console.log(error) // for debug
  36. return Promise.reject(error)
  37. }
  38. )
  39. // response interceptor
  40. service.interceptors.response.use(
  41. (response) => {
  42. const res = response.data
  43. if (response.config.responseType === 'blob') {
  44. console.log(response.data)
  45. return response.data
  46. }
  47. if (res.code !== '' && res.code != 200) {
  48. Message({
  49. message: res.message || 'Error',
  50. type: 'error',
  51. duration: 5 * 1000
  52. })
  53. // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
  54. /**
  55. token为空 20003
  56. public static final String TOKEN_IS_NULL = "20003";
  57. token认证失败
  58. public static final String TOKEN_APPROVE_ERROR = "20004";
  59. 请先登录
  60. public static final String USER_NOT_LOGIN = "20005";
  61. */
  62. const tokenerr = [20003, '20003', 20004, '20004', 20005, '20005']
  63. if (tokenerr.includes(res.code)) {
  64. localStorage.clear()
  65. removeToken()
  66. router.push({ path: '/login' })
  67. location.reload()
  68. }
  69. if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
  70. // to re-login
  71. MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
  72. confirmButtonText: 'Re-Login',
  73. cancelButtonText: 'Cancel',
  74. type: 'warning'
  75. }).then(() => {
  76. store.dispatch('user/resetToken').then(() => {
  77. location.reload()
  78. })
  79. })
  80. }
  81. return Promise.reject(new Error(res.message || 'Error'))
  82. }
  83. return res
  84. },
  85. (error) => {
  86. if (!error.message.includes('timeout')) {
  87. Message({
  88. message: '服务器暂无响应,请稍后重试',
  89. type: 'error',
  90. duration: 5 * 1000
  91. })
  92. }
  93. return Promise.reject(error)
  94. }
  95. )
  96. export default service