Skip to content

Commit 9a17ba0

Browse files
committed
feat(user): update user info update frequency and improve error message
1 parent 6f850fa commit 9a17ba0

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

flow-client/src/main/java/com/ligg/flowclient/controller/FlowUserController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public Result<FlowUserVo> getUserInfo(
3939
}
4040

4141
/**
42-
* 更新当前登录用户资料,每位用户每天仅可更新一次。
42+
* 更新当前登录用户资料
4343
*/
4444
@PutMapping
4545
public Result<FlowUserVo> updateUserInfo(

flow-client/src/main/java/com/ligg/flowclient/service/impl/UserServiceImpl.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
public class UserServiceImpl implements UserService {
4444

4545
private static final int DAILY_LIMIT_SECONDS = 86_400;
46+
private static final int USER_INFO_UPDATE_INTERVAL_SECONDS = 180;
4647
private static final long MAX_AVATAR_SIZE = 2 * 1024 * 1024;
4748
private static final Set<String> ALLOWED_AVATAR_TYPES = Set.of(
4849
"image/jpeg", "image/png", "image/webp", "image/gif"
@@ -259,14 +260,19 @@ private void markPasswordResetDaily(String email) {
259260

260261
private void checkUserInfoUpdateDailyLimit(Long userId) {
261262
String key = Constants.USER_INFO_UPDATE_DAILY_KEY + ':' + userId;
262-
if (redisTemplate.hasKey(key)) {
263-
throw new IllegalArgumentException("今日已更新过用户资料,请明天再试");
263+
long ttl = redisTemplate.getExpire(key, TimeUnit.SECONDS);
264+
if (ttl > 0) {
265+
long minutes = ttl / 60;
266+
long seconds = ttl % 60;
267+
throw new IllegalArgumentException(
268+
"用户信息更新过于频繁,请 %d 分 %d 秒后再试".formatted(minutes, seconds)
269+
);
264270
}
265271
}
266272

267273
private void markUserInfoUpdateDaily(Long userId) {
268274
String key = Constants.USER_INFO_UPDATE_DAILY_KEY + ':' + userId;
269-
redisTemplate.opsForValue().set(key, "1", DAILY_LIMIT_SECONDS, TimeUnit.SECONDS);
275+
redisTemplate.opsForValue().set(key, "1", USER_INFO_UPDATE_INTERVAL_SECONDS, TimeUnit.SECONDS);
270276
}
271277

272278
private FlowUserVo loadUserVo(Long userId) {

0 commit comments

Comments
 (0)