1a195cc19b2aad53d01ca66ed8a1f11045f8e985.svn-base 3.1 KB

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