App.vue 3.4 KB

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