美文网首页
Angular10封装http-2

Angular10封装http-2

作者: 小米和豆豆 | 来源:发表于2021-06-26 21:49 被阅读0次
    HttpServer .ts
    import { HttpClient, HttpErrorResponse, HttpHeaders, HttpParams } from '@angular/common/http';
    import { Injectable } from '@angular/core';
    import { Router } from '@angular/router';
    import { throwError } from 'rxjs';
    import { catchError, map } from 'rxjs/internal/operators';
    import { AppCommon } from "../common/app-common";
    
    @Injectable()
    export class HttpService {
        constructor(
            public httpClient: HttpClient,
            public router: Router,
        ) { }
    
        getParams(data: any, isJsonp = false): HttpParams {
            let params = new HttpParams();
            for (const key in data) {
                let val = data[key];
                if (typeof val == 'function') { continue; }
                params = params.set(key, val);
            }
            if (isJsonp) {
                params = params.set('callback', 'JSONP_CALLBACK');
            }
            return params;
        }
    
        jsonCall(data: any, url: string, type:string = 'get', withToken = true): any {
            // 使用httpClient模块开始
            // const lang = Language.getLanguage();
            let myHeader = new HttpHeaders();
            //myHeader.append('Accept-Language', lang);
            // if (!!AppCommon.userInfo) {
            //  myHeader.append('tenantId', AppCommon.userInfo.organization.id);
            //  myHeader.append('userId', AppCommon.userInfo.id);
            // }
            let options = { observe: 'response', headers: myHeader };
            if (type == 'get' || type == 'delete') {
                let obj:any = Object.assign(options, { params: this.getParams(data) });
                return this.httpClient[type](url, obj).pipe(
                    map(res=>{
                        this.processCallback(res);
                        return res['body']
                    }),
                    catchError(this.handleError)
                )
            } else {
                return this.httpClient[type](url, data, options).pipe(
                    map(res => {
                        this.processCallback(res);
                        return res['body']
                    }),
                    catchError(this.handleError)
                )
            }
        }
        private handleError(error: HttpErrorResponse) {
            let errObj = { 'status_code': error['status'], 'msg': error.message };
            if (error['status'] == 401) {
                AppCommon.userInfo = null;
            } 
            // Return an observable with a user-facing error message.
            return throwError(errObj);
        }
        private processCallback(data: any) {
            //活跃用户刷新token
            // if (data.headers.get('authorization') && data.headers.get('refreshtoken')) {
            //  let obj = {
            //      access_token: data.headers.get('authorization'),
            //      refresh_token: data.headers.get('refreshtoken')
            //  }
            // }
        }
    

    相关文章

      网友评论

          本文标题:Angular10封装http-2

          本文链接:https://www.haomeiwen.com/subject/cbgwjltx.html