编辑用户名,改为当前月允许修改5次

This commit is contained in:
2025-01-08 14:20:12 +08:00
parent 9f37cb3f8d
commit e4936a23bc
7 changed files with 41 additions and 23 deletions

View File

@@ -173,7 +173,7 @@ public interface AccountService extends IService<Account> {
void registerUserToVisitor();
Map<String, Long> getNicknameModifyTimes();
Long getNicknameModifyTimes();
void editUserName(String newUserName);

View File

@@ -1830,36 +1830,36 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
return redisUtil.getPersonalHomepageViewCount(accountId);
}
// 获取当前用户30天内 剩余昵称修改次数
public Map<String, Long> getNicknameModifyTimes(){
// 获取当前用户本月内 剩余昵称修改次数
public 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;
// 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 5L - times;
}
// 修改用户名 允许用户30天内修改5次
// 修改用户名 允许用户当前月内修改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");
// Map<String, Long> remainTimes = getNicknameModifyTimes();
Long remainingModifyTimes = getNicknameModifyTimes();
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);
Account account = baseMapper.selectById(accountId);
// 当新昵称与旧昵称不同时,修改
if (!account.getUserName().equals(newUserName)){
account.setUserName(newUserName);
account.setId(accountId);
baseMapper.updateById(account);
// 每个月初清除RedisUtil.NICKNAME_MODIFY_TIMES下的所有记录
String key = RedisUtil.NICKNAME_MODIFY_TIMES + accountId;
// 增加修改次数
redisUtil.increaseCount(key);
}
// 增加修改次数
redisUtil.increaseCount(key);
}else {
throw new BusinessException("remaining.modifications", 1);
}