diff --git a/src/store/index.js b/src/store/index.js index d4caeb5..e6c9851 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -18,9 +18,9 @@ const defaultState = () => ({ }, }); -function isOnline(LastActive) { +function isOnline(LastActive, currentTime = Date.now()) { const lastActiveTime = dayjs(LastActive)?.valueOf?.() || 0; - if (Date.now() - lastActiveTime > 10 * 1000) { + if (currentTime - lastActiveTime > 10 * 1000) { return -1; } return 1; @@ -75,18 +75,18 @@ const store = createStore({ * 加载服务器列表 */ async loadServers({ commit }) { - const serverConfig = await loadNezhaConfig(); - if (!serverConfig) { + const serverResult = await loadNezhaConfig(); + if (!serverResult) { console.error('load server config failed'); return; } - const servers = serverConfig.map((i) => { + const servers = serverResult.servers?.map?.((i) => { const item = { ...i, - online: isOnline(i.LastActive), + online: isOnline(i.LastActive, serverResult.now), }; return item; - }); + }) || []; commit('SET_SERVERS', servers); }, /** @@ -97,13 +97,13 @@ const store = createStore({ }) { msg.on('servers', (res) => { if (res) { - const servers = res.map((i) => { + const servers = res.servers?.map?.((i) => { const item = { ...i, - online: isOnline(i.LastActive), + online: isOnline(i.LastActive, res.now), }; return item; - }); + }) || []; commit('UPDATE_SERVERS', servers); } }); diff --git a/src/utils/load-nezha-config.js b/src/utils/load-nezha-config.js index 69c4c6d..c15fa01 100644 --- a/src/utils/load-nezha-config.js +++ b/src/utils/load-nezha-config.js @@ -17,7 +17,7 @@ export default async () => fetch(config.nazhua.nezhaPath).then((res) => res.text } const remoteConfig = JSON.parse(unescaped(configStr)); if (remoteConfig?.servers) { - return remoteConfig.servers.map((i) => { + remoteConfig.servers = remoteConfig.servers.map((i) => { const item = { ...i, }; @@ -28,6 +28,7 @@ export default async () => fetch(config.nazhua.nezhaPath).then((res) => res.text } return item; }); + return remoteConfig; } return null; }).catch(() => null); diff --git a/src/ws/index.js b/src/ws/index.js index 821f6cd..117e0e8 100644 --- a/src/ws/index.js +++ b/src/ws/index.js @@ -16,7 +16,7 @@ const wsService = new WSService({ }, onMessage: (data) => { if (data?.now) { - msg.emit('servers', data?.servers); + msg.emit('servers', data); } else { msg.emit('message', data); }