App.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <script>
  2. import timPresenceManager from '@/utils/tim-presence-manager.js';
  3. import timManager from '@/utils/tim-manager.js';
  4. export default {
  5. onLaunch: function () {
  6. console.log('=== App启动 ===');
  7. // 初始化TIM(全局)
  8. this.initGlobalTIM();
  9. },
  10. onShow: function () {
  11. console.log('=== App显示 ===');
  12. // 延迟检查TIM连接状态,避免在登录过程中重复调用
  13. setTimeout(() => {
  14. this.checkTIMConnection();
  15. }, 1000);
  16. },
  17. onHide: function () {
  18. console.log('=== App隐藏 ===');
  19. // App切换到后台时,不断开连接,保持在线状态
  20. },
  21. methods: {
  22. /**
  23. * 初始化全局TIM
  24. */
  25. async initGlobalTIM() {
  26. try {
  27. // 获取当前登录用户ID
  28. const userInfo = uni.getStorageSync('userInfo');
  29. const userId = uni.getStorageSync('userId') || userInfo?.userId || userInfo?.id;
  30. if (!userId) {
  31. console.log('⚠️ 未登录,跳过TIM初始化');
  32. return;
  33. }
  34. // 如果TIM已经登录且用户ID匹配,跳过
  35. if (timManager.isLogin && String(timManager.userId) === String(userId)) {
  36. console.log('✅ TIM已登录,用户ID:', userId);
  37. return;
  38. }
  39. console.log('🚀 开始初始化全局TIM,用户ID:', userId);
  40. // 初始化TIM SDK(只在首次初始化)
  41. const SDKAppID = 1600109674;
  42. if (!timManager.tim) {
  43. timManager.init(SDKAppID);
  44. console.log('✅ TIM SDK 初始化完成');
  45. }
  46. // 如果用户ID不匹配,先登出旧用户
  47. if (timManager.isLogin && timManager.userId && String(timManager.userId) !== String(userId)) {
  48. console.log('⚠️ 检测到用户切换,先登出旧用户...');
  49. try {
  50. await timManager.logout();
  51. console.log('✅ 旧用户已登出');
  52. } catch (e) {
  53. console.warn('⚠️ 登出失败,继续登录新用户:', e.message);
  54. }
  55. }
  56. // 获取UserSig
  57. const res = await uni.request({
  58. url: `http://localhost:8083/api/im/getUserSig?userId=${userId}`,
  59. method: 'GET'
  60. });
  61. if (res[1].data.code === 200) {
  62. const userSig = res[1].data.data.userSig;
  63. // 登录TIM
  64. await timManager.login(String(userId), userSig);
  65. console.log('✅ 全局TIM登录成功');
  66. // TIM 登录成功后,初始化在线状态管理器
  67. await this.initPresenceManager(userId);
  68. } else {
  69. throw new Error('获取UserSig失败');
  70. }
  71. } catch (error) {
  72. console.error('❌ 初始化全局TIM失败:', error);
  73. }
  74. },
  75. /**
  76. * 初始化在线状态管理器
  77. */
  78. async initPresenceManager(userId) {
  79. try {
  80. console.log('🌐 初始化在线状态管理器,用户ID:', userId);
  81. // 初始化 TIM + WebSocket 混合状态管理器
  82. await timPresenceManager.init(String(userId));
  83. console.log('✅ 在线状态管理器初始化成功');
  84. } catch (error) {
  85. console.error('❌ 初始化在线状态管理器失败:', error);
  86. }
  87. },
  88. /**
  89. * 检查TIM连接状态
  90. */
  91. checkTIMConnection() {
  92. if (timManager.isLogin) {
  93. console.log('✅ TIM连接正常');
  94. } else {
  95. console.log('⚠️ TIM未连接,尝试重新初始化');
  96. this.initGlobalTIM();
  97. }
  98. }
  99. },
  100. globalData: {
  101. }
  102. }
  103. </script>
  104. <style lang="scss">
  105. /*每个页面公共css */
  106. page {
  107. min-height: 100vh;
  108. width: 750rpx;
  109. background-color: #fff;
  110. padding-bottom: constant(safe-area-inset-bottom);
  111. padding-bottom: env(safe-area-inset-bottom);
  112. }
  113. .content {
  114. display: flex;
  115. flex-direction: column;
  116. }
  117. </style>