如何在 http 拦截器中使用 cordova 设备就绪功能

How can I use cordova device ready function inside http interceptor

我正在将我的 angular 8 应用程序转换为 android 应用程序。在 android 应用程序中我不能使用 'ngx-cookie-service' 所以我必须使用 cordova-plugin-cookiemaster。所以我需要知道我在我的 http 拦截器中使用哪个平台,然后我可以决定我应该使用 ngx-cookie-service 还是 cordova-plugin-cookiemaster。这是我的拦截器代码

import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpHandler, HttpRequest, HttpEvent, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { CookieService } from 'ngx-cookie-service';
import { environment } from '../../../environments/environment';
declare var cookieMaster;

@Injectable()
export class Interceptor implements HttpInterceptor {
  device = {};
  domain = environment.domain;
  constructor(
    private cookieService: CookieService,
  ) { }

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    let token = '';
    token = this.cookieService.get('tolotoken');
    console.log(this.getCookie());
    // if (this.device['platform'] === 'Android') {
    //   this.getCookie();
    // } else {
    // }
    if (token) {
      request = request.clone({ headers: request.headers.set('Authorization', 'Bearer ' + token) });
    }
    if (!request.headers.has('Content-Type')) {
      request = request.clone({ headers: request.headers.set('Content-Type', 'application/json') });
    }
    request = request.clone({ headers: request.headers.set('Accept', 'application/json') });
    return next.handle(request).pipe(
      map((event: HttpEvent<any>) => {
        if (event instanceof HttpResponse) {
        }
        return event;
      }));
  }

  getCookie() {
    cookieMaster.getCookieValue(this.domain, 'tolotoken', (data) => {
      console.log(data);
    }, (error) => {
      if (error) {
        console.log('error: ' + error);
      }
    });
  }
}

并且我已经编写了 cordova-device-ready in service

import { Injectable, OnInit } from '@angular/core';
declare var device;

@Injectable({
  providedIn: 'root'
})
export class UtilityService implements OnInit {
  _device = {};
  constructor() { }

  ngOnInit() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
  }
  onDeviceReady() {
    this._device = device;
    console.log(device);
  }
  getDevice() {
    return this._device;
  }

}

并且我尝试通过注入实用程序服务在 Http-interceptor 中使用此服务,但它不起作用设备始终为空。是否有任何解决方法来查找设备信息?或任何其他想法来决定使用哪种 cookie 服务?谢谢。

只需使用@angular/cdk/platform

import { Platform } from "@angular/cdk/platform";
...
constructor(private platform:Platform){}
...
if(this.platform.ANDROID){
//call your cookie method
}

如果您需要更多 ondeviceready 功能,只需将其添加到您的 main.ts(例如,如果您使用的是 cordova)

const bootstrap = () => {
  platformBrowserDynamic().bootstrapModule(AppModule);
};

if (typeof window["cordova"] !== "undefined") {
  document.addEventListener(
    "deviceready",
    () => {
      bootstrap();
    },
    false
  );
} else {
  bootstrap();
}

更多信息:https://material.angular.io/cdk/platform/api