编辑用户名,改为当前月允许修改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

@@ -1,5 +1,6 @@
package com.ai.da.common.task;
import com.ai.da.common.utils.RedisUtil;
import com.ai.da.mapper.primary.entity.Account;
import com.ai.da.service.AccountService;
import lombok.extern.slf4j.Slf4j;
@@ -15,6 +16,8 @@ public class AccountTask {
@Resource
private AccountService accountService;
@Resource
private RedisUtil redisUtil;
/**
* 每周日晚上刷新 年付用户、月付用户的积分
@@ -69,4 +72,11 @@ public class AccountTask {
public void registerUserToVisitor() {
accountService.registerUserToVisitor();
}
@Scheduled(cron = "0 0 0 1 * ?")
// 每月初刷新所有用户用户名剩余修改次数
public void resetUsernameModifyTimes(){
log.info("重置所有用户的用户名修改次数");
redisUtil.batchDeleteKeysWithSamePrefix(RedisUtil.NICKNAME_MODIFY_TIMES);
}
}

View File

@@ -102,7 +102,7 @@ public class PaymentTask {
}
@Scheduled(cron = "0 */5 * * * *") // Run every 5 minutes
// @Scheduled(cron = "0 */5 * * * *") // Run every 5 minutes
public void updateAffiliateInfoWithPayment(){
affiliateService.updateAffiliateInfoWithPayment();
}

View File

@@ -292,4 +292,12 @@ public class RedisUtil {
String key = AFFILIATE_LINK_VIEW_KEY + accountId;
return redisTemplate.opsForValue().increment(key, 0);
}
public void batchDeleteKeysWithSamePrefix(String prefix){
Set<String> keys = redisTemplate.keys(prefix + "*");
assert keys != null;
if (!keys.isEmpty()){
redisTemplate.delete(keys);
}
}
}

View File

@@ -224,7 +224,7 @@ public class AccountController {
@ApiOperation(value = "getUsernameModifyTimes")
@GetMapping("/getNicknameModifyTimes")
public Response<Map<String, Long>> getNicknameModifyTimes(){
public Response<Long> getNicknameModifyTimes(){
return Response.success(accountService.getNicknameModifyTimes());
}

View File

@@ -71,6 +71,6 @@ public class AccountLoginVO {
private String occupation;
private Map<String, Long> usernameModify;
private Long usernameModify;
}

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);
}