1.优化接口登录逻辑

This commit is contained in:
2025-07-24 21:10:47 +08:00
parent e74a8bfd98
commit 061711a9c0
4 changed files with 102 additions and 60 deletions

View File

@@ -19,7 +19,7 @@ public enum ErrorCode {
NOT_FOUND_ERROR(40800, "请求数据不存在"),
FORBIDDEN_ERROR(40300, "禁止访问"),
TENANT_NAME_NOT_EXISTS(40600, "租户不存在"),
LOGIN_NOW_ALLOWED(40700, "当前账号不允许登录"),
LOGIN_NOW_ALLOWED(40700, "当前账号没有登录权限"),
SYSTEM_ERROR(50000, "系统内部异常"),
OPERATION_ERROR(50001, "操作失败"),
QUEUE_ERROR(60001, "队列消息添加失败"),

View File

@@ -10,8 +10,10 @@ import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.dto.user.SystemUsersDTO;
import com.yupi.springbootinit.model.entity.SystemUsers;
import com.yupi.springbootinit.model.enums.CommonStatusEnum;
import com.yupi.springbootinit.model.enums.LoginSceneEnum;
import com.yupi.springbootinit.model.vo.user.SystemUsersVO;
import com.yupi.springbootinit.service.SystemUsersService;
import com.yupi.springbootinit.service.impl.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
@@ -28,83 +30,51 @@ import javax.annotation.Resource;
public class UserController {
// @Resource
// private SystemUsersService usersService;
@Resource
private SystemUsersService usersService;
private LoginService loginService;
// 用户登陆接口
@PostMapping("doLogin")
public BaseResponse<SystemUsersVO> doLogin(@RequestBody SystemUsersDTO usersDTO) {
SystemUsers user = getUserByName(usersDTO);
if (!usersService.checkCrawlRole(user.getId())){
throw new BusinessException(ErrorCode.LOGIN_NOW_ALLOWED);
}
Long second = usersService.getTenantExpiredTime(usersDTO.getTenantId());
SystemUsersVO systemUsersVO = new SystemUsersVO();
BeanUtil.copyProperties(user, systemUsersVO);
// 赋予用户 Id
StpUtil.login(user.getId(),"host");
// 设置 token 有效期为当前日期和套餐有效期的差值
StpUtil.renewTimeout(second);
systemUsersVO.setTokenName(StpUtil.getTokenName());
systemUsersVO.setTokenValue(StpUtil.getTokenValue());
return ResultUtils.success(systemUsersVO);
return ResultUtils.success(loginService.login(LoginSceneEnum.HOST, usersDTO));
// return ResultUtils.success(systemUsersVO);
}
// 用户登陆接口
@PostMapping("bigbrother-doLogin")
public BaseResponse<SystemUsersVO> bigBrotherDoLogin(@RequestBody SystemUsersDTO usersDTO) {
SystemUsers user = getUserByName(usersDTO);
if (!usersService.checkbigBrotherlRole(user.getId())){
throw new BusinessException(ErrorCode.LOGIN_NOW_ALLOWED);
}
Long second = usersService.getTenantExpiredTime(usersDTO.getTenantId());
SystemUsersVO systemUsersVO = new SystemUsersVO();
BeanUtil.copyProperties(user, systemUsersVO);
// 赋予用户 Id
StpUtil.login(user.getId(),"bigbrother");
// 设置 token 有效期为当前日期和套餐有效期的差值
StpUtil.renewTimeout(second);
systemUsersVO.setTokenName(StpUtil.getTokenName());
systemUsersVO.setTokenValue(StpUtil.getTokenValue());
return ResultUtils.success(systemUsersVO);
return ResultUtils.success(loginService.login(LoginSceneEnum.BIG_BROTHER, usersDTO));
}
// 用户登陆接口
@PostMapping("aiChat-doLogin")
public BaseResponse<SystemUsersVO> aiChatDoLogin(@RequestBody SystemUsersDTO usersDTO) {
SystemUsers user = getUserByName(usersDTO);
if (!usersService.checkAiCHatLoginRole(user.getId())){
throw new BusinessException(ErrorCode.LOGIN_NOW_ALLOWED);
}
Long second = usersService.getTenantExpiredTime(usersDTO.getTenantId());
SystemUsersVO systemUsersVO = new SystemUsersVO();
BeanUtil.copyProperties(user, systemUsersVO);
// 赋予用户 Id
StpUtil.login(user.getId(),"aiChat");
// 设置 token 有效期为当前日期和套餐有效期的差值
StpUtil.renewTimeout(second);
systemUsersVO.setTokenName(StpUtil.getTokenName());
systemUsersVO.setTokenValue(StpUtil.getTokenValue());
return ResultUtils.success(systemUsersVO);
return ResultUtils.success(loginService.login(LoginSceneEnum.AI_CHAT, usersDTO));
// return ResultUtils.success(systemUsersVO);
}
private SystemUsers getUserByName(@RequestBody SystemUsersDTO usersDTO) {
SystemUsers user = usersService.getUserByUserName(usersDTO.getUsername(),usersDTO.getTenantId());
if (user == null) {
throw new BusinessException(ErrorCode.USERNAME_OR_PASSWORD_ERROR);
}
if (!usersService.isPasswordMatch(usersDTO.getPassword(), user.getPassword())) {
throw new BusinessException(ErrorCode.USERNAME_OR_PASSWORD_ERROR);
}
if (CommonStatusEnum.isDisable(Integer.valueOf(user.getStatus()))) {
throw new BusinessException(ErrorCode.USER_DISABLE);
}
if (usersService.isExpired(usersDTO.getTenantId())){
throw new BusinessException(ErrorCode.PACKAGE_EXPIRED);
}
return user;
}
//
// private SystemUsers getUserByName(@RequestBody SystemUsersDTO usersDTO) {
// SystemUsers user = usersService.getUserByUserName(usersDTO.getUsername(),usersDTO.getTenantId());
// if (user == null) {
// throw new BusinessException(ErrorCode.USERNAME_OR_PASSWORD_ERROR);
// }
// if (!usersService.isPasswordMatch(usersDTO.getPassword(), user.getPassword())) {
// throw new BusinessException(ErrorCode.USERNAME_OR_PASSWORD_ERROR);
// }
//
// if (CommonStatusEnum.isDisable(Integer.valueOf(user.getStatus()))) {
// throw new BusinessException(ErrorCode.USER_DISABLE);
// }
// if (usersService.isExpired(usersDTO.getTenantId())){
// throw new BusinessException(ErrorCode.PACKAGE_EXPIRED);
// }
// return user;
// }
}

View File

@@ -0,0 +1,16 @@
package com.yupi.springbootinit.model.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public enum LoginSceneEnum {
HOST("doLogin", "host", "checkCrawlRole"),
BIG_BROTHER("bigbrother-doLogin", "bigbrother", "checkBigBrotherRole"),
AI_CHAT("aiChat-doLogin", "aiChat", "checkAiChatLoginRole");
private final String path; // 对应 @PostMapping
private final String saMode; // Sa-Token 登录模式
private final String checker; // SystemUsersService 中对应的校验方法名
}

View File

@@ -0,0 +1,56 @@
package com.yupi.springbootinit.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.dto.user.SystemUsersDTO;
import com.yupi.springbootinit.model.entity.SystemUsers;
import com.yupi.springbootinit.model.enums.CommonStatusEnum;
import com.yupi.springbootinit.model.enums.LoginSceneEnum;
import com.yupi.springbootinit.model.vo.user.SystemUsersVO;
import com.yupi.springbootinit.service.SystemUsersService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class LoginService {
private final SystemUsersService usersService;
public SystemUsersVO login(LoginSceneEnum scene, SystemUsersDTO dto) {
SystemUsers user = validateUser(dto); // 校验用户名、密码、状态、租户过期
checkRole(scene, user.getId()); // 按场景做角色校验
Long second = usersService.getTenantExpiredTime(dto.getTenantId());
// Sa-Token 登录
StpUtil.login(user.getId(), scene.getSaMode());
StpUtil.renewTimeout(second);
SystemUsersVO vo = new SystemUsersVO();
BeanUtil.copyProperties(user, vo);
vo.setTokenName(StpUtil.getTokenName());
vo.setTokenValue(StpUtil.getTokenValue());
return vo;
}
private SystemUsers validateUser(SystemUsersDTO dto) {
SystemUsers user = usersService.getUserByUserName(dto.getUsername(), dto.getTenantId());
if (user == null) throw new BusinessException(ErrorCode.USERNAME_OR_PASSWORD_ERROR);
if (!usersService.isPasswordMatch(dto.getPassword(), user.getPassword()))
throw new BusinessException(ErrorCode.USERNAME_OR_PASSWORD_ERROR);
if (CommonStatusEnum.isDisable(Integer.valueOf(user.getStatus())))
throw new BusinessException(ErrorCode.USER_DISABLE);
if (usersService.isExpired(dto.getTenantId()))
throw new BusinessException(ErrorCode.PACKAGE_EXPIRED);
return user;
}
private void checkRole(LoginSceneEnum scene, Long userId) {
Boolean pass = switch (scene) {
case HOST -> usersService.checkCrawlRole(userId);
case BIG_BROTHER -> usersService.checkbigBrotherlRole(userId);
case AI_CHAT -> usersService.checkAiCHatLoginRole(userId);
};
if (!pass) throw new BusinessException(ErrorCode.LOGIN_NOW_ALLOWED);
}
}