fetch.js 941 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import axios from "axios";
  2. //axios 配置
  3. const instance = axios.create({
  4. headers: {
  5. "Content-Type": "application/json"
  6. },
  7. timeout: 3000000,
  8. retry: 4, //重新请求次数
  9. retryDelay: 1000, //重新请求的间隔
  10. })
  11. instance.interceptors.request.use(config => {
  12. // 在发送请求之前做些什么,比如传token
  13. const token = localStorage.getItem('token');
  14. const projectId = localStorage.getItem('projectId');
  15. if (token) {
  16. config.headers.Authorization = token;
  17. }
  18. if (projectId) {
  19. config.headers.ProjectId = projectId;
  20. }
  21. return config
  22. }, error => {
  23. console.log(error)
  24. return Promise.reject(error)
  25. })
  26. // 添加相应拦截器
  27. instance.interceptors.response.use(response => {
  28. //对相应的数据做处理
  29. const res = response;
  30. return res;
  31. }, error => {
  32. console.error(error)
  33. return Promise.reject(error)
  34. })
  35. export default instance