import { Component } from '@angular/core'; import { Platform, Events, AlertController } from '@ionic/angular'; import { SplashScreen } from '@ionic-native/splash-screen/ngx'; import { StatusBar } from '@ionic-native/status-bar/ngx'; import { Storage } from '@ionic/storage' import { UserData } from '../providers/user-data'; import { Router } from '@angular/router'; import { LocalNotifications } from '@ionic-native/local-notifications/ngx'; // import { WebSocketService } from '../providers/WebSocketService'; import { Device } from '@ionic-native/device/ngx'; import { Update } from '../providers/update' @Component({ selector: 'app-root', templateUrl: 'app.component.html' }) export class AppComponent { private loggedIn: boolean = false; private version_number: any constructor( private device: Device, private localNotifications: LocalNotifications, private router: Router, private events: Events, private update: Update, private userData: UserData, private storage: Storage, private platform: Platform, private alertController: AlertController, private splashScreen: SplashScreen, private statusBar: StatusBar, // private wsService: WebSocketService ) { this.initializeApp(); // this.update.isUpdate() this.backButtonEvent() } ngOnInit() { this.initWebSocket() this.getVersionNumber() //WS连接的IP和端口提前保存在localStorage里,现在读出来 // this.wsService.createObservableSocket(`ws://192.168.20.32:8804/websocket/403`); // this.wsService.sendMessage('testhis.fullScreenImaget') // this.getScanCode() this.checkLoginStatus() this.listenForLoginEvents() } getUserId(): Promise { return this.storage.get('user_id').then((value) => { // this.update.isUpdate() return value; }); } async getVersionNumber() { this.version_number = await this.update.getVersionNumber() } async initWebSocket() { let _this = this let user_id = await this.getUserId() if (user_id) { let wsUrl = `ws://dev.sgsino.cn/websocket/${user_id}` let ws = new WebSocket(wsUrl) ws.onopen = function (evt) { } ws.onmessage = function (evt) { _this.localNotifications.schedule({ text: evt.data, // trigger: {at: new Date(new Date().getTime() + 3600)}, led: 'FF0000', sound: _this.device.platform == 'Android' ? 'file://sound.mp3' : 'file://beep.caf', }); } } } // ngAfterContentInit() { // this.appVersion.getVersionNumber().then((value: any) => { // console.log(value) // }) // } ionViewDidLoad() { } getScanCode() { let _this = this let lastTime = null; let nextTime = null; let code = ''; document.onkeydown = function (e) { let keycode = e.keyCode || e.which || e.charCode; nextTime = new Date(); if (keycode === 13) { if (lastTime && (nextTime - lastTime < 30)) { // 扫码枪 // do something } else { // 键盘 // do something } code = ''; lastTime = null; e.preventDefault(); } else { if (!lastTime) { code = String.fromCharCode(keycode); } else { if (nextTime - lastTime < 30) { code += String.fromCharCode(keycode); } else { code = ''; } } lastTime = nextTime; } if (code.split('_').length === 2) { _this.router.navigateByUrl('/enter-store') _this.storage.set('pId', code.split('_')[0]) _this.storage.set('sscId', code.split('_')[1]) } console.log(code) } } checkLoginStatus() { return this.userData.isLoggedIn().then(loggedIn => { return this.updateLoggedInStatus(loggedIn); }); } updateLoggedInStatus(loggedIn: boolean) { setTimeout(() => { this.loggedIn = loggedIn; }, 300); } listenForLoginEvents() { this.events.subscribe('user:login', () => { this.initWebSocket() this.updateLoggedInStatus(true); }); this.events.subscribe('user:logout', () => { this.updateLoggedInStatus(false); }); } async logout() { this.userData.logout() } initializeApp() { this.platform.ready().then(() => { this.statusBar.styleDefault(); this.splashScreen.hide(); }); } //android通过返回按钮退出应用 lastTimeBackPress = 0; timePeriodToExit = 2000; backButtonEvent() { this.platform.backButton.subscribe(() => { if (this.router.url.indexOf('home') > -1 || this.router.url.indexOf('login') > -1 || this.router.url.indexOf('tab3') > -1) { if (new Date().getTime() - this.lastTimeBackPress < this.timePeriodToExit) { navigator['app'].exitApp(); //退出APP } else { this.presentAlertConfirm(); this.lastTimeBackPress = new Date().getTime(); } // navigator['app'].exitApp(); //ionic4 退出APP的方法 } }) } async presentAlertConfirm() { const alert = await this.alertController.create({ // header: 'Confirm!', message: '您要退出APP吗?', buttons: [ { text: '取消', role: 'cancel', cssClass: 'secondary', handler: (blah) => { } }, { text: '退出', handler: () => { navigator['app'].exitApp(); } } ] }); await alert.present(); } }