// tslint:disable import {Injectable, QueryList, ViewChildren} from '@angular/core'; import {AppVersion} from '@ionic-native/app-version/ngx'; import {ResultModel} from '../../models/result-model'; import {ScreenOrientation} from '@ionic-native/screen-orientation/ngx'; import {SplashScreen} from '@ionic-native/splash-screen/ngx'; import {StatusBar} from '@ionic-native/status-bar/ngx'; import { ActionSheetController, Config, IonRouterOutlet, MenuController, ModalController, Platform, PopoverController, ToastController } from '@ionic/angular'; import {ApiAnon} from '../../core/services/http/apiAnon'; import {M2Logger} from '../../core/services/m2.logger'; import {Device} from '@ionic-native/device/ngx'; import {map} from 'rxjs/operators'; import {Router} from '@angular/router'; import {M2Toast} from '../../layout/setting/m2.toast'; import {Location} from '@angular/common'; import {TabsUtilsService} from '../../core/services/menu/tabs-utils.service'; import {AppData} from '../../../df-ext/app-data'; @Injectable() export class NativeService { private _ready: Promise; // 真机运行版本 private _localVersion: any; // 远程最新版本 private _remoteVersion: any; // 最新版本是否强制升级 private _forceUpgrade: boolean; // 最新版本升级内容 private _upgradeContent: string; // 设备信息 private _cordovaVersion: string; private _model: string; private _osName: string; private _uuid: string; private _osVersion: string; private _manufacturer: string; private _isVirtual: boolean; private _serial: string; // 平台统一名称:Android,IOS,Windows, Browser private _platformName: string; // 对android手机回退按键的处理 lastTimeBackPress = 0; timePeriodToExit = 3000; constructor(private platform: Platform, private m2log: M2Logger, private apiAnon: ApiAnon, private m2Toast: M2Toast, private appVersion: AppVersion, private config: Config, private statusBar: StatusBar, private device: Device, private splashScreen: SplashScreen, private screenOrientation: ScreenOrientation, public modalCtrl: ModalController, private menu: MenuController, private actionSheetCtrl: ActionSheetController, private popoverCtrl: PopoverController, private router: Router, private tabsUtilsService: TabsUtilsService, private location: Location) { } get localVersion(): any { return this._localVersion; } get platformName(): string { return this._platformName; } get remoteVersion(): any { return this._remoteVersion; } get forceUpgrade(): boolean { return this._forceUpgrade; } get upgradeContent(): string { return this._upgradeContent; } get model(): string { return this._model; } get cordovaVersion(): string { return this._cordovaVersion; } get osName(): string { return this._osName; } get uuid(): string { return this._uuid; } get osVersion(): string { return this._osVersion; } get manufacturer(): string { return this._manufacturer; } get isVirtual(): boolean { return this._isVirtual; } get serial(): string { return this._serial; } ready() { return this._ready; } reload() { this._init(); return this._ready; } private _init() { // 平台判断 if (this.platform.is('ios')) { this._platformName = 'IOS'; } else if (this.platform.is('android')) { this._platformName = 'Android'; } else { this._platformName = 'Browser'; } // 设置强制升级标志,默认为false this._forceUpgrade = false; this._ready = new Promise((resolve, reject) => { this.platform.ready().then(() => { if (!AppData.config.IS_DEBUG) { // 如果是真机模式,则准备app运行版本,并获取远程服务器最新版本及其相关升级信息 this._cordovaVersion = this.device.cordova; this._manufacturer = this.device.manufacturer; this._model = this.device.model; this._osName = this.device.platform; this._osVersion = this.device.version; this._uuid = this.device.uuid; this._isVirtual = this.device.isVirtual; this._serial = this.device.serial; this.appVersion.getVersionNumber().then(value => { this._localVersion = value; this.m2log.log('系统运行版本获取完毕'); const queryParam = { platform: this._platformName, appId: AppData.config.APP_ID, version: this._localVersion, }; 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._forceUpgrade = true; } this._remoteVersion = data.result.version; this._upgradeContent = data.result.upgradeContent; this.m2log.log('远程最新版本获取完毕'); resolve(value); } }, error2 => { reject(error2); } ); }); } else { // 如果是浏览器运行,则将app运行版本和远程服务器最新版本都设为1.0.0 this._localVersion = '1.0.0'; this._remoteVersion = '1.0.0'; this._platformName = 'Browser'; this.m2log.log('系统在浏览器下运行!'); resolve(this._localVersion); } }); }); } appSetting() { if (!AppData.config.IS_DEBUG) { // 真机运行才设置状态等信息 // this.statusBar.styleDefault(); // this.statusBar.styleLightContent(); // this.statusBar.overlaysWebView(true); // this.statusBar.show(); this.statusBar.hide(); // this.statusBar.backgroundColorByHexString('#3171e0'); // this.statusBar.overlaysWebView(true); this.screenOrientation.lock(this.screenOrientation.ORIENTATIONS.PORTRAIT) .then(value => { }) .catch(reason => console.log(reason)); // 对于android机型设置回退按键事件 if (this.platform.is('android')) { this._exitApp(); } } // 设置ionic全局行为 this.config.set('backButtonText', '返回'); } hideSplash() { if (!AppData.config.IS_DEBUG) { this.splashScreen.hide(); } } private _exitApp() { // this.platform.backButton.subscribe(() => { // // code that is executed when the user pressed the back button // console.log("backButton.subscribe"); // }); this.platform.backButton.subscribeWithPriority(0, async () => { console.log('back!!!'); // close action sheet try { const element = await this.actionSheetCtrl.getTop(); if (element) { element.dismiss(); return; } } catch (error) { } // close popover try { const element = await this.popoverCtrl.getTop(); if (element) { element.dismiss(); return; } } catch (error) { } // close modal try { const element = await this.modalCtrl.getTop(); if (element) { element.dismiss(); return; } } catch (error) { console.log(error); } // close side menua try { const element = await this.menu.getOpen(); if (element) { this.menu.close(); return; } } catch (error) { } if (!this._checkRootPage(this.location.path())) { this.location.back(); } else { if (new Date().getTime() - this.lastTimeBackPress < this.timePeriodToExit) { // this.platform.exitApp(); // Exit from app (navigator as any).app.exitApp(); // work in ionic 4 } else { this.m2Toast.presentToast('再按一下退出应用!').then(() => { this.lastTimeBackPress = new Date().getTime(); }); } } // routerOutlets.forEach((outlet: IonRouterOutlet) => { // console.log(outlet); // console.log(outlet.canGoBack() ); // if (outlet && outlet.canGoBack()) { // outlet.pop(); // // } else if (this.router.url === '/home') { // if (new Date().getTime() - this.lastTimeBackPress < this.timePeriodToExit) { // // this.platform.exitApp(); // Exit from app // navigator['app'].exitApp(); // work in ionic 4 // // } else { // this.m2Toast.presentToast("再按一下退出应用!").then( () => { // this.lastTimeBackPress = new Date().getTime(); // }); // } // } // }); }); } private _checkRootPage(url: string): boolean { const index = this.tabsUtilsService.getTabs().findIndex(menu => '/m2/' + menu.navUrl === url); return index !== -1 ? true : false; } }