0b4a29fd40b9621a193599f91daa3136149ae738.svn-base 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import axios from 'axios'
  2. import { Message } from 'element-ui'
  3. import Cookies from 'js-cookie'
  4. import store from '../store'
  5. const fetchInstance = axios.create({
  6. baseURL: process.env.VUE_APP_BASE_API
  7. // timeout: 2000,
  8. })
  9. // request拦截器
  10. // fetchInstance.interceptors.request.use(config => {
  11. // if (store.getters.token) {
  12. // config.headers['DS-Token'] = Cookies.get('DS-Token')
  13. // }
  14. // config.withCredentials = true
  15. // return config
  16. // }, error => {
  17. // console.log(error) // for debug
  18. // Promise.reject(error)
  19. // })
  20. fetchInstance.interceptors.request.use(config => {
  21. // if (store.getters.token) {
  22. // config.headers['DS-Token'] = Cookies.get('DS-Token')
  23. // }
  24. config.withCredentials = true
  25. return config
  26. }, error => {
  27. console.log(error) // for debug
  28. Promise.reject(error)
  29. })
  30. // response拦截器
  31. fetchInstance.interceptors.response.use(
  32. response => {
  33. const res = response.data
  34. if (res.code !== 20000) {
  35. Message({
  36. message: res.message,
  37. type: 'error',
  38. duration: 5 * 1000
  39. })
  40. return Promise.reject('request error')
  41. } else {
  42. return response.data
  43. }
  44. },
  45. error => {
  46. if (error.message !== 'cancel') {
  47. console.log('err:' + error)// for debug
  48. Message({
  49. message: error.message,
  50. type: 'error',
  51. duration: 5 * 1000
  52. })
  53. }
  54. // Raven.captureException(error)
  55. return Promise.reject(error)
  56. }
  57. )
  58. export default fetchInstance