App.vue 875 B

123456789101112131415161718192021222324252627282930313233343536
  1. <script>
  2. import { getToken, setToken, getCurrentUserInfo } from './utils/api'
  3. import { useWebSocket } from './composables/useWebSocket'
  4. import { setupAppNotifications } from './utils/notificationSetup'
  5. export default {
  6. onLaunch: async function() {
  7. try {
  8. const token = getToken()
  9. if (!token) {
  10. uni.reLaunch({ url: '/pages/login/index' })
  11. return
  12. }
  13. await getCurrentUserInfo(token)
  14. // token 有效时建立 WebSocket,保证任意页面都能实时收消息(含聊天页)
  15. useWebSocket(() => {}).connect()
  16. // #ifdef APP-PLUS
  17. setupAppNotifications()
  18. // #endif
  19. } catch (e) {
  20. setToken('')
  21. uni.reLaunch({ url: '/pages/login/index' })
  22. }
  23. },
  24. onShow: function() {
  25. console.log('App Show')
  26. },
  27. onHide: function() {
  28. console.log('App Hide')
  29. }
  30. }
  31. </script>
  32. <style>
  33. /*每个页面公共css */
  34. </style>