requestInterceptors.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  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 && httpConfig.url.includes("/user/findUser")){
  17. httpConfig.baseURL = uni.getStorageSync('requestPath')+'/app';
  18. }
  19. if (httpConfig.custom.isPgyerApi) {
  20. httpConfig.baseURL = "https://www.pgyer.com";
  21. }
  22. httpConfig.header = {
  23. ...httpConfig.header,
  24. operationID: uuidV4(),
  25. };
  26. //console.log("qxj httprequest config:"+JSON.stringify(httpConfig));
  27. // 可以在此通过vm引用vuex中的变量,具体值在vm.$store.state中
  28. console.log("qxj httprequest url:"+(httpConfig.baseURL+httpConfig.url)+" \n method:"+httpConfig.method+" header:"+JSON.stringify(httpConfig.header)+" body:"+JSON.stringify(httpConfig.data)+" \n");
  29. return httpConfig;
  30. },
  31. (
  32. httpConfig, // 可使用async await 做异步操作
  33. ) => Promise.reject(httpConfig),
  34. );
  35. };