新增:

1.用户登录时判断租户是否过期,在登录时赋予 token有效期为当前租户到期时间和当前日期的差值;
2.实现租户名查 租户 Id 接口
This commit is contained in:
2025-06-20 15:58:42 +08:00
parent 7ca3394fd9
commit 440776cfac
10 changed files with 295 additions and 3 deletions

View File

@@ -0,0 +1,36 @@
package com.yupi.springbootinit.controller;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.vo.user.SystemUsersVO;
import com.yupi.springbootinit.service.SystemTenantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
/*
* @author: ziin
* @date: 2025/6/20 15:18
*/
@RestController
@RequestMapping("/tenant")
@Slf4j
@CrossOrigin
public class TenantController {
@Resource
private SystemTenantService systemTenantService;
@GetMapping("/get-id-by-name")
public BaseResponse<Long> getTenantIdByName(@RequestParam("name") String name) {
if (name == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
return ResultUtils.success( systemTenantService.getTenantIdByName(name));
}
}

View File

@@ -34,7 +34,7 @@ public class UserController {
// 用户登陆接口
@PostMapping("doLogin")
public BaseResponse<SystemUsersVO> doLogin(@RequestBody SystemUsersDTO usersDTO) {
SystemUsers user = usersService.getUserByUserName(usersDTO.getUsername());
SystemUsers user = usersService.getUserByUserName(usersDTO.getUsername(),usersDTO.getTenantId());
if (user == null) {
throw new BusinessException(ErrorCode.USERNAME_OR_PASSWORD_ERROR);
}
@@ -46,9 +46,16 @@ public class UserController {
if (CommonStatusEnum.isDisable(Integer.valueOf(user.getStatus()))) {
throw new BusinessException(ErrorCode.USER_DISABLE);
}
if (usersService.isExpired(usersDTO.getTenantId())){
throw new BusinessException(ErrorCode.PACKAGE_EXPIRED);
}
Long second = usersService.getTenantExpiredTime(usersDTO.getTenantId());
SystemUsersVO systemUsersVO = new SystemUsersVO();
BeanUtil.copyProperties(user, systemUsersVO);
// 赋予用户 Id
StpUtil.login(user.getId());
// 设置 token 有效期为当前日期和套餐有效期的差值
StpUtil.renewTimeout(second);
systemUsersVO.setTokenName(StpUtil.getTokenName());
systemUsersVO.setTokenValue(StpUtil.getTokenValue());
return ResultUtils.success(systemUsersVO);