diff --git a/src/main/java/jty/expressdistributionsystem/controller/CommonController.java b/src/main/java/jty/expressdistributionsystem/controller/CommonController.java new file mode 100644 index 0000000..8ab10e9 --- /dev/null +++ b/src/main/java/jty/expressdistributionsystem/controller/CommonController.java @@ -0,0 +1,155 @@ +package jty.expressdistributionsystem.controller; + +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.UserModifyInfoDTO; +import jty.expressdistributionsystem.DTO.UserModifyPasswordDTO; +import jty.expressdistributionsystem.entity.Result; +import jty.expressdistributionsystem.entity.User; +import jty.expressdistributionsystem.service.UserService; +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.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import java.time.LocalDateTime; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.TimeUnit; + +@RestController +@RequestMapping("/common") +@Slf4j +public class CommonController { + @Resource + private UserService userService; + + @Resource + private StringRedisTemplate stringRedisTemplate; + + // 注册 + @PostMapping("/register") + public Result register(@RequestBody @Validated @NotNull User user) { + // 查找用户是否存在 + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("user_name", user.getUserName()) + .or() + .eq("account", user.getAccount()) + .or() + .eq("phone", user.getPhone()); + User u = userService.getOne(queryWrapper); + if (u != null) { + return new Result<>(409, "账号或用户名或手机号已存在", ""); + } + // 密码加密 + user.setPassword(Md5Util.getMD5String(user.getPassword())); + // 用户数据入库 + userService.save(user); + return new Result<>(200, "注册成功", ""); + } + + // 通过账号登录 + @PostMapping("/loginByAccount") + public Result login(@RequestBody @Validated @NotNull UserLoginByAccountDTO userLoginByAccountDTO) { + // 查询用户是否存在 + Optional optionalUser = Optional.ofNullable(userService.getOne(new QueryWrapper() + .eq("account", userLoginByAccountDTO.getAccount()))); + if (optionalUser.isEmpty() || !Md5Util.getMD5String(userLoginByAccountDTO.getPassword()).equals(optionalUser.get().getPassword())) { + return new Result<>(401, "账号或者密码错误", ""); + } + User user = optionalUser.get(); + if(user.getDisabled() == 1){ + return new Result<>(403, "该账号已被禁用, 请联系管理员", ""); + } + user.setLoginTime(LocalDateTime.now()); + userService.update(user, new UpdateWrapper().eq("id", user.getId())); + // 校验通过 + Map claims = new HashMap<>(); + claims.put("id", user.getId()); + claims.put("account", userLoginByAccountDTO.getAccount()); + // 获取token + String token = JwtUtil.genToken(claims); + // 存放token到redis当中进行持久化存储 + ValueOperations stringStringValueOperations = stringRedisTemplate.opsForValue(); + // 设置过期时间(1天) + stringStringValueOperations.set(token, token, 1, TimeUnit.DAYS); + // 返回token给前端 + return new Result<>(200, "登录成功", token); + } + + // 修改个人信息 + @PutMapping("/modify") + 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(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", userModifyInfoDTO.getPhone()); + if (userService.getOne(queryWrapper) != null) { + return new Result<>(400, "该手机号已被注册", ""); + } + user.setPhone(userModifyInfoDTO.getPhone()); + } + queryWrapper.clear(); + if (!isUserNameSame) { + queryWrapper.eq("user_name", userModifyInfoDTO.getUserName()); + if (userService.getOne(queryWrapper) != null) { + return new Result<>(400, "该用户名已被使用", ""); + } + user.setUserName(userModifyInfoDTO.getUserName()); + } + user.setUpdateTime(null); + userService.updateById(user); + return new Result<>(200, "用户信息修改成功", user); + } + + // 修改个人密码 + @PutMapping("/rePassword") + public Result modifyPassword(@RequestHeader("Authorization") String token, + @RequestBody @Validated @NotNull UserModifyPasswordDTO userModifyPasswordDTO) { + // 判断密码 + if (userModifyPasswordDTO.getOldPassword().equals(userModifyPasswordDTO.getNewPassword())) { + return new Result<>(409, "新密码不得和旧密码一致", ""); + } + if (!userModifyPasswordDTO.getNewPassword().equals(userModifyPasswordDTO.getReNewPassword())) { + return new Result<>(409, "两次输入密码不相同", ""); + } + Long id = GetIdUtil.getId(); + User user = userService.getById(id); + if (!user.getIdentity().equals(userModifyPasswordDTO.getIdentity())) { + return new Result<>(409, "身份证有误", ""); + } + user.setPassword(Md5Util.getMD5String(userModifyPasswordDTO.getNewPassword())); + user.setUpdateTime(null); + userService.updateById(user); + // 删除redis当中的token + stringRedisTemplate.delete(token); + // 删除当前线程的存储信息 + ThreadLocalUtil.remove(); + return new Result<>(200, "密码修改成功, 请重新登陆", ""); + } + + // 获取个人信息 + @GetMapping("/info") + public Result getInfo() { + Long id = GetIdUtil.getId(); + User user = userService.getById(id); + return new Result<>(200, "", user); + } +}