config.service.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import {Injectable, Optional} from '@angular/core';
  2. import {HttpClient} from '@angular/common/http';
  3. import {HttpErrorResponse, HttpResponse, HttpHeaders} from '@angular/common/http';
  4. import {Observable, throwError} from 'rxjs';
  5. import {catchError, retry, share} from 'rxjs/operators';
  6. import {UserService} from './user.service';
  7. //import * as jsonConfig from '../../assets/appconfig.json';
  8. const httpOptions = {
  9. headers: new HttpHeaders({
  10. 'Content-Type': 'application/json'
  11. })
  12. };
  13. //const jConfig:Config=jsonConfig.default;
  14. export interface Config {
  15. webSite: string;
  16. webServerHost: string;
  17. siteUrl: string;
  18. isWxLogin: boolean;
  19. isDev?: boolean;
  20. }
  21. export interface RequsetData {
  22. Data: any;
  23. IsSuccess: boolean;
  24. Message: string;
  25. }
  26. @Injectable({providedIn: 'root'})
  27. export class ConfigService {
  28. configUrl = 'assets/appconfig.json';
  29. baseConfig: Config;
  30. constructor(private http: HttpClient, private userService: UserService) {
  31. }
  32. GetCofigData(): Config {
  33. return this.baseConfig;
  34. };
  35. GetConfig() {
  36. var cf = this.http.get<Config>(this.configUrl)
  37. .pipe(
  38. retry(3), // retry a failed request up to 3 times
  39. catchError(this.handleError), // then handle the error
  40. share()
  41. );
  42. return cf;
  43. };
  44. HttpGetRomote(url: string, params?: any): Observable<RequsetData> {
  45. const httpGetSubscriber = new Observable<RequsetData>((observer) => {
  46. this.GetConfig().subscribe((cfdata: Config) => {
  47. var remoteUrl = cfdata.webServerHost + url;
  48. let user = this.userService.GetUser();
  49. let ops = {};
  50. if (user) {
  51. ops = Object.assign(ops, {
  52. params: params,
  53. headers: httpOptions.headers.set("userName", encodeURI(user.username)).set("authorization", 'Bearer '+this.userService.GetToken())
  54. });
  55. } else {
  56. ops = Object.assign(ops, {params: params}, httpOptions);
  57. }
  58. //let ops = Object.assign({}, {params: params}, httpOptions);
  59. this.http.get<RequsetData>(remoteUrl, ops).pipe(
  60. retry(3),
  61. catchError(this.handleError)
  62. ).subscribe(observer);
  63. });
  64. return {
  65. unsubscribe() {
  66. }
  67. };
  68. });
  69. /*
  70. params=params||{};
  71. let httpGetSubscriber = new Observable<RequsetData>((observer)=>{
  72. var remoteUrl=this.baseConfig.webServerHost+url;
  73. const ops = Object.assign({}, {params: params}, httpOptions);
  74. this.http.get<RequsetData>(remoteUrl,ops).pipe(
  75. retry(3),
  76. catchError(this.handleError)
  77. ).subscribe(observer);
  78. return {unsubscribe() {}};
  79. });
  80. */
  81. return httpGetSubscriber;
  82. };
  83. HttpPostRomote(url: string, data: any): Observable<RequsetData> {
  84. const httpPostSubscriber = new Observable<RequsetData>((observer) => {
  85. this.GetConfig().subscribe((cfdata: Config) => {
  86. var remoteUrl = cfdata.webServerHost + url;
  87. let user = this.userService.GetUser();
  88. let ops = {};
  89. if (user) {
  90. //httpOptions.headers=httpOptions.headers.set("userName",user.username);
  91. ops = { headers: httpOptions.headers.set("userName", encodeURI(user.username)).set("authorization", 'Bearer ' +this.userService.GetToken())};
  92. } else {
  93. ops = httpOptions;
  94. }
  95. this.http.post<RequsetData>(remoteUrl, data, ops).pipe(
  96. retry(3),
  97. catchError(this.handleError)
  98. ).subscribe(observer);
  99. });
  100. return {
  101. unsubscribe() {
  102. }
  103. };
  104. });
  105. /*
  106. let httpPostSubscriber = new Observable<RequsetData>((observer)=>{
  107. var remoteUrl=this.baseConfig.webServerHost+url;
  108. this.http.post<RequsetData>(remoteUrl,data,httpOptions).pipe(
  109. retry(3),
  110. catchError(this.handleError)
  111. ).subscribe(observer);
  112. return {unsubscribe() {}};
  113. });
  114. */
  115. return httpPostSubscriber;
  116. };
  117. private handleError(error: HttpErrorResponse) {
  118. if (error.error instanceof ErrorEvent) {
  119. // A client-side or network error occurred. Handle it accordingly.
  120. console.error('http,An error occurred:', error.error.message);
  121. } else {
  122. // The backend returned an unsuccessful response code.
  123. // The response body may contain clues as to what went wrong,
  124. console.error(
  125. `http,Backend returned code ${error.status}, ` +
  126. `body was: ${error.error},` +
  127. `error was: ${error}`);
  128. }
  129. // return an observable with a user-facing error message
  130. return throwError(
  131. 'http,Backend returned code ' + error.status + ',message was: ' + error.error.message);
  132. };
  133. }