统一排行榜对齐星雀UI,页面资源后台配置实时刷新

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
XingQue
2026-06-27 01:26:11 +08:00
parent 1ab2e2080a
commit 21112173f2
292 changed files with 64010 additions and 81 deletions

View File

@@ -2,7 +2,7 @@
import GoEasy from '../static/lib/goeasy-2.13.24.esm.min';
import { jianquanxian } from './imAuth/jianquanxian';
import { parseOrderCardText } from './group-chat.js';
import { persistGroupMeta } from './im-user.js';
import { persistGroupMeta, getFreshImUser } from './im-user.js';
let _globalPrivateHandler = null;
let _globalGroupHandler = null;
@@ -28,9 +28,27 @@ function initGlobalMessageSystem(app) {
loadUserSettings(app);
initNetworkListener(app);
initAppStateListener(app);
checkAndRestoreConnection(app);
bindGoEasyLifecycle(app);
setupEventSystem(app);
setupMessageListeners(app);
// 监听器在连接成功 / onShow 时注册,不在 IM 未就绪时空绑
checkAndRestoreConnection(app);
}
function bindGoEasyLifecycle(app) {
if (!wx.goEasy || app._goEasyLifecycleBound) return;
app._goEasyLifecycleBound = true;
try {
wx.goEasy.on('connected', () => {
app.globalData.goEasyConnection.status = 'connected';
setupMessageListeners(app);
loadConversations(app);
});
wx.goEasy.on('disconnected', () => {
app.globalData.goEasyConnection.status = 'disconnected';
});
} catch (e) {
console.warn('绑定 GoEasy 生命周期失败:', e);
}
}
function getCurrentGoEasyUserId() {
@@ -78,12 +96,8 @@ function initNetworkListener(app) {
function initAppStateListener(app) {
wx.onAppShow(() => {
ensureConnection(app);
updateCurrentPageState(app);
// ✅ 连接正常时,主动刷一次未读角标
if (wx.goEasy.getConnectionStatus && wx.goEasy.getConnectionStatus() === 'connected') {
loadConversations(app);
}
ensureConnection(app);
});
wx.onAppHide(() => {
pauseHeartbeat(app);
@@ -101,25 +115,34 @@ function checkAndRestoreConnection(app) {
const validityHours = app.globalData.goEasyConnection.config.cacheValidityHours || 12;
if (hoursDiff > validityHours) {
clearSavedConnection(app);
} else {
ensureConnection(app);
}
}
ensureConnection(app);
} catch (error) {
console.warn('检查保存的连接信息失败:', error);
}
}
function setupEventSystem(app) {
app.globalData.eventListeners = {};
if (!app.globalData.eventListeners) {
app.globalData.eventListeners = {};
}
}
// 🔥 修改点:不依赖 saved 缓存,只要连接状态正常就刷新角标
function isImConnected() {
if (!wx.goEasy?.getConnectionStatus) return false;
const s = wx.goEasy.getConnectionStatus();
return s === 'connected' || s === 'reconnected';
}
// 进入小程序即尝试连接并监听(不依赖历史缓存)
function ensureConnection(app) {
const connectionStatus = wx.goEasy.getConnectionStatus ? wx.goEasy.getConnectionStatus() : 'disconnected';
if (connectionStatus === 'connected' || connectionStatus === 'reconnected') {
if (!wx.goEasy?.connect) return;
if (app.globalData.chatEnabled === false) return;
if (isImConnected()) {
setupMessageListeners(app);
loadConversations(app); // 每次进入前台都刷新未读
loadConversations(app);
return;
}
@@ -206,9 +229,21 @@ function disconnectGoEasy(app) {
}
async function connectWithIdentity(app, identityType, userId, isAutoRestore = false) {
const quanxian = await jianquanxian(app);
if (!quanxian.allowed) {
return Promise.reject(new Error(quanxian.reason || '无聊天权限'));
if (!wx.goEasy?.connect) {
return Promise.reject(new Error('聊天服务未就绪'));
}
// 冷启动/后台恢复:先连上 IM 才能收消息;完整鉴权留给消息页/发消息
if (!isAutoRestore) {
const quanxian = await jianquanxian(app);
if (!quanxian.allowed) {
return Promise.reject(new Error(quanxian.reason || '无聊天权限'));
}
} else {
const uid = wx.getStorageSync('uid');
if (!uid) {
return Promise.reject(new Error('未登录'));
}
}
app.globalData.goEasyConnection.autoReconnect = true;
@@ -302,7 +337,7 @@ function connectForCurrentRole(app) {
if (currentUserId) {
wx.goEasy.disconnect({ onSuccess: () => {}, onFailed: () => {} });
}
connectWithIdentity(app, role, targetUserId, false);
connectWithIdentity(app, role, targetUserId, true);
}
async function switchRoleAndReconnect(app, newRole) {
@@ -577,7 +612,7 @@ function cacheMessage(app, message) {
}
function loadConversations(app) {
if (!getCurrentGoEasyUserId()) return;
if (!getCurrentGoEasyUserId() || !wx.goEasy?.im?.latestConversations) return;
wx.goEasy.im.latestConversations({
onSuccess: (result) => {
if (result.unreadTotal !== undefined) {