登录时校验VIP时间范围和试用期

This commit is contained in:
liuchengqian 2023-03-25 20:45:16 +08:00
parent da91b6f4b6
commit e75e86ac07
2 changed files with 28 additions and 4 deletions

View File

@ -1,10 +1,11 @@
package com.xkrs.common.account; package com.xkrs.common.account;
import com.xkrs.service.SysAuthorityService;
import com.xkrs.model.entity.SysAuthorityEntity; import com.xkrs.model.entity.SysAuthorityEntity;
import com.xkrs.model.entity.SysUserEntity; import com.xkrs.model.entity.SysUserEntity;
import com.xkrs.service.SysAuthorityService;
import com.xkrs.service.SysRoleService; import com.xkrs.service.SysRoleService;
import com.xkrs.service.SysUserService; import com.xkrs.service.SysUserService;
import com.xkrs.utilsnew.VipTimeRangeUtils;
import com.xkrs.utilsold.DateTimeUtil; import com.xkrs.utilsold.DateTimeUtil;
import org.springframework.security.authentication.AuthenticationProvider; import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.authentication.BadCredentialsException;
@ -70,12 +71,19 @@ public class CustomAuthenticationProvider implements AuthenticationProvider {
// 检查用户是否存在 // 检查用户是否存在
if (userEntity == null) { if (userEntity == null) {
throw new BadCredentialsException("账号不存在或错误,请您确认注册"); throw new DisabledException("您的账号不存在,请您前往注册。");
} }
// 检查用户是否激活 // 检查用户是否激活
if (userEntity.getActiveFlag() != 0) { if (userEntity.getActiveFlag() != 0) {
throw new DisabledException("该账号还未激活,请联系管理员"); throw new DisabledException("您的账号未激活,详情请联系客服人员。");
}
// 检查用户是否在VIP时间范围内
boolean inVipTimeRange = VipTimeRangeUtils.checkIfInVipTimeRange(userEntity.getVipTimeRangeJson());
// 检查用户是否在试用期范围内
boolean inTryTimeRange = VipTimeRangeUtils.checkIfInTryTimeRange(userEntity.getAddTime(), userEntity.getDayNum());
//既不在VIP时间范围内 也不在试用期范围内
if ((!inVipTimeRange) && (!inTryTimeRange)) {
throw new DisabledException("您的账号已到期,详情请联系客服人员。");
} }
// 认证逻辑 // 认证逻辑

View File

@ -18,6 +18,22 @@ public class VipTimeRangeUtils {
private VipTimeRangeUtils() { private VipTimeRangeUtils() {
} }
public static boolean checkIfInTryTimeRange(String addTime, Long dayNum) {
return checkIfInTryTimeRange(addTime, dayNum, LocalDateTime.now());
}
/**
* 校验传入的时间是否在指定的试用期过期时间之前
*/
public static boolean checkIfInTryTimeRange(String addTime, Long dayNum, LocalDateTime targetLocalDateTime) {
if (TextUtils.isEmpty(addTime) || dayNum == null || targetLocalDateTime == null) {
return false;
}
LocalDateTime addLocalDateTime = LocalDateTime.parse(addTime, DateTimeUtils.DATE_TIME_FORMATTER_1);
LocalDateTime tryLocalDateTime = addLocalDateTime.plusDays(dayNum);
return targetLocalDateTime.isBefore(tryLocalDateTime);
}
public static boolean checkIfInVipTimeRange(String vipTimeRangeJson) { public static boolean checkIfInVipTimeRange(String vipTimeRangeJson) {
return checkIfInVipTimeRange(vipTimeRangeJson, LocalDateTime.now()); return checkIfInVipTimeRange(vipTimeRangeJson, LocalDateTime.now());
} }