requestInterceptors.js 1.4 KB

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