app.component.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { Component } from '@angular/core';
  2. import { Platform, Events, AlertController } from '@ionic/angular';
  3. import { SplashScreen } from '@ionic-native/splash-screen/ngx';
  4. import { StatusBar } from '@ionic-native/status-bar/ngx';
  5. import { Storage } from '@ionic/storage'
  6. import { UserData } from '../providers/user-data';
  7. import { Router } from '@angular/router';
  8. import { LocalNotifications } from '@ionic-native/local-notifications/ngx';
  9. // import { WebSocketService } from '../providers/WebSocketService';
  10. import { Device } from '@ionic-native/device/ngx';
  11. import { Update } from '../providers/update'
  12. @Component({
  13. selector: 'app-root',
  14. templateUrl: 'app.component.html'
  15. })
  16. export class AppComponent {
  17. private loggedIn: boolean = false;
  18. private version_number: any
  19. constructor(
  20. private device: Device,
  21. private localNotifications: LocalNotifications,
  22. private router: Router,
  23. private events: Events,
  24. private update: Update,
  25. private userData: UserData,
  26. private storage: Storage,
  27. private platform: Platform,
  28. private alertController: AlertController,
  29. private splashScreen: SplashScreen,
  30. private statusBar: StatusBar,
  31. // private wsService: WebSocketService
  32. ) {
  33. this.initializeApp();
  34. // this.update.isUpdate()
  35. this.backButtonEvent()
  36. }
  37. ngOnInit() {
  38. this.initWebSocket()
  39. this.getVersionNumber()
  40. //WS连接的IP和端口提前保存在localStorage里,现在读出来
  41. // this.wsService.createObservableSocket(`ws://192.168.20.32:8804/websocket/403`);
  42. // this.wsService.sendMessage('testhis.fullScreenImaget')
  43. // this.getScanCode()
  44. this.checkLoginStatus()
  45. this.listenForLoginEvents()
  46. }
  47. getUserId(): Promise<string> {
  48. return this.storage.get('user_id').then((value) => {
  49. // this.update.isUpdate()
  50. return value;
  51. });
  52. }
  53. async getVersionNumber() {
  54. this.version_number = await this.update.getVersionNumber()
  55. }
  56. async initWebSocket() {
  57. let _this = this
  58. let user_id = await this.getUserId()
  59. if (user_id) {
  60. let wsUrl = `ws://dev.sgsino.cn/websocket/${user_id}`
  61. let ws = new WebSocket(wsUrl)
  62. ws.onopen = function (evt) {
  63. }
  64. ws.onmessage = function (evt) {
  65. _this.localNotifications.schedule({
  66. text: evt.data,
  67. // trigger: {at: new Date(new Date().getTime() + 3600)},
  68. led: 'FF0000',
  69. sound: _this.device.platform == 'Android' ? 'file://sound.mp3' : 'file://beep.caf',
  70. });
  71. }
  72. }
  73. }
  74. // ngAfterContentInit() {
  75. // this.appVersion.getVersionNumber().then((value: any) => {
  76. // console.log(value)
  77. // })
  78. // }
  79. ionViewDidLoad() {
  80. }
  81. getScanCode() {
  82. let _this = this
  83. let lastTime = null;
  84. let nextTime = null;
  85. let code = '';
  86. document.onkeydown = function (e) {
  87. let keycode = e.keyCode || e.which || e.charCode;
  88. nextTime = new Date();
  89. if (keycode === 13) {
  90. if (lastTime && (nextTime - lastTime < 30)) {
  91. // 扫码枪
  92. // do something
  93. } else {
  94. // 键盘
  95. // do something
  96. }
  97. code = '';
  98. lastTime = null;
  99. e.preventDefault();
  100. } else {
  101. if (!lastTime) {
  102. code = String.fromCharCode(keycode);
  103. } else {
  104. if (nextTime - lastTime < 30) {
  105. code += String.fromCharCode(keycode);
  106. } else {
  107. code = '';
  108. }
  109. }
  110. lastTime = nextTime;
  111. }
  112. if (code.split('_').length === 2) {
  113. _this.router.navigateByUrl('/enter-store')
  114. _this.storage.set('pId', code.split('_')[0])
  115. _this.storage.set('sscId', code.split('_')[1])
  116. }
  117. console.log(code)
  118. }
  119. }
  120. checkLoginStatus() {
  121. return this.userData.isLoggedIn().then(loggedIn => {
  122. return this.updateLoggedInStatus(loggedIn);
  123. });
  124. }
  125. updateLoggedInStatus(loggedIn: boolean) {
  126. setTimeout(() => {
  127. this.loggedIn = loggedIn;
  128. }, 300);
  129. }
  130. listenForLoginEvents() {
  131. this.events.subscribe('user:login', () => {
  132. this.initWebSocket()
  133. this.updateLoggedInStatus(true);
  134. });
  135. this.events.subscribe('user:logout', () => {
  136. this.updateLoggedInStatus(false);
  137. });
  138. }
  139. async logout() {
  140. this.userData.logout()
  141. }
  142. initializeApp() {
  143. this.platform.ready().then(() => {
  144. this.statusBar.styleDefault();
  145. this.splashScreen.hide();
  146. });
  147. }
  148. //android通过返回按钮退出应用
  149. lastTimeBackPress = 0;
  150. timePeriodToExit = 2000;
  151. backButtonEvent() {
  152. this.platform.backButton.subscribe(() => {
  153. if (this.router.url.indexOf('home') > -1 || this.router.url.indexOf('login') > -1 || this.router.url.indexOf('tab3') > -1) {
  154. if (new Date().getTime() - this.lastTimeBackPress < this.timePeriodToExit) {
  155. navigator['app'].exitApp(); //退出APP
  156. } else {
  157. this.presentAlertConfirm();
  158. this.lastTimeBackPress = new Date().getTime();
  159. }
  160. // navigator['app'].exitApp(); //ionic4 退出APP的方法
  161. }
  162. })
  163. }
  164. async presentAlertConfirm() {
  165. const alert = await this.alertController.create({
  166. // header: 'Confirm!',
  167. message: '您要退出APP吗?',
  168. buttons: [
  169. {
  170. text: '取消',
  171. role: 'cancel',
  172. cssClass: 'secondary',
  173. handler: (blah) => {
  174. }
  175. }, {
  176. text: '退出',
  177. handler: () => {
  178. navigator['app'].exitApp();
  179. }
  180. }
  181. ]
  182. });
  183. await alert.present();
  184. }
  185. }