From 0bfdba7e3570b13583b7df52d12d2dab8e0ecd3e Mon Sep 17 00:00:00 2001 From: yjxx <1055683039@qq.com> Date: Sat, 19 Oct 2024 14:53:50 +0800 Subject: [PATCH] =?UTF-8?q?=E9=AA=8C=E6=94=B6=E9=80=9A=E7=94=A8=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=B9=B6=E8=BF=9B=E8=A1=8C=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...MessageDTO.java => UserModifyInfoDTO.java} | 2 +- .../DTO/UserModifyPasswordDTO.java | 2 +- .../config/MyMetaObjectHandler.java | 15 +++-------- .../config/WebConfig.java | 2 +- .../controller/AdminController.java | 4 --- .../controller/UserController.java | 27 ++++++++++--------- .../entity/User.java | 4 +-- 7 files changed, 22 insertions(+), 34 deletions(-) rename src/main/java/jty/expressdistributionsystem/DTO/{UserModifyMessageDTO.java => UserModifyInfoDTO.java} (90%) diff --git a/src/main/java/jty/expressdistributionsystem/DTO/UserModifyMessageDTO.java b/src/main/java/jty/expressdistributionsystem/DTO/UserModifyInfoDTO.java similarity index 90% rename from src/main/java/jty/expressdistributionsystem/DTO/UserModifyMessageDTO.java rename to src/main/java/jty/expressdistributionsystem/DTO/UserModifyInfoDTO.java index bbb1228..e83769c 100644 --- a/src/main/java/jty/expressdistributionsystem/DTO/UserModifyMessageDTO.java +++ b/src/main/java/jty/expressdistributionsystem/DTO/UserModifyInfoDTO.java @@ -5,7 +5,7 @@ import jakarta.validation.constraints.Pattern; import lombok.Data; @Data -public class UserModifyMessageDTO { +public class UserModifyInfoDTO { @NotBlank(message = "用户名不能为空") private String userName; diff --git a/src/main/java/jty/expressdistributionsystem/DTO/UserModifyPasswordDTO.java b/src/main/java/jty/expressdistributionsystem/DTO/UserModifyPasswordDTO.java index 61a8783..77dc6ee 100644 --- a/src/main/java/jty/expressdistributionsystem/DTO/UserModifyPasswordDTO.java +++ b/src/main/java/jty/expressdistributionsystem/DTO/UserModifyPasswordDTO.java @@ -8,7 +8,7 @@ import lombok.Data; public class UserModifyPasswordDTO { @NotBlank(message = "旧密码不能为空") @Pattern(regexp = "^.+$") - private String OldPassword; + private String oldPassword; @NotBlank(message = "新密码不能为空") @Pattern(regexp = "^.+$") diff --git a/src/main/java/jty/expressdistributionsystem/config/MyMetaObjectHandler.java b/src/main/java/jty/expressdistributionsystem/config/MyMetaObjectHandler.java index e1653c2..0bb66b2 100644 --- a/src/main/java/jty/expressdistributionsystem/config/MyMetaObjectHandler.java +++ b/src/main/java/jty/expressdistributionsystem/config/MyMetaObjectHandler.java @@ -8,24 +8,15 @@ import java.time.LocalDateTime; @Component public class MyMetaObjectHandler implements MetaObjectHandler { - private static final ThreadLocal skipUpdateFill = new ThreadLocal<>(); - - public static void setSkipUpdateFill(boolean skip) { - skipUpdateFill.set(skip); - } - @Override public void insertFill(MetaObject metaObject) { this.strictInsertFill(metaObject, "createTime", LocalDateTime::now, LocalDateTime.class); - if (skipUpdateFill.get() == null || !skipUpdateFill.get()) { - this.strictInsertFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class); - } + this.strictInsertFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class); } @Override public void updateFill(MetaObject metaObject) { - if (skipUpdateFill.get() == null || !skipUpdateFill.get()) { - this.strictUpdateFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class); - } + this.strictUpdateFill(metaObject, "updateTime", LocalDateTime::now, LocalDateTime.class); } } + diff --git a/src/main/java/jty/expressdistributionsystem/config/WebConfig.java b/src/main/java/jty/expressdistributionsystem/config/WebConfig.java index 166bfcc..2eb4bd7 100644 --- a/src/main/java/jty/expressdistributionsystem/config/WebConfig.java +++ b/src/main/java/jty/expressdistributionsystem/config/WebConfig.java @@ -13,6 +13,6 @@ public class WebConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(loginInterceptor).excludePathPatterns("/user/login", "/user/register"); + registry.addInterceptor(loginInterceptor).excludePathPatterns("/user/login", "/user/loginByAccount"); } } diff --git a/src/main/java/jty/expressdistributionsystem/controller/AdminController.java b/src/main/java/jty/expressdistributionsystem/controller/AdminController.java index fda8c58..2531b5a 100644 --- a/src/main/java/jty/expressdistributionsystem/controller/AdminController.java +++ b/src/main/java/jty/expressdistributionsystem/controller/AdminController.java @@ -50,11 +50,7 @@ public class AdminController { return new Result<>(200, "删除成功", ""); } } - // 禁用自动填充 - MyMetaObjectHandler.setSkipUpdateFill(true); userService.updateById(user); - // 开启自动填充 - MyMetaObjectHandler.setSkipUpdateFill(false); return new Result<>(200, "操作成功", ""); } diff --git a/src/main/java/jty/expressdistributionsystem/controller/UserController.java b/src/main/java/jty/expressdistributionsystem/controller/UserController.java index ff13fdd..cff2ff9 100644 --- a/src/main/java/jty/expressdistributionsystem/controller/UserController.java +++ b/src/main/java/jty/expressdistributionsystem/controller/UserController.java @@ -4,7 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import jakarta.annotation.Resource; import jty.expressdistributionsystem.DTO.UserLoginByAccountDTO; -import jty.expressdistributionsystem.DTO.UserModifyMessageDTO; +import jty.expressdistributionsystem.DTO.UserModifyInfoDTO; import jty.expressdistributionsystem.DTO.UserModifyPasswordDTO; import jty.expressdistributionsystem.config.MyMetaObjectHandler; import jty.expressdistributionsystem.entity.Result; @@ -14,9 +14,11 @@ import jty.expressdistributionsystem.utils.GetIdUtil; import jty.expressdistributionsystem.utils.JwtUtil; import jty.expressdistributionsystem.utils.Md5Util; import jty.expressdistributionsystem.utils.ThreadLocalUtil; +import lombok.extern.slf4j.Slf4j; import org.jetbrains.annotations.NotNull; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; +import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; @@ -28,6 +30,7 @@ import java.util.concurrent.TimeUnit; @RestController @RequestMapping("/user") +@Slf4j public class UserController { @Resource private UserService userService; @@ -67,8 +70,6 @@ public class UserController { if(user.getDisabled() == 1){ return new Result<>(403, "该账号已被禁用, 请联系管理员", ""); } - // 禁用自动填充 - MyMetaObjectHandler.setSkipUpdateFill(true); user.setLoginTime(LocalDateTime.now()); userService.update(user, new UpdateWrapper().eq("id", user.getId())); // 校验通过 @@ -81,40 +82,39 @@ public class UserController { ValueOperations stringStringValueOperations = stringRedisTemplate.opsForValue(); // 设置过期时间(1天) stringStringValueOperations.set(token, token, 1, TimeUnit.DAYS); - // 开启自动填充 - MyMetaObjectHandler.setSkipUpdateFill(false); // 返回token给前端 return new Result<>(200, "登录成功", token); } // 修改个人信息 @PutMapping("/modify") - public Result modifyMessage(@RequestBody @Validated @NotNull UserModifyMessageDTO userModifyMessageDTO) { + public Result modifyInfo(@RequestBody @Validated @NotNull UserModifyInfoDTO userModifyInfoDTO) { // 获取登录用户id Long id = GetIdUtil.getId(); User user = userService.getById(id); // 检查phone和userName是否与当前用户信息一致 - boolean isPhoneSame = user.getPhone().equals(userModifyMessageDTO.getPhone()); - boolean isUserNameSame = user.getUserName().equals(userModifyMessageDTO.getUserName()); + boolean isPhoneSame = user.getPhone().equals(userModifyInfoDTO.getPhone()); + boolean isUserNameSame = user.getUserName().equals(userModifyInfoDTO.getUserName()); if (isPhoneSame && isUserNameSame) { return new Result<>(400, "请修改您的信息", ""); } QueryWrapper queryWrapper = new QueryWrapper<>(); if (!isPhoneSame) { - queryWrapper.eq("phone", userModifyMessageDTO.getPhone()); + queryWrapper.eq("phone", userModifyInfoDTO.getPhone()); if (userService.getOne(queryWrapper) != null) { return new Result<>(400, "该手机号已被注册", ""); } - user.setPhone(userModifyMessageDTO.getPhone()); + user.setPhone(userModifyInfoDTO.getPhone()); } queryWrapper.clear(); if (!isUserNameSame) { - queryWrapper.eq("user_name", userModifyMessageDTO.getUserName()); + queryWrapper.eq("user_name", userModifyInfoDTO.getUserName()); if (userService.getOne(queryWrapper) != null) { return new Result<>(400, "该用户名已被使用", ""); } - user.setUserName(userModifyMessageDTO.getUserName()); + user.setUserName(userModifyInfoDTO.getUserName()); } + user.setUpdateTime(null); userService.updateById(user); return new Result<>(200, "用户信息修改成功", user); } @@ -133,9 +133,10 @@ public class UserController { Long id = GetIdUtil.getId(); User user = userService.getById(id); if (!user.getIdentity().equals(userModifyPasswordDTO.getIdentity())) { - return new Result<>(409, "请输入您的身份证", ""); + return new Result<>(409, "身份证有误", ""); } user.setPassword(Md5Util.getMD5String(userModifyPasswordDTO.getNewPassword())); + user.setUpdateTime(null); userService.updateById(user); // 删除redis当中的token stringRedisTemplate.delete(token); diff --git a/src/main/java/jty/expressdistributionsystem/entity/User.java b/src/main/java/jty/expressdistributionsystem/entity/User.java index 243bffb..1e21d62 100644 --- a/src/main/java/jty/expressdistributionsystem/entity/User.java +++ b/src/main/java/jty/expressdistributionsystem/entity/User.java @@ -60,9 +60,9 @@ public class User implements Serializable { @TableField(value = "create_time", fill = FieldFill.INSERT) private LocalDateTime createTime; - @TableField(value = "login_time", fill = FieldFill.UPDATE) + @TableField(value = "login_time") private LocalDateTime loginTime; - @TableField(value = "update_time", fill = FieldFill.INSERT) + @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE) private LocalDateTime updateTime; }