baseService.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import { IHttpResponse, IObject } from "@/types/interface";
  2. import http from "../utils/http";
  3. /**
  4. * 常用CRUD
  5. */
  6. export default {
  7. /**
  8. * 删除
  9. * @param path
  10. * @param params
  11. * @returns
  12. */
  13. delete(path: string, params: IObject): Promise<IHttpResponse> {
  14. return http({
  15. url: path,
  16. data: params,
  17. method: "DELETE"
  18. });
  19. },
  20. get(path: string, params?: IObject, headers?: IObject): Promise<IHttpResponse> {
  21. return new Promise((resolve, reject) => {
  22. http({
  23. url: path,
  24. params,
  25. headers,
  26. method: "GET"
  27. })
  28. .then(resolve)
  29. .catch((error) => {
  30. if (error !== "-999") {
  31. reject(error);
  32. }
  33. });
  34. });
  35. },
  36. put(path: string, params?: IObject, headers?: IObject): Promise<IHttpResponse> {
  37. return http({
  38. url: path,
  39. data: params,
  40. headers: {
  41. "Content-Type": "application/json;charset=UTF-8",
  42. ...headers
  43. },
  44. method: "PUT"
  45. });
  46. },
  47. /**
  48. * 通用post方法
  49. * @param path
  50. * @param body
  51. * @returns
  52. */
  53. post(path: string, body?: IObject, headers?: IObject): Promise<IHttpResponse> {
  54. return http({
  55. url: path,
  56. method: "post",
  57. headers: {
  58. "Content-Type": "application/json;charset=UTF-8",
  59. ...headers
  60. },
  61. data: body
  62. });
  63. }
  64. };