You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1018 B

4 years ago
  1. import Vue from 'vue'
  2. import axios from 'axios'
  3. // ---------------------------------------------- 封装 post 和 get
  4. const instance = axios.create({
  5. baseURL: 'http://127.0.0.1:9090/',
  6. timeout: 10000 * 30,
  7. withCredentials: true,
  8. headers: {
  9. 'Content-Type': 'application/json; charset=utf-8'
  10. }
  11. })
  12. /**
  13. * 请求拦截
  14. */
  15. instance.interceptors.request.use(config => {
  16. config.headers['token'] =Vue.cookie.get('token') // 请求头带上token
  17. return config
  18. }, error => {
  19. return Promise.reject(error)
  20. })
  21. /**
  22. * 响应拦截
  23. */
  24. instance.interceptors.response.use(response => {
  25. if (response.data && response.data.code === 401) { // 401, token失效
  26. clearLoginInfo()
  27. router.push({ name: 'login' })
  28. }
  29. return response
  30. }, error => {
  31. return Promise.reject(error)
  32. })
  33. export const createAPI = (url, method, data) => {
  34. let config = {}
  35. if (method === 'get') {
  36. config.params = data
  37. } else {
  38. config.data = data
  39. }
  40. return instance({
  41. url,
  42. method,
  43. ...config
  44. })
  45. }