1、修改用户名

2、更改账号绑定邮箱地址
This commit is contained in:
2024-09-25 16:15:18 +08:00
parent b43f9baead
commit 2bc5fef175
13 changed files with 243 additions and 7 deletions

View File

@@ -384,6 +384,10 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
result = SendEmailUtil.send(emailSendDTO.getEmail(), null,
SendEmailUtil.BIND_MAILBOX_TEMPLATE_ID, randomVerifyCode);
break;
case CHANGE_MAILBOX:
result = SendEmailUtil.send(emailSendDTO.getEmail(), null,
SendEmailUtil.CHANGE_MAILBOX_TEMPLATE_ID, randomVerifyCode);
break;
default:
}
if (!result) {
@@ -1580,8 +1584,99 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
}
private Long viewPersonalHomepageCount(Long accountId) {
redisUtil.getPersonalHomepageViewCount(accountId);
return null;
return redisUtil.getPersonalHomepageViewCount(accountId);
}
// 获取当前用户30天内 剩余昵称修改次数
public Map<String, Long> getNicknameModifyTimes(){
Long accountId = UserContext.getUserHolder().getId();
String key = RedisUtil.NICKNAME_MODIFY_TIMES + accountId;
Long times = redisUtil.getIncrementCount(key);
HashMap<String, Long> resp = new HashMap<>();
resp.put("remainingTimes", 5L - times);
resp.put("remainingDays", redisUtil.getExpire(key) == -1 ? 30L : (long) Math.ceil((double) redisUtil.getExpire(key) / (24 * 60 * 60)));
return resp;
}
// 修改用户名 允许用户30天内修改5次
@Transactional(rollbackFor = Exception.class)
public void editUserName(String newUserName){
Long accountId = UserContext.getUserHolder().getId();
// 判断当前用户是否还有修改昵称的次数
Map<String, Long> remainTimes = getNicknameModifyTimes();
Long remainingModifyTimes = remainTimes.get("remainingTimes");
if (remainingModifyTimes > 0){
Account account = new Account().setUserName(newUserName);
account.setId(accountId);
baseMapper.updateById(account);
String key = RedisUtil.NICKNAME_MODIFY_TIMES + accountId;
// 先判断有没有这个key,若没有这个key, 需要为key添加有效期
if (remainingModifyTimes == 5){
redisUtil.setKeyExpire(key, 30L);
}
// 增加修改次数
redisUtil.increaseCount(key);
}else {
throw new BusinessException("remaining.modifications", 1);
}
}
// 验证是否是本人进行邮箱绑定更改
public void verifyUserEmail(String verifyCode){
// 向旧邮箱发送验证码,以保证是当前邮箱拥有者在进行更改
String userEmail = baseMapper.selectById(UserContext.getUserHolder().getId()).getUserEmail();
//校验邮箱验证码
String verifyCodeCatch = LocalCacheUtils.getVerifyCodeCache(AuthenticationOperationTypeEnum.CHANGE_MAILBOX.name() + "_" + userEmail);
if (StringUtils.isBlank(verifyCodeCatch)) {
throw new BusinessException("the.verification.code.has.expired", ResultEnum.PROMPT.getCode());
}
if (!verifyCode.equals(verifyCodeCatch)) {
throw new BusinessException("verification.code.error", ResultEnum.PROMPT.getCode());
}
}
// 修改邮箱地址
public void changeUserEmail(String newMailbox){
AuthPrincipalVo userHolder = UserContext.getUserHolder();
Long accountId = userHolder.getId();
// 将新邮箱信息存储到redis
String key = RedisUtil.CHANGE_MAILBOX + accountId;
redisUtil.addToString(key, newMailbox, CommonConstant.CHANGE_MAILBOX_LINK_VALIDITY / 1000);
String username = userHolder.getUsername();
String token = jwtTokenHelper.createToken(accountId, newMailbox);
// 准备激活链接,链接应该要有有效期
String link = "?" + token;
// 向新邮箱发送邮件,邮件附带激活链接,点击链接进行验证
SendEmailUtil.changeMailboxConfirm(newMailbox, userHolder.getLanguage(), username, link);
}
// 验证激活链接
public void activateNewEmail(String token){
// 获取链接地址信息,更新指定用户邮箱
String emailAndId = jwtTokenHelper.parseToEmailAndId(token);
String newMailbox = emailAndId.substring(0, emailAndId.lastIndexOf("_"));
String accountId = emailAndId.substring(emailAndId.lastIndexOf("_") + 1);
// 与redis的数据对比
String key = RedisUtil.CHANGE_MAILBOX + accountId;
String preInfo = redisUtil.getFromString(key);
if (StringUtil.isNullOrEmpty(preInfo)){
throw new BusinessException("申请已过期", 1);
}else if (!preInfo.equals(newMailbox)){
throw new BusinessException("信息不匹配,请重新申请");
}
// 执行替换
log.info("执行邮箱替换用户id:{},新邮箱:{}", accountId, newMailbox);
Account account = new Account();
account.setUserEmail(newMailbox);
account.setId(Long.parseLong(accountId));
baseMapper.updateById(account);
log.info("邮箱绑定更改完成用户id:{},新邮箱:{}", accountId, newMailbox);
}
}