request.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import axios from 'axios'
  2. import router from '../router'
  3. import store from '../store'
  4. import Cookies from 'js-cookie'
  5. import { Message } from 'element-ui'
  6. import { getToken, removeToken } from "../utils/auth"
  7. import { saveSession, getSession, removeSession } from "@/utils/role";
  8. axios.defaults.withCredentials = true
  9. axios.defaults.dataType = 'json'
  10. axios.defaults.headers['cache-control'] = 'no-cache'
  11. axios.defaults.headers['Content-Type'] = 'application/json'
  12. axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'
  13. // create an axios instance
  14. const service = axios.create({
  15. baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  16. // withCredentials: true, // send cookies when cross-domain requests
  17. timeout: 60000 // request timeout
  18. })
  19. // request interceptor
  20. service.interceptors.request.use(
  21. config => {
  22. // do something before request is sent
  23. // let each request carry token
  24. // ['X-Token'] is a custom headers key
  25. // please modify it according to the actual situation
  26. config.headers['Content-Type'] = 'application/json'
  27. return config
  28. },
  29. error => {
  30. // do something with request error
  31. console.log(error) // for debug
  32. return Promise.reject(error)
  33. }
  34. )
  35. // response interceptor
  36. service.interceptors.response.use(
  37. /**
  38. * If you want to get http information such as headers or status
  39. * Please return response => response
  40. */
  41. /**
  42. * Determine the request status by custom code
  43. * Here is just an example
  44. * You can also judge the status by HTTP Status Code
  45. */
  46. response => {
  47. const res = response.data
  48. // console.log(res)
  49. let msg = null
  50. // if the custom code is not 20000, it is judged as an error.
  51. if (res.status == 0 || res.status == -2) {
  52. // 执行错误 JSON 数据格式错误
  53. msg = Message({
  54. message: res.msg || res.error || 'Error',
  55. type: 'error',
  56. showClose: true,
  57. duration: 0
  58. })
  59. return Promise.reject(new Error(res.msg || res.error || 'Error'))
  60. } else if (res.status === -1) {
  61. // 登录失效
  62. Cookies.remove("JSESSIONID");
  63. removeToken()
  64. removeSession("SysList")
  65. msg = Message({
  66. message: '登录会话失效,请重新登录',
  67. type: 'error',
  68. showClose: true,
  69. duration: 0
  70. })
  71. if (process.env.NODE_ENV == "development") {
  72. router.push("/login")
  73. } else {
  74. window.location.href = "/"
  75. }
  76. return false
  77. } else {
  78. Message.closeAll()
  79. return res
  80. }
  81. },
  82. error => {
  83. // || (error+1).indexOf('500') > -1
  84. if ((error + 1).indexOf('401') > -1) {
  85. // console.log(router)
  86. // store.dispatch('user/postError')
  87. // router.push(`/login`)
  88. }
  89. Message({
  90. message: '网络错误,请稍后重试',
  91. type: 'error',
  92. duration: 2 * 1000
  93. })
  94. return Promise.reject(error)
  95. }
  96. )
  97. export default service