From aca04c190dfc5ad919acf30a3ceaa184a8c0c903 Mon Sep 17 00:00:00 2001 From: RB Date: Fri, 15 Nov 2024 20:30:30 +0800 Subject: [PATCH] feat: LoginChannel --- .../rebuild/web/user/signup/LoginAction.java | 16 ++-- .../rebuild/web/user/signup/LoginChannel.java | 76 +++++++++++++++++++ 2 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/rebuild/web/user/signup/LoginChannel.java diff --git a/src/main/java/com/rebuild/web/user/signup/LoginAction.java b/src/main/java/com/rebuild/web/user/signup/LoginAction.java index f10791d73..472cfb2d8 100644 --- a/src/main/java/com/rebuild/web/user/signup/LoginAction.java +++ b/src/main/java/com/rebuild/web/user/signup/LoginAction.java @@ -166,12 +166,10 @@ private void createLoginLog(HttpServletRequest request, ID user) { OperatingSystem os = UA.getOperatingSystem(); if (os != null) { uaSimple += " (" + os + ")"; - if (os.getDeviceType() != null && os.getDeviceType() == DeviceType.MOBILE) uaSimple += " [Mobile]"; } + uaSimple += " [" + LoginChannel.parse(userAgent).name() + "]"; if (request.getAttribute(SK_TEMP_AUTH) != null) uaSimple += " [TempAuth]"; - if (userAgent.contains("DingTalk")) uaSimple += " [DingTalk]"; - if (userAgent.contains("wxwork")) uaSimple += " [WeCom]"; } catch (Exception ex) { log.warn("Unknown User-Agent : {}", userAgent); @@ -181,14 +179,14 @@ private void createLoginLog(HttpServletRequest request, ID user) { final String ipAddr = StringUtils.defaultString(ServletUtils.getRemoteAddr(request), "127.0.0.1"); final String reqUrl = request.getRequestURL() == null ? "" : request.getRequestURL().toString(); - final Record llog = EntityHelper.forNew(EntityHelper.LoginLog, UserService.SYSTEM_USER); - llog.setID("user", user); - llog.setString("ipAddr", ipAddr); - llog.setString("userAgent", uaSimple); - llog.setDate("loginTime", CalendarUtils.now()); + final Record lgLog = EntityHelper.forNew(EntityHelper.LoginLog, UserService.SYSTEM_USER); + lgLog.setID("user", user); + lgLog.setString("ipAddr", ipAddr); + lgLog.setString("userAgent", uaSimple); + lgLog.setDate("loginTime", CalendarUtils.now()); TaskExecutors.queue(() -> { - Application.getCommonsService().create(llog); + Application.getCommonsService().create(lgLog); User u = Application.getUserStore().getUser(user); String uid = StringUtils.defaultString(u.getEmail(), u.getName()); diff --git a/src/main/java/com/rebuild/web/user/signup/LoginChannel.java b/src/main/java/com/rebuild/web/user/signup/LoginChannel.java new file mode 100644 index 000000000..741e5d510 --- /dev/null +++ b/src/main/java/com/rebuild/web/user/signup/LoginChannel.java @@ -0,0 +1,76 @@ +/*! +Copyright (c) REBUILD and/or its owners. All rights reserved. + +rebuild is dual-licensed under commercial and open source licenses (GPLv3). +See LICENSE and COMMERCIAL in the project root for license information. +*/ + +package com.rebuild.web.user.signup; + +import eu.bitwalker.useragentutils.DeviceType; +import eu.bitwalker.useragentutils.OperatingSystem; +import eu.bitwalker.useragentutils.UserAgent; +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; + +/** + * 登录渠道/类型 + * + * @author devezhao + * @since 2024/11/15 + */ +@Slf4j +public enum LoginChannel { + + PC_WEB("PC浏览器"), + PC_DINGTALK("PC钉钉"), + PC_WECOM("PC企业微信"), + PC_WECHAT("PC微信"), + PC_DESKTOP("PC桌面"), + + MOB_WEB("手机浏览器"), + MOB_DINGTALK("手机钉钉"), + MOB_WECOM("手机企业微信"), + MOB_WECHAT("手机微信"), + MOB_ANDROID("手机APP"), + MOB_IOS("iOS APP"), // 保留 + + ; + + @Getter + private final String name; + LoginChannel(String name) { + this.name = name; + } + + /** + * @param userAgent + * @return + */ + public static LoginChannel parse(String userAgent) { + userAgent = userAgent.toUpperCase(); + UserAgent UA = UserAgent.parseUserAgentString(userAgent); + OperatingSystem OS = UA.getOperatingSystem(); + + boolean isDingtalk = userAgent.contains("DINGTALK"); + boolean isWecom = userAgent.contains("WXWORK"); + boolean isWechat = !isWecom && userAgent.contains("MICROMESSENGER"); + boolean isH5PlusApp = userAgent.contains("HTML5PLUS"); + + if (OS != null && OS.getDeviceType() == DeviceType.MOBILE) { + if (isDingtalk) return MOB_DINGTALK; + else if (isWecom) return MOB_WECOM; + else if (isWechat) return MOB_WECHAT; + else if (isH5PlusApp) return MOB_ANDROID; + return MOB_WEB; + } + + boolean isDesktop = userAgent.contains("ELECTRON"); + + if (isDingtalk) return PC_DINGTALK; + else if (isWecom) return PC_WECOM; + else if (isWechat) return PC_WECHAT; + else if (isDesktop) return PC_DESKTOP; + return PC_WEB; + } +}