update:恢复ws电量通知代码

This commit is contained in:
2026-09-23 14:22:57 +08:00
parent bad3c8ff9a
commit fd6be25ba5
+38 -20
View File
@@ -87,6 +87,22 @@ function createWebSocket(token, onMessage) {
const { data, event, code, timestamp } = response || {};
if (event === "pong") return;
const passthroughEvents = [
"/addons/shoot/battery",
"/addons/shoot/otaProgress",
"/addons/shoot/otaResult",
];
if (passthroughEvents.includes(event)) {
if (
(code == null || Number(code) === 0) &&
onMessage &&
data &&
typeof data === "object"
) {
onMessage({ event, data, code, timestamp });
}
return;
}
if (!data || typeof data !== "object") return;
if (data.type) {
if (ENABLE_REALTIME_MESSAGE_LOG) {
@@ -97,27 +113,29 @@ function createWebSocket(token, onMessage) {
}
if (!Array.isArray(data.updates)) return;
if (onMessage) onMessage(data.updates);
const msg = data.updates[0];
if (msg) {
if (ENABLE_REALTIME_MESSAGE_LOG) {
console.log(
"收到 WebSocket 更新",
getMessageTypeName(msg.constructor),
{ updateCount: data.updates.length }
);
}
if (msg.constructor === MESSAGETYPES.RankUpdate) {
uni.setStorageSync("latestRank", msg.lvl);
} else if (msg.constructor === MESSAGETYPES.LvlUpdate) {
uni.setStorageSync("latestLvl", msg.lvl);
} else if (msg.constructor === MESSAGETYPES.DeviceOnline) {
uni.$emit("update-online", true);
} else if (msg.constructor === MESSAGETYPES.DeviceOffline) {
uni.$emit("update-online", false);
} else if (msg.constructor === MESSAGETYPES.DeviceCharging) {
uni.$emit("device-charging");
}
const firstMessage = data.updates[0];
if (firstMessage && ENABLE_REALTIME_MESSAGE_LOG) {
console.log(
"收到 WebSocket 更新",
getMessageTypeName(firstMessage.constructor),
{ updateCount: data.updates.length }
);
}
data.updates.forEach((msg) => {
if (msg) {
if (msg.constructor === MESSAGETYPES.RankUpdate) {
uni.setStorageSync("latestRank", msg.lvl);
} else if (msg.constructor === MESSAGETYPES.LvlUpdate) {
uni.setStorageSync("latestLvl", msg.lvl);
} else if (msg.constructor === MESSAGETYPES.DeviceOnline) {
uni.$emit("update-online", true);
} else if (msg.constructor === MESSAGETYPES.DeviceOffline) {
uni.$emit("update-online", false);
} else if (msg.constructor === MESSAGETYPES.DeviceCharging) {
uni.$emit("device-charging");
}
}
});
});
socketTask.onError((err) => {