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