123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- import {Injectable, Optional} from '@angular/core';
- import {HttpClient} from '@angular/common/http';
- import {HttpErrorResponse, HttpResponse, HttpHeaders} from '@angular/common/http';
- import {Observable, throwError} from 'rxjs';
- import {catchError, retry, share} from 'rxjs/operators';
- import {UserService} from './user.service';
- //import * as jsonConfig from '../../assets/appconfig.json';
- const httpOptions = {
- headers: new HttpHeaders({
- 'Content-Type': 'application/json'
- })
- };
- //const jConfig:Config=jsonConfig.default;
- export interface Config {
- webSite: string;
- webServerHost: string;
- siteUrl: string;
- isWxLogin: boolean;
- isDev?: boolean;
- }
- export interface RequsetData {
- Data: any;
- IsSuccess: boolean;
- Message: string;
- }
- @Injectable({providedIn: 'root'})
- export class ConfigService {
- configUrl = 'assets/appconfig.json';
- baseConfig: Config;
- constructor(private http: HttpClient, private userService: UserService) {
- }
- GetCofigData(): Config {
- return this.baseConfig;
- };
- GetConfig() {
- var cf = this.http.get<Config>(this.configUrl)
- .pipe(
- retry(3), // retry a failed request up to 3 times
- catchError(this.handleError), // then handle the error
- share()
- );
- return cf;
- };
- HttpGetRomote(url: string, params?: any): Observable<RequsetData> {
- const httpGetSubscriber = new Observable<RequsetData>((observer) => {
- this.GetConfig().subscribe((cfdata: Config) => {
- var remoteUrl = cfdata.webServerHost + url;
- let user = this.userService.GetUser();
- let ops = {};
- if (user) {
- ops = Object.assign(ops, {
- params: params,
- headers: httpOptions.headers.set("userName", encodeURI(user.username)).set("authorization", 'Bearer '+this.userService.GetToken())
- });
- } else {
- ops = Object.assign(ops, {params: params}, httpOptions);
- }
- //let ops = Object.assign({}, {params: params}, httpOptions);
- this.http.get<RequsetData>(remoteUrl, ops).pipe(
- retry(3),
- catchError(this.handleError)
- ).subscribe(observer);
- });
- return {
- unsubscribe() {
- }
- };
- });
- /*
- params=params||{};
- let httpGetSubscriber = new Observable<RequsetData>((observer)=>{
- var remoteUrl=this.baseConfig.webServerHost+url;
- const ops = Object.assign({}, {params: params}, httpOptions);
- this.http.get<RequsetData>(remoteUrl,ops).pipe(
- retry(3),
- catchError(this.handleError)
- ).subscribe(observer);
- return {unsubscribe() {}};
- });
- */
- return httpGetSubscriber;
- };
- HttpPostRomote(url: string, data: any): Observable<RequsetData> {
- const httpPostSubscriber = new Observable<RequsetData>((observer) => {
- this.GetConfig().subscribe((cfdata: Config) => {
- var remoteUrl = cfdata.webServerHost + url;
- let user = this.userService.GetUser();
- let ops = {};
- if (user) {
- //httpOptions.headers=httpOptions.headers.set("userName",user.username);
- ops = { headers: httpOptions.headers.set("userName", encodeURI(user.username)).set("authorization", 'Bearer ' +this.userService.GetToken())};
- } else {
- ops = httpOptions;
- }
- this.http.post<RequsetData>(remoteUrl, data, ops).pipe(
- retry(3),
- catchError(this.handleError)
- ).subscribe(observer);
- });
- return {
- unsubscribe() {
- }
- };
- });
- /*
- let httpPostSubscriber = new Observable<RequsetData>((observer)=>{
- var remoteUrl=this.baseConfig.webServerHost+url;
- this.http.post<RequsetData>(remoteUrl,data,httpOptions).pipe(
- retry(3),
- catchError(this.handleError)
- ).subscribe(observer);
- return {unsubscribe() {}};
- });
- */
- return httpPostSubscriber;
- };
- private handleError(error: HttpErrorResponse) {
- if (error.error instanceof ErrorEvent) {
- // A client-side or network error occurred. Handle it accordingly.
- console.error('http,An error occurred:', error.error.message);
- } else {
- // The backend returned an unsuccessful response code.
- // The response body may contain clues as to what went wrong,
- console.error(
- `http,Backend returned code ${error.status}, ` +
- `body was: ${error.error},` +
- `error was: ${error}`);
- }
- // return an observable with a user-facing error message
- return throwError(
- 'http,Backend returned code ' + error.status + ',message was: ' + error.error.message);
- };
- }
|