优化用添加个人信息功能
This commit is contained in:
@@ -71,10 +71,10 @@ public class UserController {
|
||||
* @return 更新后的用户信息
|
||||
*/
|
||||
@PostMapping("/info/update")
|
||||
public Result userInfoUpdate(@RequestBody UserInfoUpdateDTO userInfoUpdateDTO) {
|
||||
public Result<UserInfoVO> userInfoUpdate(@RequestBody UserInfoUpdateDTO userInfoUpdateDTO) {
|
||||
Long userId = UserContext.getUserId();
|
||||
userInfoUpdateDTO.setUserId(userId);
|
||||
userService.updateUserInfo(userInfoUpdateDTO);
|
||||
return Result.success(ResultCode.SUCCESS, "用户信息更新成功", null);
|
||||
UserInfoVO userInfoVO =userService.updateUserInfo(userInfoUpdateDTO);
|
||||
return Result.success(ResultCode.SUCCESS, "用户信息更新成功", userInfoVO);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.util.List;
|
||||
@Data
|
||||
public class UserInfoUpdateDTO {
|
||||
private Long userId;
|
||||
private String userName;
|
||||
private String nickname;
|
||||
private String avatarUrl;
|
||||
private String backgroundUrl;
|
||||
@@ -17,4 +18,6 @@ public class UserInfoUpdateDTO {
|
||||
private List<String> hobbies;
|
||||
private String signature;
|
||||
private LocalDateTime updatedAt;
|
||||
/** 需要清空的字段 */
|
||||
private List<String> clearFields;
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import java.util.List;
|
||||
@Data
|
||||
public class UserInfoVO {
|
||||
private Long userId;
|
||||
private String userName;
|
||||
private String nickname;
|
||||
private String avatarUrl;
|
||||
private String backgroundUrl;
|
||||
|
||||
@@ -41,5 +41,5 @@ public interface UserService {
|
||||
* 更新用户信息
|
||||
* @param userInfoUpdateDTO 用户信息
|
||||
*/
|
||||
void updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO);
|
||||
UserInfoVO updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import com.bao.dating.mapper.UserMapper;
|
||||
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
|
||||
import com.bao.dating.pojo.dto.UserLoginDTO;
|
||||
import com.bao.dating.pojo.entity.User;
|
||||
import com.bao.dating.pojo.vo.PostEditVO;
|
||||
import com.bao.dating.pojo.vo.UserInfoVO;
|
||||
import com.bao.dating.pojo.vo.UserLoginVO;
|
||||
import com.bao.dating.service.UserService;
|
||||
@@ -18,7 +17,6 @@ import com.bao.dating.util.MD5Util;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
|
||||
@@ -30,6 +28,11 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 用户服务实现类
|
||||
*
|
||||
* @author KilLze
|
||||
*/
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
@@ -48,7 +51,7 @@ public class UserServiceImpl implements UserService {
|
||||
/**
|
||||
* 用户登录
|
||||
*
|
||||
* @param userLoginDTO
|
||||
* @param userLoginDTO 登录参数
|
||||
* @return 登录信息
|
||||
*/
|
||||
@Override
|
||||
@@ -84,7 +87,7 @@ public class UserServiceImpl implements UserService {
|
||||
/**
|
||||
* 获取用户信息
|
||||
*
|
||||
* @param userId
|
||||
* @param userId 用户ID
|
||||
* @return 用户信息
|
||||
*/
|
||||
@Override
|
||||
@@ -98,6 +101,12 @@ public class UserServiceImpl implements UserService {
|
||||
return userInfoVO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传头像接口
|
||||
*
|
||||
* @param file 头像文件
|
||||
* @return 上传后的文件URL
|
||||
*/
|
||||
@Override
|
||||
public String uploadAvatar(MultipartFile file) {
|
||||
// 参数校验
|
||||
@@ -137,6 +146,12 @@ public class UserServiceImpl implements UserService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传背景图片
|
||||
*
|
||||
* @param file 背景图片文件
|
||||
* @return 上传后的文件URL
|
||||
*/
|
||||
@Override
|
||||
public String uploadBackground(MultipartFile file) {
|
||||
// 参数校验
|
||||
@@ -178,11 +193,10 @@ public class UserServiceImpl implements UserService {
|
||||
/**
|
||||
* 更新用户信息
|
||||
*
|
||||
* @param userInfoUpdateDTO
|
||||
* @return 用户信息
|
||||
* @param userInfoUpdateDTO 用户信息更新参数
|
||||
*/
|
||||
@Override
|
||||
public void updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO) {
|
||||
public UserInfoVO updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO) {
|
||||
Long userId = userInfoUpdateDTO.getUserId();
|
||||
User user = userMapper.selectByUserId(userId);
|
||||
if (user == null) {
|
||||
@@ -247,10 +261,19 @@ public class UserServiceImpl implements UserService {
|
||||
throw new RuntimeException("头像或背景图需要人工审核,暂无法修改");
|
||||
}
|
||||
}
|
||||
|
||||
// 默认昵称兜底
|
||||
if (userInfoUpdateDTO.getNickname() == null || userInfoUpdateDTO.getNickname().trim().isEmpty()) {
|
||||
userInfoUpdateDTO.setNickname(user.getUserName());
|
||||
}
|
||||
userInfoUpdateDTO.setUpdatedAt(LocalDateTime.now());
|
||||
|
||||
// 更新数据库
|
||||
userMapper.updateUserInfoByUserId(userInfoUpdateDTO);
|
||||
|
||||
// 封装返回结果
|
||||
User updatedUser = userMapper.selectByUserId(userInfoUpdateDTO.getUserId());
|
||||
UserInfoVO userInfoVO = new UserInfoVO();
|
||||
BeanUtils.copyProperties(updatedUser, userInfoVO);
|
||||
return userInfoVO;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
<!--根据用户id查询用户信息-->
|
||||
<resultMap id="UserResultMap" type="com.bao.dating.pojo.entity.User">
|
||||
<id property="userId" column="user_id"/>
|
||||
<result property="userName" column="user_name"/>
|
||||
<result property="nickname" column="nickname"/>
|
||||
<result property="avatarUrl" column="avatar_url"/>
|
||||
<result property="backgroundUrl" column="background_url"/>
|
||||
@@ -31,6 +32,7 @@
|
||||
<select id="selectByUserId" resultMap="UserResultMap">
|
||||
SELECT
|
||||
user_id,
|
||||
user_name,
|
||||
nickname,
|
||||
avatar_url,
|
||||
background_url,
|
||||
|
||||
Reference in New Issue
Block a user