httpUtils.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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(url: string, headers: any, data: any, method: string) {
  51. return await super.customRequest(url, headers, data, method);
  52. }
  53. }