request.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // api/request.js
  2. import axios from 'axios';
  3. import qs from "qs"
  4. // 创建请求实例
  5. const instance = axios.create({
  6. // baseURL: 'http://47.101.145.23/tianzong', // 设置接口基础路径
  7. baseURL: process.env.VUE_APP_BASE_API, // 设置接口基础路径
  8. timeout: 5000 // 设置请求超时时间
  9. });
  10. // 设置请求拦截器
  11. instance.interceptors.request.use(
  12. config => {
  13. if(config.emulateJSON){
  14. config.headers["Content-Type"] = "application/x-www-form-urlencoded;charset=UTF-8"
  15. }
  16. // 在请求发送前进行一些操作,如设置请求头、处理请求参数等
  17. return config;
  18. },
  19. error => {
  20. // 对请求错误进行处理
  21. return Promise.reject(error);
  22. }
  23. );
  24. // 设置响应拦截器
  25. instance.interceptors.response.use(
  26. response => {
  27. // 对接口返回的数据进行处理,如统一处理错误码、格式化数据等
  28. return response.data;
  29. },
  30. error => {
  31. // 对响应错误进行处理
  32. return Promise.reject(error);
  33. }
  34. );
  35. export default instance;