添加用户管理
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package org.jiayunet.controller;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.jiayunet.pojo.dto.user.UserInfoDto;
|
||||
import org.jiayunet.pojo.param.user.UserInfoParam;
|
||||
import org.jiayunet.service.UserManageService;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* 用户账号管理接口
|
||||
*
|
||||
* @author zk
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/user/manage")
|
||||
@AllArgsConstructor
|
||||
public class UserManageController {
|
||||
|
||||
private final UserManageService userManageService;
|
||||
|
||||
/**
|
||||
* 查看个人信息
|
||||
*/
|
||||
@GetMapping("/info")
|
||||
public UserInfoDto getInfo() {
|
||||
return userManageService.getInfo();
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改个人信息
|
||||
*/
|
||||
@PostMapping("/info")
|
||||
public void updateInfo(@Validated @RequestBody UserInfoParam param) {
|
||||
userManageService.updateInfo(param);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销账号
|
||||
*/
|
||||
@PostMapping("/cancel")
|
||||
public void cancelAccount() {
|
||||
userManageService.cancelAccount();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.jiayunet.pojo.dto.user;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
/**
|
||||
* 用户个人信息DTO
|
||||
*
|
||||
* @author zk
|
||||
*/
|
||||
@Data
|
||||
public class UserInfoDto {
|
||||
/** 用户ID */
|
||||
private Long id;
|
||||
/** 手机号 */
|
||||
private String mobileNumber;
|
||||
/** 邮箱 */
|
||||
private String email;
|
||||
/** 昵称 */
|
||||
private String nick;
|
||||
/** 真实姓名 */
|
||||
private String realName;
|
||||
/** 头像 */
|
||||
private String picture;
|
||||
/** 生日 */
|
||||
private Instant birthday;
|
||||
/** 性别 1男 2女 */
|
||||
private Integer sex;
|
||||
/** 邀请码 */
|
||||
private String inviteCode;
|
||||
/** 注册时间 */
|
||||
private Instant createTime;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package org.jiayunet.pojo.param.user;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
/**
|
||||
* 修改个人信息入参
|
||||
*
|
||||
* @author zk
|
||||
*/
|
||||
@Data
|
||||
public class UserInfoParam {
|
||||
/** 昵称 */
|
||||
private String nick;
|
||||
/** 头像 */
|
||||
private String picture;
|
||||
/** 生日 */
|
||||
private Instant birthday;
|
||||
/** 性别 1男 2女 */
|
||||
private Integer sex;
|
||||
/** 真实姓名 */
|
||||
private String realName;
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package org.jiayunet.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jiayunet.constant.PreRedisKeyName;
|
||||
import org.jiayunet.mapper.UserMapper;
|
||||
import org.jiayunet.pojo.dto.user.UserInfoDto;
|
||||
import org.jiayunet.pojo.param.user.UserInfoParam;
|
||||
import org.jiayunet.pojo.po.User;
|
||||
import org.jiayunet.tool.UserSecurityTool;
|
||||
import org.jiayunet.tool.server.RedisServerTool;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* 用户账号管理服务
|
||||
* <p>主要功能:查看个人信息、修改个人信息、注销账号</p>
|
||||
* <p>使用表:bg_user(查询/更新/逻辑删除)</p>
|
||||
* <p>使用Redis:login:token:{userId}(注销时清理登录态)</p>
|
||||
*
|
||||
* @author zk
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class UserManageService {
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Autowired
|
||||
private RedisServerTool redisServerTool;
|
||||
|
||||
/**
|
||||
* 查看个人信息
|
||||
* <p>根据当前登录用户ID查询用户基本信息</p>
|
||||
*/
|
||||
public UserInfoDto getInfo() {
|
||||
Long userId = UserSecurityTool.getUserId();
|
||||
User user = userMapper.selectById(userId);
|
||||
Assert.notNull(user, "用户不存在");
|
||||
|
||||
UserInfoDto dto = new UserInfoDto();
|
||||
BeanUtils.copyProperties(user, dto);
|
||||
return dto;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改个人信息
|
||||
* <p>仅更新非null字段</p>
|
||||
*/
|
||||
public void updateInfo(UserInfoParam param) {
|
||||
Long userId = UserSecurityTool.getUserId();
|
||||
LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<User>().eq(User::getId, userId);
|
||||
|
||||
if (param.getNick() != null) wrapper.set(User::getNick, param.getNick());
|
||||
if (param.getPicture() != null) wrapper.set(User::getPicture, param.getPicture());
|
||||
if (param.getBirthday() != null) wrapper.set(User::getBirthday, param.getBirthday());
|
||||
if (param.getSex() != null) wrapper.set(User::getSex, param.getSex());
|
||||
if (param.getRealName() != null) wrapper.set(User::getRealName, param.getRealName());
|
||||
|
||||
userMapper.update(null, wrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注销账号
|
||||
* <p>1. 逻辑删除用户 2. 清理Redis登录态</p>
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void cancelAccount() {
|
||||
Long userId = UserSecurityTool.getUserId();
|
||||
|
||||
// 逻辑删除
|
||||
userMapper.deleteById(userId);
|
||||
|
||||
// 清理Redis登录态
|
||||
redisServerTool.delete(PreRedisKeyName.LOGIN_TOKEN + userId);
|
||||
|
||||
log.info("用户注销账号 userId:{}", userId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user