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

@@ -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;
// }
}