| | |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.security.KeyPair; |
| | | import java.time.Duration; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.List; |
| | | import java.util.Objects; |
| | | import java.util.concurrent.TimeUnit; |
| | |
| | | */ |
| | | @RestController |
| | | public class SysUserController extends BaseController { |
| | | private static final DateTimeFormatter UNIQUE_NUMBER_DATE_FORMATTER = DateTimeFormatter.BASIC_ISO_DATE; |
| | | private static final String UNIQUE_NUMBER_REDIS_KEY_PREFIX = "sys:user:unique-number:"; |
| | | |
| | | @Resource |
| | | private ISysUserService userService; |
| | | |
| | |
| | | if (StringUtils.isNotEmpty(user.getUserAccount()) && !userService.checkUserNameUnique(user)) { |
| | | throw new ServiceException("登录账号重复", I18nKeyConstant.ACCOUNT_DUPLICATE); |
| | | } |
| | | //校验编号是否唯一 |
| | | Long count = userService.lambdaQuery().eq(SysUser::getUniqueNumber, user.getUniqueNumber()).count(); |
| | | if (count > 0) { |
| | | throw new ServiceException("用户编号已存在", I18nKeyConstant.ACCOUNT_UNIQUE_NUMBER_DUPLICATE); |
| | | } |
| | | user.setUniqueNumber(generateUniqueNumber()); |
| | | user.setPassword(SecurityUtils.encryptPassword(user.getPassword())); |
| | | //TODO 手机号脱敏存储 |
| | | userService.insertUser(user); |
| | |
| | | return AjaxResult.success(); |
| | | } |
| | | |
| | | private String generateUniqueNumber() { |
| | | LocalDate today = LocalDate.now(); |
| | | String dateStr = UNIQUE_NUMBER_DATE_FORMATTER.format(today); |
| | | String redisKey = UNIQUE_NUMBER_REDIS_KEY_PREFIX + dateStr; |
| | | |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | LocalDateTime tomorrowStart = today.plusDays(1).atStartOfDay(); |
| | | long expireSeconds = Duration.between(now, tomorrowStart).getSeconds() + 60; |
| | | |
| | | for (int i = 0; i < 10; i++) { |
| | | Long seq = redisService.redisTemplate.opsForValue().increment(redisKey); |
| | | if (seq == null) { |
| | | throw new ServiceException("用户编号生成失败"); |
| | | } |
| | | if (expireSeconds > 0) { |
| | | redisService.expire(redisKey, expireSeconds, TimeUnit.SECONDS); |
| | | } |
| | | if (seq > 999999L) { |
| | | throw new ServiceException("当天生成用户编号已达上限"); |
| | | } |
| | | String uniqueNumber = dateStr + String.format("%06d", seq); |
| | | |
| | | Long count = userService.lambdaQuery().eq(SysUser::getUniqueNumber, uniqueNumber).count(); |
| | | if (count == null || count == 0L) { |
| | | return uniqueNumber; |
| | | } |
| | | } |
| | | throw new ServiceException("用户编号生成失败,请稍后重试"); |
| | | } |
| | | |
| | | |
| | | @GetMapping("/verifyUserNameRepeat/{username}") |
| | | @ApiOperation(value = "校验账号是否重复", tags = {"管理后台-系统用户管理"}) |