5ce1b6b936c40660b67e8b7714496003a9817d1f.svn-base 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import axios from 'axios'
  2. import { MessageBox, Message } from 'element-ui'
  3. import store from '@/store'
  4. import { getToken } from '@/utils/auth'
  5. // create an axios instance
  6. const service = axios.create({
  7. baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  8. withCredentials: true, // send cookies when cross-domain requests
  9. timeout: 60000 // request timeout
  10. })
  11. // request interceptor
  12. service.interceptors.request.use(
  13. config => {
  14. // do something before request is sent
  15. // config.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8' //此处是增加的代码,设置请求头的类型
  16. if (process.env.VUE_APP_BASE_API !== '/dev-api') {
  17. config.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
  18. config.withCredentials = false
  19. }
  20. if (store.getters.token) {
  21. // let each request carry token
  22. // ['Authorization'] is a custom headers key
  23. // please modify it according to the actual situation
  24. if (process.env.VUE_APP_BASE_API === '/dev-api') {
  25. config.headers['X-Token'] = getToken()
  26. } else {
  27. config.headers['token'] = getToken()
  28. }
  29. }
  30. return config
  31. },
  32. error => {
  33. // do something with request error
  34. console.log(error) // for debug
  35. return Promise.reject(error)
  36. }
  37. )
  38. // response interceptor
  39. service.interceptors.response.use(
  40. /**
  41. * If you want to get http information such as headers or status
  42. * Please return response => response
  43. */
  44. /**
  45. * Determine the request status by custom code
  46. * Here is just an example
  47. * You can also judge the status by HTTP Status Code
  48. */
  49. response => {
  50. const res = response.data
  51. // if the custom code is not 20000, it is judged as an error.
  52. if (res.code !== 200) {
  53. Message({
  54. // message: res.msg + res.code,
  55. message: '请求超时',
  56. type: 'error',
  57. duration: 5 * 1000
  58. })
  59. // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
  60. if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
  61. // to re-login
  62. MessageBox.confirm('你已经注销登陆,你可以取消或重新登陆', '确认注销', {
  63. confirmButtonText: '重新登陆',
  64. cancelButtonText: '取消',
  65. type: 'warning'
  66. }).then(() => {
  67. store.dispatch('user/resetToken').then(() => {
  68. location.reload()
  69. })
  70. })
  71. }
  72. if (res.code === 20002) {
  73. store.dispatch('user/resetToken').then(() => {
  74. location.reload()
  75. })
  76. }
  77. if (res.code === undefined) {
  78. return res
  79. } else {
  80. return Promise.reject(new Error(res.message || 'Error'))
  81. }
  82. } else {
  83. return res
  84. }
  85. },
  86. error => {
  87. console.log('err' + error) // for debug
  88. Message({
  89. message: error.message,
  90. type: 'error',
  91. duration: 5 * 1000
  92. })
  93. return Promise.reject(error)
  94. }
  95. )
  96. export default service