requestInterceptors.js 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. import { config } from "../../common/config";
  2. import { v4 as uuidV4 } from "uuid";
  3. /**
  4. * 请求拦截
  5. * @param {Object} http
  6. */
  7. module.exports = (vm) => {
  8. uni.$u.http.interceptors.request.use(
  9. (httpConfig) => {
  10. // 可使用async await 做异步操作
  11. // 初始化请求拦截器时,会执行此方法,此时data为undefined,赋予默认{}
  12. httpConfig.data = httpConfig.data || {};
  13. if (httpConfig.custom.isIMApi) {
  14. httpConfig.baseURL = config.getApiUrl();
  15. }
  16. if (httpConfig.custom.isPgyerApi) {
  17. httpConfig.baseURL = "https://www.pgyer.com";
  18. }
  19. httpConfig.header = {
  20. ...httpConfig.header,
  21. operationID: uuidV4(),
  22. };
  23. //console.log("qxj httprequest config:"+JSON.stringify(httpConfig));
  24. // 可以在此通过vm引用vuex中的变量,具体值在vm.$store.state中
  25. console.log("qxj httprequest url:"+(httpConfig.baseURL+httpConfig.url)+" \n method:"+httpConfig.method+" header:"+JSON.stringify(httpConfig.header)+" body:"+JSON.stringify(httpConfig.data)+" \n");
  26. return httpConfig;
  27. },
  28. (
  29. httpConfig, // 可使用async await 做异步操作
  30. ) => Promise.reject(httpConfig),
  31. );
  32. };