import {Injectable} from '@angular/core'; import {DeviceInfoInterface} from '../../core/interfaces/device-info.interface'; import {Platform} from '@ionic/angular'; import {AppVersion} from '@ionic-native/app-version/ngx'; import {Device} from '@ionic-native/device/ngx'; import {map} from 'rxjs/operators'; import {ScreenService} from '../../core/app-error/service/screen-service'; import {PlatFormInfo} from '../../core/app-error/models/plat_form_info'; import {M2Logger} from '../../core/services/m2.logger'; import {ApiAnon} from '../../core/services/http/apiAnon'; import {ResultModel} from '../../models/result-model'; import {PLATFORM} from '../../core/enum/platform'; import {DeviceInfoBrowserService} from '../../core/app-error/service/device-info.browser.service'; import {AppData} from '../../../df-ext/app-data'; @Injectable() export class DeviceInfoNativeService implements DeviceInfoInterface { private deviceReady: Promise; private platformInfo: PlatFormInfo; constructor( private device: Device, private platform: Platform, private appVersion: AppVersion, private m2log: M2Logger, private apiAnon: ApiAnon, private screenService: ScreenService, private deviceInfoBrowserService: DeviceInfoBrowserService) { this.deviceInit(); } getPlatformInfo(): PlatFormInfo { return this.platformInfo; } ready() { return this.deviceReady; } reload() { this.deviceInit(); return this.deviceReady; } // 初始化设备信息 deviceInit() { this.platformInfo = new PlatFormInfo(); // 设置强制升级标志,默认为false this.platformInfo.forceUpgrade = false; this.deviceReady = new Promise((resolve, reject) => { this.platform.ready().then(() => { const isCordova = this.platform.is('cordova'); if (isCordova) { this.platformInfo.appId = AppData.config.APP_ID; this.getScreenInfo(); this.platformInfo.cordovaVersion = this.device.cordova; this.platformInfo.isCordova = isCordova; this.platformInfo.manufacturer = this.device.manufacturer; this.platformInfo.model = this.device.model; this.platformInfo.osName = this.device.platform; this.platformInfo.osVersion = this.device.version; this.platformInfo.uuid = this.device.uuid; this.platformInfo.isVirtual = this.device.isVirtual; this.platformInfo.serial = this.device.serial; this.appVersion.getAppName().then((value: string) => { this.platformInfo.appName = value; }); // 平台判断 if (this.platformInfo.platform == null) { if (this.platform.is('android')) { this.platformInfo.platform = PLATFORM.Android; } else if (this.platform.is('ios')) { this.platformInfo.platform = PLATFORM.IOS; } else { this.platformInfo.platform = PLATFORM.Browser; } } this.platformInfo.runtimeEnvironment = 'APP'; this.appVersion.getVersionNumber().then(value => { this.platformInfo.appVersion = value; const queryParam = { platform: this.platformInfo.platform, appId: AppData.config.APP_ID, version: this.platformInfo.appVersion, }; this.apiAnon.get('app_upgrade/last_version', queryParam).pipe(map((resp: any) => resp as ResultModel)) .subscribe((data: ResultModel) => { if (data.code === 1) { if (data.result.ifForce === '1') { this.platformInfo.forceUpgrade = true; } this.platformInfo.remoteVersion = data.result.version; this.platformInfo.upgradeContent = data.result.upgradeContent; resolve(this.platformInfo); } }, error2 => { reject(error2); } ); }); } else { this.deviceInfoBrowserService.reload().then(item => { this.platformInfo = item; resolve(this.platformInfo); }); } }); }); } // 屏幕信息 getScreenInfo() { this.platformInfo.screenWidth = this.screenService.screenInfo.screenWidth; this.platformInfo.screenHeight = this.screenService.screenInfo.screenHeight; this.platformInfo.deviceWidth = this.screenService.screenInfo.deviceWidth; this.platformInfo.deviceHeight = this.screenService.screenInfo.deviceHeight; this.platformInfo.devicePixelRatio = this.screenService.screenInfo.devicePixelRatio; } }