From 817265dad8b8dbff8dcf55ea69aa7846019a6dc4 Mon Sep 17 00:00:00 2001 From: Himit_ZH <372347736@qq.com> Date: Tue, 5 Oct 2021 11:41:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=8F=98=E5=BC=82=E5=9C=B0=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hoj/service/impl/SessionServiceImpl.java | 27 +++++++++++++++---- hoj-vue/src/components/oj/common/NavBar.vue | 15 +++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/hoj-springboot/DataBackup/src/main/java/top/hcode/hoj/service/impl/SessionServiceImpl.java b/hoj-springboot/DataBackup/src/main/java/top/hcode/hoj/service/impl/SessionServiceImpl.java index 82f97b65..bcf7930a 100644 --- a/hoj-springboot/DataBackup/src/main/java/top/hcode/hoj/service/impl/SessionServiceImpl.java +++ b/hoj-springboot/DataBackup/src/main/java/top/hcode/hoj/service/impl/SessionServiceImpl.java @@ -54,10 +54,14 @@ public class SessionServiceImpl extends ServiceImpl impl Session lastSession = sessionList.get(1); // 如果两次登录的ip不相同,需要发通知给用户 if (!nowSession.getIp().equals(lastSession.getIp())) { + String remoteLoginContent = getRemoteLoginContent(lastSession.getIp(), nowSession.getIp(), nowSession.getGmtCreate()); + if (remoteLoginContent == null) { + return; + } AdminSysNotice adminSysNotice = new AdminSysNotice(); adminSysNotice .setType("Single") - .setContent(getRemoteLoginContent(nowSession.getIp(), nowSession.getGmtCreate())) + .setContent(remoteLoginContent) .setTitle("账号异地登录通知(Account Remote Login Notice)") .setAdminId("1") .setState(false) @@ -78,16 +82,29 @@ public class SessionServiceImpl extends ServiceImpl impl } } - private String getRemoteLoginContent(String newIp, Date loginDate) { + private String getRemoteLoginContent(String oldIp, String newIp, Date loginDate) { String dateStr = DateUtil.format(loginDate, "yyyy-MM-dd HH:mm:ss"); StringBuilder sb = new StringBuilder(); sb.append("亲爱的用户,您好!您的账号于").append(dateStr); String addr = null; try { - String res = HttpUtil.get("https://whois.pconline.com.cn/ipJson.jsp?ip=" + newIp + "&json=true"); - JSONObject resJson = JSONUtil.parseObj(res); - addr = resJson.getStr("addr"); + String newRes = HttpUtil.get("https://whois.pconline.com.cn/ipJson.jsp?ip=" + newIp + "&json=true"); + JSONObject newResJson = JSONUtil.parseObj(newRes); + addr = newResJson.getStr("addr"); + + String newCityCode = newResJson.getStr("cityCode"); + + String oldRes = HttpUtil.get("https://whois.pconline.com.cn/ipJson.jsp?ip=" + oldIp + "&json=true"); + JSONObject oldResJson = JSONUtil.parseObj(oldRes); + + String oldCityCode = oldResJson.getStr("cityCode"); + + if (newCityCode == null || oldCityCode == null || newCityCode.equals(oldCityCode)) { + return null; + } + } catch (Exception ignored) { + return null; } if (!StringUtils.isEmpty(addr)) { sb.append("在【") diff --git a/hoj-vue/src/components/oj/common/NavBar.vue b/hoj-vue/src/components/oj/common/NavBar.vue index cd134432..ee7b6e1a 100644 --- a/hoj-vue/src/components/oj/common/NavBar.vue +++ b/hoj-vue/src/components/oj/common/NavBar.vue @@ -634,6 +634,21 @@ export default { }, }, }, + watch: { + isAuthenticated() { + if (this.isAuthenticated) { + if (this.msgTimer) { + clearInterval(this.msgTimer); + } + this.getUnreadMsgCount(); + this.msgTimer = setInterval(() => { + this.getUnreadMsgCount(); + }, 120 * 1000); + } else { + clearInterval(this.msgTimer); + } + }, + }, };