【问题标题】:How can I use cordova device ready function inside http interceptor如何在http拦截器中使用cordova设备就绪功能
【发布时间】:2020-02-23 07:21:43
【问题描述】:

我正在将我的 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 服务?谢谢。

【问题讨论】:

  • 试试这个 console.log(device.uuid); console.log(device.model);
  • 但我们必须在此处添加设备就绪事件,因此我将其投入使用

标签: angular cordova cordova-plugins angular8 angular-http-interceptors


【解决方案1】:

只需使用@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

【讨论】:

    猜你喜欢
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 2015-07-01
    • 2015-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多