httpUtils.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * 一切http辅助类的基类,用于调用axios
  3. * nh 2021.12.23
  4. */
  5. import { axiosUtils } from "./axiosUtils";
  6. import { toolUtils } from "../toolUtils";
  7. export class httpUtils extends axiosUtils {
  8. protected constructor(baseUrl: string) {
  9. super();
  10. this.baseUrl = baseUrl;
  11. }
  12. //根域名地址
  13. private _baseUrl = "";
  14. private set baseUrl(val: string) {
  15. this._baseUrl = val;
  16. }
  17. private get baseUrl() {
  18. return this._baseUrl;
  19. }
  20. private async send(url: string, data: any, superFnName: string) {
  21. var newUrl = toolUtils.getBaseHttpUrl(this.baseUrl, url);
  22. return await super[superFnName](newUrl, data);
  23. }
  24. /**
  25. * get请求
  26. * @param url
  27. * @param data
  28. * @returns
  29. */
  30. public async getRequest(url: string, data: any) {
  31. return await this.send(url, data, "getRequest");
  32. }
  33. /**
  34. * post请求
  35. * @param url
  36. * @param data
  37. * @returns
  38. */
  39. public async postRequest(url: string, data: any) {
  40. return await this.send(url, data, "postRequest");
  41. }
  42. /**
  43. * 自定义请求
  44. * @param url
  45. * @param headers
  46. * @param data
  47. * @param method get | post 默认post
  48. * @returns
  49. */
  50. protected async customRequest(
  51. url: string,
  52. headers: any,
  53. data: any,
  54. method: string
  55. ) {
  56. return await super.customRequest(url, headers, data, method);
  57. }
  58. }