http.service.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient, HttpHeaders } from '@angular/common/http';
  3. @Injectable({
  4. providedIn: 'root',
  5. })
  6. export class HttpService {
  7. constructor(public http: HttpClient) {}
  8. customSQL(sql: string) {
  9. return new Promise((resolve, reason) => {
  10. this.http
  11. .post('https://server.fmode.cn/api/novaql/select', {
  12. sql,
  13. })
  14. .subscribe((data) => {
  15. resolve(data);
  16. });
  17. });
  18. }
  19. // 定义请求方法
  20. httpPostRquest(url: string, params: any) : Promise<any> {
  21. return new Promise((resolve, reason) => {
  22. this.http
  23. .post(url, {
  24. params,
  25. })
  26. .subscribe((data) => {
  27. resolve(data);
  28. });
  29. });
  30. }
  31. postAuth(company: string, idCard: string, name: string) {
  32. return new Promise((resolve, reason) => {
  33. this.http
  34. .post('https://server.fmode.cn/api/apig/idcard', {
  35. company: company,
  36. cardNo: idCard,
  37. realName: name,
  38. })
  39. .subscribe((data) => {
  40. resolve(data);
  41. });
  42. });
  43. }
  44. // 定义jsonp的方法
  45. getJsonpMsg(url: string, cb: string) {
  46. return this.http.jsonp(url, cb);
  47. }
  48. async httpRequst(url: string, reqBody: any, method: string = 'GET') {
  49. let queryString = '';
  50. if (method === 'GET' && reqBody) {
  51. queryString = new URLSearchParams(reqBody).toString();
  52. url += `?${queryString}`;
  53. }
  54. let result = await fetch(url, {
  55. method: method,
  56. headers: {
  57. 'Content-Type': 'application/json',
  58. },
  59. body: method !== 'GET' ? JSON.stringify(reqBody) : undefined,
  60. });
  61. let data = await result.json();
  62. // console.log(data);
  63. return data;
  64. }
  65. }