request.js 3.1 KB

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