Przeglądaj źródła

修改获取用户登录设备信息

zhouxianguang 6 lat temu
rodzic
commit
d2175fe6b2

+ 59 - 2
rankin-api-web/src/main/java/cn/rankin/apiweb/controller/LoginController.java

@@ -39,14 +39,17 @@ public class LoginController
 
         String terminal = request.getHeader("terminal");
         String merchant = request.getHeader("merchant");
+        String userAgent = request.getHeader("User-Agent");
+
 
         String ip = HttpUtil.getClientIp(request);
         loginInfoDTO.setIp(ip);
         loginInfoDTO.setTerminal(terminal);
         loginInfoDTO.setMerchant(merchant);
 
-        log.info("login request header : ip={},terminal={},merchant={},user-agent={}", ip, terminal, merchant);
-
+        log.info("login request header : ip={},terminal={},merchant={},user-agent={}", ip, terminal, merchant,userAgent);
+        //填充设备信息
+        fillDeviceInfo(userAgent, loginInfoDTO);
 
         return userService.login(loginInfoDTO);
     }
@@ -126,4 +129,58 @@ public class LoginController
         }
     }
 
+    /**
+     * Fill device info
+     * @param userAgentStr the string of user agent
+     * @param loginInfoDTO the login info
+     */
+    private void fillDeviceInfo(String userAgentStr, LoginInfoDTO loginInfoDTO)
+    {
+        if (userAgentStr == "" || userAgentStr == null)
+        {
+            return;
+        }
+        if (loginInfoDTO == null)
+        {
+            return;
+        }
+
+        UserAgent userAgent = UserAgent.parseUserAgentString(userAgentStr);
+
+        //获取浏览器对象
+        Browser browser = userAgent.getBrowser();
+
+        //获取操作系统对象
+        OperatingSystem operatingSystem = userAgent.getOperatingSystem();
+
+        //操作系统名称
+        String osName = operatingSystem.getName();
+        loginInfoDTO.setDeviceName(osName);
+
+        //获取厂商
+        String osMfrs = operatingSystem.getManufacturer().toString();
+        loginInfoDTO.setDeviceMfrs(osMfrs);
+
+        //获取设备类型
+        String deviceType = operatingSystem.getDeviceType().toString();
+        loginInfoDTO.setDeviceType(deviceType);
+
+        //获取设备型号
+        if ("MOBILE".equals(deviceType))
+        {
+            Pattern pattern = Pattern.compile(";\\s?(\\S*?\\s?\\S*?)\\s?(Build)?/");
+            Matcher matcher = pattern.matcher(userAgentStr);
+            String model = null;
+            if (matcher.find())
+            {
+                model = matcher.group(1).trim();
+                loginInfoDTO.setDeviceModel(model);
+            }
+        }
+        else
+        {
+            loginInfoDTO.setDeviceModel(osName);
+        }
+    }
+
 }

+ 22 - 0
rankin-api-web/src/main/java/cn/rankin/apiweb/service/user/UserService.java

@@ -134,6 +134,28 @@ public class UserService
         UserInfoVo userInfoVo = toUserInfoVo(deviceUserVo, true);
         log.info("user={} login success, userInfo={}", eid, JSON.toJSONString(userInfoVo));
 
+
+        //操作用户登录设备信息
+        if (deviceName != null && !"".equals(deviceName.trim()))
+        {
+            UserDeviceDTO dto = new UserDeviceDTO();
+            dto.setDeviceCode(deviceCode);
+            dto.setDeviceModel(deviceModel);
+            dto.setDeviceType(deviceType);
+            dto.setDeviceMfrs(deviceMfrs);
+            dto.setDeviceName(deviceName);
+
+            int opRet = userClient.opUserDevice(userId,dto);
+            if (opRet == 0)
+            {
+                log.error("Failed to operated user device with[uid={},deviceCode={},deviceModel={},deviceName={}]", userId, deviceCode, deviceModel, deviceName);
+            }
+            else
+            {
+                log.info("Succeed to operating user device with[uid={},deviceCode={},deviceModel={},deviceName={}]", userId, deviceCode, deviceModel, deviceName);
+            }
+        }
+
         return APIResult.ok(userInfoVo);
     }