|
|
@@ -0,0 +1,91 @@
|
|
|
+package com.yingpai.gupiao.service.impl;
|
|
|
+
|
|
|
+import com.yingpai.gupiao.domain.dto.UpdateProfileDTO;
|
|
|
+import com.yingpai.gupiao.domain.po.User;
|
|
|
+import com.yingpai.gupiao.domain.vo.LoginVO;
|
|
|
+import com.yingpai.gupiao.mapper.UserMapper;
|
|
|
+import com.yingpai.gupiao.service.UserService;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.time.LocalDateTime;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 用户服务实现类
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class UserServiceImpl implements UserService {
|
|
|
+
|
|
|
+ private final UserMapper userMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取用户信息
|
|
|
+ * @param userId 用户ID
|
|
|
+ * @return 用户信息VO
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public LoginVO.UserInfoVO getUserInfo(Long userId) {
|
|
|
+ User user = userMapper.selectById(userId);
|
|
|
+ if (user == null) {
|
|
|
+ throw new RuntimeException("用户不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 手机号脱敏
|
|
|
+ String maskedPhone = null;
|
|
|
+ if (user.getPhone() != null) {
|
|
|
+ maskedPhone = user.getPhone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
|
|
|
+ }
|
|
|
+
|
|
|
+ return LoginVO.UserInfoVO.builder()
|
|
|
+ .id(user.getId())
|
|
|
+ .nickname(user.getNickname())
|
|
|
+ .avatar(user.getAvatar())
|
|
|
+ .phone(maskedPhone)
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新用户资料
|
|
|
+ * @param userId 用户ID
|
|
|
+ * @param updateProfileDTO 更新资料DTO
|
|
|
+ * @return 更新后的用户信息VO
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public LoginVO.UserInfoVO updateProfile(Long userId, UpdateProfileDTO updateProfileDTO) {
|
|
|
+ User user = userMapper.selectById(userId);
|
|
|
+ if (user == null) {
|
|
|
+ throw new RuntimeException("用户不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ boolean needUpdate = false;
|
|
|
+
|
|
|
+ // 更新昵称
|
|
|
+ if (updateProfileDTO.getNickname() != null &&
|
|
|
+ !updateProfileDTO.getNickname().isEmpty() &&
|
|
|
+ !updateProfileDTO.getNickname().equals(user.getNickname())) {
|
|
|
+ user.setNickname(updateProfileDTO.getNickname());
|
|
|
+ needUpdate = true;
|
|
|
+ log.info("更新用户昵称,userId: {}, 新昵称: {}", userId, updateProfileDTO.getNickname());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新头像
|
|
|
+ if (updateProfileDTO.getAvatar() != null &&
|
|
|
+ !updateProfileDTO.getAvatar().isEmpty() &&
|
|
|
+ !updateProfileDTO.getAvatar().equals(user.getAvatar())) {
|
|
|
+ user.setAvatar(updateProfileDTO.getAvatar());
|
|
|
+ needUpdate = true;
|
|
|
+ log.info("更新用户头像,userId: {}, 新头像: {}", userId, updateProfileDTO.getAvatar());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (needUpdate) {
|
|
|
+ user.setUpdateTime(LocalDateTime.now());
|
|
|
+ userMapper.updateById(user);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 返回更新后的用户信息
|
|
|
+ return getUserInfo(userId);
|
|
|
+ }
|
|
|
+}
|