Browse Source

增加小程序不用校验验证码的登录接口

953954216 11 months ago
parent
commit
2c3b15d864

+ 9 - 0
ruoyi-auth/src/main/java/com/ruoyi/auth/controller/TokenController.java

@@ -44,6 +44,15 @@ public class TokenController
         return R.ok(tokenService.createToken(userInfo));
     }
 
+    @PostMapping("loginMini")
+    public R<?> loginMini(@RequestBody LoginBody form)
+    {
+        // 用户登录
+        LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
+        // 获取登录token
+        return R.ok(tokenService.createToken(userInfo));
+    }
+
     @DeleteMapping("logout")
     public R<?> logout(HttpServletRequest request)
     {

+ 40 - 0
ruoyi-auth/src/main/java/com/ruoyi/auth/service/SysLoginService.java

@@ -95,6 +95,46 @@ public class SysLoginService
         return userInfo;
     }
 
+    public LoginUser loginMini(String username, String password)
+    {
+        // 用户名或密码为空 错误
+        if (StringUtils.isAnyBlank(username, password))
+        {
+            recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
+            throw new ServiceException("用户/密码必须填写");
+        }
+        // 查询用户信息
+        R<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
+
+        if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
+        {
+            recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
+            throw new ServiceException("登录用户:" + username + " 不存在");
+        }
+
+        if (R.FAIL == userResult.getCode())
+        {
+            throw new ServiceException(userResult.getMsg());
+        }
+
+        LoginUser userInfo = userResult.getData();
+        SysUser user = userResult.getData().getSysUser();
+        if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
+        {
+            recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
+            throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
+        }
+        if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
+        {
+            recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
+            throw new ServiceException("对不起,您的账号:" + username + " 已停用");
+        }
+
+        passwordService.validate(user, password);
+        recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
+        return userInfo;
+    }
+
     public void logout(String loginName)
     {
         recordLogService.recordLogininfor(loginName, Constants.LOGOUT, "退出成功");

+ 4 - 1
ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/ValidateCodeFilter.java

@@ -28,6 +28,7 @@ import java.util.concurrent.atomic.AtomicReference;
 public class ValidateCodeFilter extends AbstractGatewayFilterFactory<Object>
 {
     private final static String[] VALIDATE_URL = new String[] { "/auth/login", "/auth/register" };
+    private final static String[] NOAUTH_URL = new String[] { "/auth/loginMini" };
 
     @Autowired
     private ValidateCodeService validateCodeService;
@@ -55,7 +56,9 @@ public class ValidateCodeFilter extends AbstractGatewayFilterFactory<Object>
             {
                 String rspStr = resolveBodyFromRequest(request);
                 JSONObject obj = JSON.parseObject(rspStr);
-                validateCodeService.checkCaptcha(obj.getString(CODE), obj.getString(UUID));
+                if(!StringUtils.containsAnyIgnoreCase(request.getURI().getPath(), NOAUTH_URL) || !captchaProperties.getEnabled()){
+                    validateCodeService.checkCaptcha(obj.getString(CODE), obj.getString(UUID));
+                }
             }
             catch (Exception e)
             {