|
|
@@ -19,8 +19,10 @@ export default {
|
|
|
// App从后台切换到前台时,重新连接
|
|
|
this.initGlobalPresence();
|
|
|
|
|
|
- // 确保TIM连接正常
|
|
|
- this.checkTIMConnection();
|
|
|
+ // 延迟检查TIM连接状态,避免在登录过程中重复调用
|
|
|
+ setTimeout(() => {
|
|
|
+ this.checkTIMConnection();
|
|
|
+ }, 1000);
|
|
|
},
|
|
|
|
|
|
onHide: function () {
|
|
|
@@ -77,28 +79,40 @@ export default {
|
|
|
}
|
|
|
|
|
|
console.log('🚀 开始初始化全局TIM,用户ID:', userId);
|
|
|
-
|
|
|
- // 初始化TIM SDK
|
|
|
- const SDKAppID = 1600045677;
|
|
|
- if (!timManager.tim) {
|
|
|
- timManager.init(SDKAppID);
|
|
|
+
|
|
|
+ // 初始化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;
|
|
|
|
|
|
- // 获取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登录成功');
|
|
|
- } else {
|
|
|
- throw new Error('获取UserSig失败');
|
|
|
- }
|
|
|
+ // 登录TIM
|
|
|
+ await timManager.login(String(userId), userSig);
|
|
|
+ console.log('✅ 全局TIM登录成功');
|
|
|
+ } else {
|
|
|
+ throw new Error('获取UserSig失败');
|
|
|
+ }
|
|
|
} catch (error) {
|
|
|
console.error('❌ 初始化全局TIM失败:', error);
|
|
|
}
|