From 3c2cd564b2299142a96edb82dacb705c351d71ed Mon Sep 17 00:00:00 2001
From: 无关风月 <443237572@qq.com>
Date: 星期三, 15 一月 2025 10:24:29 +0800
Subject: [PATCH] 修改bug

---
 ruoyi-auth/src/main/java/com/ruoyi/auth/controller/TokenController.java                              |   21 ++++++++++
 ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysUserController.java          |    2 
 ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/factory/RemoteUserFallbackFactory.java |    5 ++
 ruoyi-auth/src/main/java/com/ruoyi/auth/service/SysLoginService.java                                 |   54 +++++++++++++++++++++++++++
 ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserMapper.java                  |    1 
 ruoyi-modules/ruoyi-system/src/main/resources/mapping/system/SysUserMapper.xml                       |    6 +++
 ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java               |    1 
 ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java       |    4 ++
 ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/RemoteUserService.java                 |    2 +
 9 files changed, 95 insertions(+), 1 deletions(-)

diff --git a/ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/RemoteUserService.java b/ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/RemoteUserService.java
index 012d8b9..edd38a6 100644
--- a/ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/RemoteUserService.java
+++ b/ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/RemoteUserService.java
@@ -26,6 +26,8 @@
      */
     @GetMapping("/user/info/{username}")
     public R<LoginUser> getUserInfo(@PathVariable("username") String username, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
+    @GetMapping("/user/infoShop/{username}")
+    public R<LoginUser> infoShop(@PathVariable("username") String username, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
 
     /**
      * 注册用户信息
diff --git a/ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/factory/RemoteUserFallbackFactory.java b/ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/factory/RemoteUserFallbackFactory.java
index 7d7f7e8..c7fee51 100644
--- a/ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/factory/RemoteUserFallbackFactory.java
+++ b/ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/factory/RemoteUserFallbackFactory.java
@@ -30,6 +30,11 @@
             {
                 return R.fail("获取用户失败:" + throwable.getMessage());
             }
+            @Override
+            public R<LoginUser> infoShop(String username, String source)
+            {
+                return R.fail("门店登录获取用户失败:" + throwable.getMessage());
+            }
 
             @Override
             public R<Boolean> registerUserInfo(SysUser sysUser, String source)
diff --git a/ruoyi-auth/src/main/java/com/ruoyi/auth/controller/TokenController.java b/ruoyi-auth/src/main/java/com/ruoyi/auth/controller/TokenController.java
index 6b128cc..6b53013 100644
--- a/ruoyi-auth/src/main/java/com/ruoyi/auth/controller/TokenController.java
+++ b/ruoyi-auth/src/main/java/com/ruoyi/auth/controller/TokenController.java
@@ -64,6 +64,27 @@
 		userClient.updateSysUser(sysUser);
 		return R.ok(map);
 	}
+	@PostMapping("loginShop")
+	public R<?> loginShop(@RequestBody LoginBody form, HttpServletRequest request) {
+		// 用户登录
+		LoginUser userInfo = sysLoginService.loginShop(form.getUsername(), form.getPassword(), request);
+		HashMap<String, Object> map = new HashMap<>();
+		map.put("token", tokenService.createToken(userInfo));
+		List<SysRole> roles = userInfo.getSysUser().getRoles();
+        if(CollectionUtils.isEmpty(roles)){
+            return R.fail("请关联角色!");
+        }
+
+        map.put("roleName",roles.get(0).getRoleName());
+		map.put("info", userInfo);
+		// 修改用户最后登录时间
+		SysUser sysUser = new SysUser();
+		sysUser.setUserId(userInfo.getSysUser().getUserId());
+		sysUser.setLoginDate(new Date());
+		System.out.println("修改用户登录时间" + sysUser);
+		userClient.updateSysUser(sysUser);
+		return R.ok(map);
+	}
 	
 	@DeleteMapping("logout")
 	public R<?> logout(HttpServletRequest request) {
diff --git a/ruoyi-auth/src/main/java/com/ruoyi/auth/service/SysLoginService.java b/ruoyi-auth/src/main/java/com/ruoyi/auth/service/SysLoginService.java
index 3b82526..b41500b 100644
--- a/ruoyi-auth/src/main/java/com/ruoyi/auth/service/SysLoginService.java
+++ b/ruoyi-auth/src/main/java/com/ruoyi/auth/service/SysLoginService.java
@@ -97,6 +97,60 @@
 		recordLogService.recordLogininfor(request, user.getUserId().intValue(), username, Constants.LOGIN_SUCCESS_STATUS, "登录成功");
 		return userInfo;
 	}
+	public LoginUser loginShop(String username, String password, HttpServletRequest request) {
+		username = username.trim();
+		// 查询用户信息
+		R<LoginUser> userResult = remoteUserService.infoShop(username, SecurityConstants.INNER);
+
+		if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
+			recordLogService.recordLogininfor(request, null, username, Constants.LOGIN_FAIL_STATUS, "登录用户不存在");
+			throw new ServiceException("登录用户:" + username + " 不存在");
+		}
+		LoginUser userInfo = userResult.getData();
+		SysUser user = userResult.getData().getSysUser();
+
+		// 用户名或密码为空 错误
+		if (StringUtils.isAnyBlank(username, password)) {
+			recordLogService.recordLogininfor(request, user.getUserId().intValue(), username, Constants.LOGIN_FAIL_STATUS, "用户/密码必须填写");
+			throw new ServiceException("用户/密码必须填写");
+		}
+		// 密码如果不在指定范围内 错误
+		if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
+				|| password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
+			recordLogService.recordLogininfor(request, user.getUserId().intValue(), username, Constants.LOGIN_FAIL_STATUS, "用户密码不在指定范围");
+			throw new ServiceException("用户密码不在指定范围");
+		}
+		// 用户名不在指定范围内 错误
+		if (username.length() < UserConstants.USERNAME_MIN_LENGTH
+				|| username.length() > UserConstants.USERNAME_MAX_LENGTH) {
+			recordLogService.recordLogininfor(request, user.getUserId().intValue(), username, Constants.LOGIN_FAIL_STATUS, "用户名不在指定范围");
+			throw new ServiceException("用户名不在指定范围");
+		}
+		// IP黑名单校验
+		String blackStr = Convert.toStr(redisService.getCacheObject(CacheConstants.SYS_LOGIN_BLACKIPLIST));
+		if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr())) {
+			recordLogService.recordLogininfor(request, user.getUserId().intValue(), username, Constants.LOGIN_FAIL_STATUS, "很遗憾,访问IP已被列入系统黑名单");
+			throw new ServiceException("很遗憾,访问IP已被列入系统黑名单");
+		}
+
+		if (R.FAIL == userResult.getCode()) {
+			throw new ServiceException(userResult.getMsg());
+		}
+
+
+		if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
+			recordLogService.recordLogininfor(request, user.getUserId().intValue(), username, Constants.LOGIN_FAIL_STATUS, "对不起,您的账号已被删除");
+			throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
+		}
+		if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
+			recordLogService.recordLogininfor(request, user.getUserId().intValue(), username, Constants.LOGIN_FAIL_STATUS, "用户已停用,请联系管理员");
+//			throw new ServiceException("对不起,您的账号:" + username + " 已停用");
+			throw new ServiceException("您的账号已被禁用,请联系平台");
+		}
+		passwordService.validate(user, password, request);
+		recordLogService.recordLogininfor(request, user.getUserId().intValue(), username, Constants.LOGIN_SUCCESS_STATUS, "登录成功");
+		return userInfo;
+	}
 	
 	public void logout(String loginName, HttpServletRequest request) {
 		recordLogService.recordLogininfor(request, null, loginName, Constants.LOGIN_SUCCESS_STATUS, "退出成功");
diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysUserController.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysUserController.java
index ccef719..07e4f17 100644
--- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysUserController.java
+++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/controller/SysUserController.java
@@ -419,7 +419,7 @@
     @InnerAuth
     @GetMapping("/infoShop/{username}")
     public R<LoginUser> infoShop(@PathVariable("username") String username) {
-        SysUser sysUser = userService.selectUserByUserName(username);
+        SysUser sysUser = userService.selectUserShopByUserName(username);
         if (StringUtils.isNull(sysUser)) {
             return R.fail("用户名或密码错误");
         }
diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserMapper.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserMapper.java
index 072eb64..1e31dc7 100644
--- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserMapper.java
+++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/mapper/SysUserMapper.java
@@ -48,6 +48,7 @@
      * @return 用户对象信息
      */
     SysUser selectUserByUserName(String userName);
+    SysUser selectUserShopByUserName(String userName);
 
     /**
      * 通过用户ID查询用户
diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java
index d6a88df..776f124 100644
--- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java
+++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/ISysUserService.java
@@ -47,6 +47,7 @@
      * @return 用户对象信息
      */
     SysUser selectUserByUserName(String userName);
+    SysUser selectUserShopByUserName(String userName);
 
     /**
      * 通过用户ID查询用户
diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java
index 5098818..1e08638 100644
--- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java
+++ b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/SysUserServiceImpl.java
@@ -137,6 +137,10 @@
 	public SysUser selectUserByUserName(String userName) {
 		return userMapper.selectUserByUserName(userName);
 	}
+	@Override
+	public SysUser selectUserShopByUserName(String userName) {
+		return userMapper.selectUserShopByUserName(userName);
+	}
 	
 	/**
 	 * 通过用户ID查询用户
diff --git a/ruoyi-modules/ruoyi-system/src/main/resources/mapping/system/SysUserMapper.xml b/ruoyi-modules/ruoyi-system/src/main/resources/mapping/system/SysUserMapper.xml
index d1cd882..ac04b1d 100644
--- a/ruoyi-modules/ruoyi-system/src/main/resources/mapping/system/SysUserMapper.xml
+++ b/ruoyi-modules/ruoyi-system/src/main/resources/mapping/system/SysUserMapper.xml
@@ -105,6 +105,12 @@
 	<select id="selectUserByUserName" parameterType="String" resultMap="SysUserResult">
 	    <include refid="selectUserVo"/>
 		where u.user_name = #{userName} and u.del_flag = '0'
+		and u.role_type = 1
+	</select>
+	<select id="selectUserShopByUserName" parameterType="String" resultMap="SysUserResult">
+	    <include refid="selectUserVo"/>
+		where u.user_name = #{userName} and u.del_flag = '0'
+		and u.role_type = 2
 	</select>
 	
 	<select id="selectUserById" parameterType="Long" resultMap="SysUserResult">

--
Gitblit v1.7.1