index.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import {httpApi} from "@/common/js/baseUrl";
  2. import {handleHttpError, handleNormalResponse} from "@/utils/fetch/responseHandler";
  3. import handleReqLoading from "@/utils/fetch/handleReqLoading";
  4. import {handleRequest} from "@/utils/fetch/requestHandler";
  5. /**
  6. * 封装请求
  7. * @param url 请求地址
  8. * @param type 请求方式
  9. * @param data 请求参数
  10. * @param dataType 请求数据类型
  11. */
  12. export const myFetch = async (url, type = "get", data = {}, dataType) => {
  13. const reqConfig = {
  14. url: httpApi + url,
  15. method: type,
  16. data: data,
  17. dataType: dataType,
  18. timeout: 60000,
  19. header: {
  20. 'content-type': 'application/json',
  21. Token: '1',
  22. appId:'2002407848',
  23. },
  24. }
  25. try {
  26. handleReqLoading(true,dataType);
  27. const response = await handleRequest(reqConfig);
  28. const responseData = handleNormalResponse(response);
  29. handleReqLoading(false,dataType);
  30. //返回一个promise对象
  31. return responseData;
  32. }catch (err) {
  33. handleReqLoading(false,dataType);
  34. return handleHttpError(err);
  35. }
  36. }