base.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import axios from 'axios'
  2. const service = axios.create({
  3. baseURL: process.env.BASE_API, // api 的 base_url
  4. timeout: 5000 // 请求超时时间
  5. })
  6. console.log(123123,process.env)
  7. // request拦截器
  8. service.interceptors.request.use(
  9. config => {
  10. if (localStorage.getItem('token')) {
  11. config.headers['token'] = localStorage.getItem('token'); // 让每个请求携带自定义token 请根据实际情况自行修改
  12. }
  13. return config
  14. },
  15. error => {
  16. // Do something with request error
  17. console.log(error) // for debug
  18. Promise.reject(error)
  19. }
  20. )
  21. // response 拦截器
  22. service.interceptors.response.use(
  23. response => {
  24. const res = response.data
  25. if (res.code !== 200) {
  26. console.log('error',res.message);
  27. } else {
  28. return response.data
  29. }
  30. },
  31. error => {
  32. console.log('err' + error) // for debug
  33. return Promise.reject(error)
  34. }
  35. )
  36. export function getExampleData (code) {
  37. return service({
  38. url: '/skillInfo',
  39. method: 'get',
  40. params: {
  41. code
  42. }
  43. })
  44. }