App.vue 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <script>
  2. import presenceManager from '@/utils/presence-manager.js';
  3. export default {
  4. onLaunch: function () {
  5. console.log('=== App启动 ===');
  6. // 初始化全局在线状态
  7. this.initGlobalPresence();
  8. },
  9. onShow: function () {
  10. console.log('=== App显示 ===');
  11. // App从后台切换到前台时,重新连接
  12. this.initGlobalPresence();
  13. },
  14. onHide: function () {
  15. console.log('=== App隐藏 ===');
  16. // App切换到后台时,不断开连接,保持在线状态
  17. },
  18. methods: {
  19. /**
  20. * 初始化全局在线状态
  21. */
  22. initGlobalPresence() {
  23. try {
  24. // 获取当前登录用户ID
  25. const userInfo = uni.getStorageSync('userInfo');
  26. const userId = uni.getStorageSync('userId') || userInfo?.userId || userInfo?.id;
  27. if (userId) {
  28. console.log('🌐 初始化全局在线状态,用户ID:', userId);
  29. // 如果未连接,则连接
  30. if (!presenceManager.getConnectionStatus()) {
  31. presenceManager.connect(String(userId));
  32. console.log('✅ 全局在线状态WebSocket已连接');
  33. } else {
  34. console.log('✅ 全局在线状态WebSocket已存在');
  35. }
  36. } else {
  37. console.log('⚠️ 未登录,跳过在线状态初始化');
  38. }
  39. } catch (error) {
  40. console.error('❌ 初始化全局在线状态失败:', error);
  41. }
  42. }
  43. },
  44. globalData: {
  45. }
  46. }
  47. </script>
  48. <style lang="scss">
  49. /*每个页面公共css */
  50. page {
  51. min-height: 100vh;
  52. width: 750rpx;
  53. background-color: #fff;
  54. padding-bottom: constant(safe-area-inset-bottom);
  55. padding-bottom: env(safe-area-inset-bottom);
  56. }
  57. .content {
  58. display: flex;
  59. flex-direction: column;
  60. }
  61. </style>