request.ts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { Interceptors } from './interceptors';
  2. export class HttpRequest {
  3. public axios: any
  4. constructor() {
  5. // 获取axios实例
  6. this.axios = new Interceptors().getInterceptors();
  7. }
  8. public get(url: string, params: String, config: Object = {}) {
  9. return new Promise((resolve, reject) => {
  10. let paramUrl = url
  11. if (params) {
  12. paramUrl += `?${params}`
  13. }
  14. this.axios.get(paramUrl, {
  15. ...config
  16. }).then((res: any) => {
  17. this.resultHandle(res, resolve, reject, url);
  18. }).catch((err: { message: any; }) => {
  19. console.log(err)
  20. reject(err.message);
  21. })
  22. })
  23. }
  24. public post(url: string, params: Object, config: Object = {}) {
  25. return new Promise((resolve, reject) => {
  26. this.axios.post(url, params, {
  27. ...config // 导出添加的下载类型
  28. }).then((res: any) => {
  29. this.resultHandle(res, resolve, reject, url);
  30. }).catch((err: { message: any; }) => {
  31. reject(err.message);
  32. })
  33. })
  34. }
  35. public delete(url: string, params: String, config: Object = {}) {
  36. return new Promise((resolve, reject) => {
  37. let paramUrl = url
  38. if (params) {
  39. paramUrl += `?${params}`
  40. }
  41. this.axios.delete(paramUrl, {
  42. ...config
  43. }).then((res: any) => {
  44. this.resultHandle(res, resolve, reject, url);
  45. }).catch((err: { message: any; }) => {
  46. console.log(err)
  47. reject(err.message);
  48. })
  49. })
  50. }
  51. public put(url: string, params: Object, config: Object = {}) {
  52. return new Promise((resolve, reject) => {
  53. this.axios.put(url, params, {
  54. ...config // 导出添加的下载类型
  55. }).then((res: any) => {
  56. this.resultHandle(res, resolve, reject, url);
  57. }).catch((err: { message: any; }) => {
  58. reject(err.message);
  59. })
  60. })
  61. }
  62. public resultHandle(res: any, resolve: { (value: unknown): void; (value: unknown): void; (arg0: any): void; },reject: { (value: unknown): void; (value: unknown): void; (arg0: any): void; }, url: string) {
  63. if (res) {
  64. if (res.code === 200 || (res.size > 0 && res.type)) { // 增加blob文件判断
  65. resolve(res);
  66. } else {
  67. resolve(res);
  68. // this.errorHandle(res, reject, url);
  69. }
  70. }
  71. }
  72. public errorHandle(res: any, reject: any, url: string) {
  73. console.error('错误接口:' + url)
  74. if (res.hasOwnProperty('message')) {
  75. // ElMessage.warning(res.message); // 统一谈服务端提示,我们提示统一由服务端提供
  76. } else if (res.hasOwnProperty('msg')) {
  77. // ElMessage.warning(res.msg); // 统一谈服务端提示,我们提示统一由服务端提供
  78. }
  79. // 状态码判断
  80. if (res) {
  81. switch (res.code) {
  82. case -102:
  83. break;
  84. case -152:
  85. break;
  86. default:
  87. reject(res);
  88. }
  89. }
  90. }
  91. }
  92. export default HttpRequest