| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <script>
- import presenceManager from '@/utils/presence-manager.js';
- export default {
- onLaunch: function () {
- console.log('=== App启动 ===');
-
- // 初始化全局在线状态
- this.initGlobalPresence();
- },
-
- onShow: function () {
- console.log('=== App显示 ===');
-
- // App从后台切换到前台时,重新连接
- this.initGlobalPresence();
- },
-
- onHide: function () {
- console.log('=== App隐藏 ===');
- // App切换到后台时,不断开连接,保持在线状态
- },
-
- methods: {
- /**
- * 初始化全局在线状态
- */
- initGlobalPresence() {
- try {
- // 获取当前登录用户ID
- const userInfo = uni.getStorageSync('userInfo');
- const userId = uni.getStorageSync('userId') || userInfo?.userId || userInfo?.id;
-
- if (userId) {
- console.log('🌐 初始化全局在线状态,用户ID:', userId);
-
- // 如果未连接,则连接
- if (!presenceManager.getConnectionStatus()) {
- presenceManager.connect(String(userId));
- console.log('✅ 全局在线状态WebSocket已连接');
- } else {
- console.log('✅ 全局在线状态WebSocket已存在');
- }
- } else {
- console.log('⚠️ 未登录,跳过在线状态初始化');
- }
- } catch (error) {
- console.error('❌ 初始化全局在线状态失败:', error);
- }
- }
- },
-
- globalData: {
-
- }
- }
- </script>
- <style lang="scss">
- /*每个页面公共css */
- page {
- min-height: 100vh;
- width: 750rpx;
- background-color: #fff;
- padding-bottom: constant(safe-area-inset-bottom);
- padding-bottom: env(safe-area-inset-bottom);
- }
- .content {
- display: flex;
- flex-direction: column;
- }
- </style>
|