mirror of
https://github.com/hi2shark/nazhua.git
synced 2026-01-17 01:30:44 +08:00
✨ 更新 isOnline 函数以支持自定义当前时间,优化服务器加载逻辑以包含当前时间信息
This commit is contained in:
parent
5e4387f6a0
commit
4542fc042a
@ -18,9 +18,9 @@ const defaultState = () => ({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
function isOnline(LastActive) {
|
function isOnline(LastActive, currentTime = Date.now()) {
|
||||||
const lastActiveTime = dayjs(LastActive)?.valueOf?.() || 0;
|
const lastActiveTime = dayjs(LastActive)?.valueOf?.() || 0;
|
||||||
if (Date.now() - lastActiveTime > 10 * 1000) {
|
if (currentTime - lastActiveTime > 10 * 1000) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@ -75,18 +75,18 @@ const store = createStore({
|
|||||||
* 加载服务器列表
|
* 加载服务器列表
|
||||||
*/
|
*/
|
||||||
async loadServers({ commit }) {
|
async loadServers({ commit }) {
|
||||||
const serverConfig = await loadNezhaConfig();
|
const serverResult = await loadNezhaConfig();
|
||||||
if (!serverConfig) {
|
if (!serverResult) {
|
||||||
console.error('load server config failed');
|
console.error('load server config failed');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const servers = serverConfig.map((i) => {
|
const servers = serverResult.servers?.map?.((i) => {
|
||||||
const item = {
|
const item = {
|
||||||
...i,
|
...i,
|
||||||
online: isOnline(i.LastActive),
|
online: isOnline(i.LastActive, serverResult.now),
|
||||||
};
|
};
|
||||||
return item;
|
return item;
|
||||||
});
|
}) || [];
|
||||||
commit('SET_SERVERS', servers);
|
commit('SET_SERVERS', servers);
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
@ -97,13 +97,13 @@ const store = createStore({
|
|||||||
}) {
|
}) {
|
||||||
msg.on('servers', (res) => {
|
msg.on('servers', (res) => {
|
||||||
if (res) {
|
if (res) {
|
||||||
const servers = res.map((i) => {
|
const servers = res.servers?.map?.((i) => {
|
||||||
const item = {
|
const item = {
|
||||||
...i,
|
...i,
|
||||||
online: isOnline(i.LastActive),
|
online: isOnline(i.LastActive, res.now),
|
||||||
};
|
};
|
||||||
return item;
|
return item;
|
||||||
});
|
}) || [];
|
||||||
commit('UPDATE_SERVERS', servers);
|
commit('UPDATE_SERVERS', servers);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@ -17,7 +17,7 @@ export default async () => fetch(config.nazhua.nezhaPath).then((res) => res.text
|
|||||||
}
|
}
|
||||||
const remoteConfig = JSON.parse(unescaped(configStr));
|
const remoteConfig = JSON.parse(unescaped(configStr));
|
||||||
if (remoteConfig?.servers) {
|
if (remoteConfig?.servers) {
|
||||||
return remoteConfig.servers.map((i) => {
|
remoteConfig.servers = remoteConfig.servers.map((i) => {
|
||||||
const item = {
|
const item = {
|
||||||
...i,
|
...i,
|
||||||
};
|
};
|
||||||
@ -28,6 +28,7 @@ export default async () => fetch(config.nazhua.nezhaPath).then((res) => res.text
|
|||||||
}
|
}
|
||||||
return item;
|
return item;
|
||||||
});
|
});
|
||||||
|
return remoteConfig;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}).catch(() => null);
|
}).catch(() => null);
|
||||||
|
|||||||
@ -16,7 +16,7 @@ const wsService = new WSService({
|
|||||||
},
|
},
|
||||||
onMessage: (data) => {
|
onMessage: (data) => {
|
||||||
if (data?.now) {
|
if (data?.now) {
|
||||||
msg.emit('servers', data?.servers);
|
msg.emit('servers', data);
|
||||||
} else {
|
} else {
|
||||||
msg.emit('message', data);
|
msg.emit('message', data);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user