| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <script>
- import timPresenceManager from '@/utils/tim-presence-manager.js';
- import timManager from '@/utils/tim-manager.js';
- export default {
- data() {
- return {
- _timInitializing: false
- }
- },
- onLaunch: function () {
- console.log('=== App启动 ===');
-
- // 初始化TIM(全局)
- this.initGlobalTIM();
-
- // 延迟挂载全局方法(等待 App 实例创建完成)
- setTimeout(() => {
- const app = getApp();
- if (app) {
- app.globalData.initGlobalTIM = this.initGlobalTIM.bind(this);
- app.globalData.initPresenceManager = this.initPresenceManager.bind(this);
- console.log('✅ 全局方法挂载成功');
- }
- }, 100);
- },
-
- onShow: function () {
- console.log('=== App显示 ===');
- console.log(' 当前时间:', new Date().toLocaleTimeString());
-
- // 延迟检查TIM连接状态,避免在登录过程中重复调用
- setTimeout(() => {
- console.log('⏰ 开始执行 checkTIMConnection...');
- this.checkTIMConnection();
- }, 500); // 缩短延迟时间到 500ms
- },
-
- onHide: function () {
- console.log('=== App隐藏 ===');
- // App切换到后台时,不断开连接,保持在线状态
- },
-
- methods: {
-
- /**
- * 初始化全局TIM
- */
- async initGlobalTIM() {
- // 避免在登录过程中被重复触发
- if (this._timInitializing) {
- return
- }
- this._timInitializing = true
- try {
- // 获取当前登录用户ID
- const userInfo = uni.getStorageSync('userInfo');
- const userId = uni.getStorageSync('userId') || userInfo?.userId || userInfo?.id;
-
- if (!userId) {
- console.log('⚠️ 未登录,跳过TIM初始化');
- return;
- }
-
- // 如果TIM已经登录且用户ID匹配,跳过
- if (timManager.isLogin && String(timManager.userId) === String(userId)) {
- console.log('✅ TIM已登录,用户ID:', userId);
- return;
- }
-
- console.log('?? 开始初始化全局TIM,用户ID:', userId);
-
- // 初始化TIM SDK(只在首次初始化)
- const SDKAppID = 1600109674;
- if (!timManager.tim) {
- timManager.init(SDKAppID);
- console.log('✅ TIM SDK 初始化完成');
- }
-
- // 如果用户ID不匹配,先登出旧用户
- if (timManager.isLogin && timManager.userId && String(timManager.userId) !== String(userId)) {
- console.log('⚠️ 检测到用户切换,先登出旧用户...');
- try {
- await timManager.logout();
- console.log('✅ 旧用户已登出');
- } catch (e) {
- console.warn('⚠️ 登出失败,继续登录新用户:', e.message);
- }
- }
-
- // 获取UserSig
- const res = await uni.request({
- url: `http://localhost:8083/api/im/getUserSig?userId=${userId}`,
- method: 'GET'
- });
-
- if (res[1].data.code === 200) {
- const userSig = res[1].data.data.userSig;
-
- // 登录TIM
- await timManager.login(String(userId), userSig);
- console.log('✅ 全局TIM登录成功');
-
- // TIM 登录成功后,初始化在线状态管理器
- await this.initPresenceManager(userId);
- } else {
- throw new Error('获取UserSig失败');
- }
- } catch (error) {
- console.error('❌ 初始化全局TIM失败:', error);
- } finally {
- this._timInitializing = false
- }
- },
-
- /**
- * 初始化在线状态管理器
- */
- async initPresenceManager(userId) {
- try {
- console.log('?? 初始化在线状态管理器,用户ID:', userId);
-
- // 初始化 TIM + WebSocket 混合状态管理器
- await timPresenceManager.init(String(userId));
-
- console.log('✅ 在线状态管理器初始化成功');
- } catch (error) {
- console.error('❌ 初始化在线状态管理器失败:', error);
- }
- },
-
- /**
- * 检查TIM连接状态和WebSocket连接状态
- */
- checkTIMConnection() {
- console.log('?? ========== 开始检查连接状态 ==========');
-
- // 获取当前登录用户ID
- const userId = uni.getStorageSync('userId');
- console.log(' 当前用户ID:', userId);
-
- if (!userId) {
- console.log('⚠️ 未登录,跳过连接检查');
- console.log('=========================================');
- return;
- }
-
- // 检查 TIM 是否登录
- console.log(' TIM 登录状态:', timManager.isLogin);
- console.log(' TIM 用户ID:', timManager.userId);
-
- if (!timManager.isLogin) {
- console.log('⚠️ TIM未登录,尝试重新初始化');
- console.log('=========================================');
- this.initGlobalTIM();
- return;
- }
-
- // 检查 WebSocket 是否连接
- console.log(' WebSocket 连接状态:', timPresenceManager.isConnected);
-
- if (!timPresenceManager.isConnected) {
- console.log('⚠️ WebSocket未连接,尝试重新初始化在线状态管理器');
- console.log('=========================================');
- this.initPresenceManager(userId);
- } else {
- console.log('✅ TIM和WebSocket连接正常');
- console.log('=========================================');
- }
- }
- },
-
- 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>
|