Browse Source

S-cfc-486 auth.profile.edit: 实现个人资料编辑契约使红单测转绿 (TDD-GREEN)

新增 AuthProfileService(editProfile/validatePhone/validateNickname/confirmSensitiveChange) + AuthProfileEditDTO(@Pattern),AuthController /profile/edit 端点;既有 /api/user/update 载体透传 phone 并统一拦截非法手机/emoji超长昵称;17/17 用例全绿;同步 API_REFERENCE/PROJECT-OVERVIEW。
Agnes Agent 3 hours ago
parent
commit
ef2dc6ab78

+ 1 - 0
cfc-backend/src/main/java/com/etotem/cfc/controller/UserController.java

@@ -135,6 +135,7 @@ public class UserController {
         com.etotem.cfc.dto.UpdateUserDTO dto = new com.etotem.cfc.dto.UpdateUserDTO();
         com.etotem.cfc.dto.UpdateUserDTO dto = new com.etotem.cfc.dto.UpdateUserDTO();
         if (params.containsKey("nickname")) dto.setNickname((String) params.get("nickname"));
         if (params.containsKey("nickname")) dto.setNickname((String) params.get("nickname"));
         if (params.containsKey("avatar")) dto.setAvatar((String) params.get("avatar"));
         if (params.containsKey("avatar")) dto.setAvatar((String) params.get("avatar"));
+        if (params.containsKey("phone")) dto.setPhone((String) params.get("phone"));
         if (params.containsKey("realName")) dto.setRealName((String) params.get("realName"));
         if (params.containsKey("realName")) dto.setRealName((String) params.get("realName"));
         if (params.containsKey("role")) dto.setRole((String) params.get("role"));
         if (params.containsKey("role")) dto.setRole((String) params.get("role"));
         if (params.containsKey("gender")) dto.setGender((String) params.get("gender"));
         if (params.containsKey("gender")) dto.setGender((String) params.get("gender"));

+ 15 - 0
cfc-backend/src/main/java/com/etotem/cfc/controller/auth/AuthController.java

@@ -7,6 +7,7 @@ import com.etotem.cfc.dto.RegisterWithIdCardDTO;
 import com.etotem.cfc.dto.RegisterWithInviteCodeDTO;
 import com.etotem.cfc.dto.RegisterWithInviteCodeDTO;
 import com.etotem.cfc.entity.User;
 import com.etotem.cfc.entity.User;
 import com.etotem.cfc.mapper.UserMapper;
 import com.etotem.cfc.mapper.UserMapper;
+import com.etotem.cfc.service.AuthProfileService;
 import com.etotem.cfc.service.SsoService;
 import com.etotem.cfc.service.SsoService;
 import com.etotem.cfc.service.UserService;
 import com.etotem.cfc.service.UserService;
 import com.etotem.cfc.service.VerificationCodeService;
 import com.etotem.cfc.service.VerificationCodeService;
@@ -43,6 +44,9 @@ public class AuthController {
     @Resource
     @Resource
     private SsoService ssoService;
     private SsoService ssoService;
 
 
+    @Resource
+    private AuthProfileService authProfileService;
+
     /**
     /**
      * 发送验证码
      * 发送验证码
      */
      */
@@ -394,4 +398,15 @@ public class AuthController {
         }
         }
     }
     }
 
 
+    /**
+     * 个人资料编辑(SC-P-64 / auth.profile.edit):编辑昵称 / 头像 / 绑定手机。
+     * 手机号格式与昵称边界非法 → 400;绑定手机变更需 verifyCode 二次确认。
+     */
+    @Operation(summary = "个人资料编辑(昵称/头像/绑定手机)")
+    @PostMapping("/profile/edit")
+    public Result<Boolean> editProfile(@RequestAttribute("userId") Long userId,
+                                       @RequestBody AuthProfileEditDTO dto) {
+        return authProfileService.editProfile(userId, dto);
+    }
+
 }
 }

+ 28 - 0
cfc-backend/src/main/java/com/etotem/cfc/dto/AuthProfileEditDTO.java

@@ -0,0 +1,28 @@
+package com.etotem.cfc.dto;
+
+import lombok.Data;
+
+import javax.validation.constraints.Pattern;
+
+/**
+ * 个人资料编辑请求(SC-P-64 / auth.profile.edit)。
+ *
+ * <p>承载昵称 / 头像 / 绑定手机三项编辑内容;绑定手机属敏感字段,
+ * 变更时须携带短信验证码 {@code verifyCode} 做二次确认。</p>
+ */
+@Data
+public class AuthProfileEditDTO {
+
+    /** 昵称(超长 / emoji 由 AuthProfileService.validateNickname 拦截) */
+    private String nickname;
+
+    /** 头像地址 */
+    private String avatar;
+
+    /** 手机号:非法格式 → 400 */
+    @Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确")
+    private String phone;
+
+    /** 敏感字段(绑定手机)变更的短信验证码 */
+    private String verifyCode;
+}

+ 2 - 0
cfc-backend/src/main/java/com/etotem/cfc/dto/UpdateUserDTO.java

@@ -6,6 +6,8 @@ import lombok.Data;
 public class UpdateUserDTO {
 public class UpdateUserDTO {
     private String nickname;
     private String nickname;
     private String avatar;
     private String avatar;
+    /** 绑定手机(SC-P-64 / auth.profile.edit) */
+    private String phone;
     private String realName;
     private String realName;
     private String role;
     private String role;
     private java.util.List<String> roles;
     private java.util.List<String> roles;

+ 132 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/AuthProfileService.java

@@ -0,0 +1,132 @@
+package com.etotem.cfc.service;
+
+import com.etotem.cfc.common.Result;
+import com.etotem.cfc.dto.AuthProfileEditDTO;
+import com.etotem.cfc.dto.UpdateUserDTO;
+import com.etotem.cfc.entity.User;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.regex.Pattern;
+
+/**
+ * 个人资料编辑统一服务(SC-P-64 / {@code auth.profile.edit})。
+ *
+ * <p>编辑昵称 / 头像 / 绑定手机的统一入口;手机号格式与昵称边界(超长 / emoji)校验
+ * 集中在此,敏感字段(绑定手机)变更须短信验证码二次确认。</p>
+ */
+@Slf4j
+@Service
+public class AuthProfileService {
+
+    private static final Pattern PHONE_PATTERN = Pattern.compile("^1[3-9]\\d{9}$");
+
+    /** 昵称最大字符数(按 Unicode code point 计,避免 emoji 拆半计数) */
+    private static final int MAX_NICKNAME_CODEPOINTS = 20;
+
+    /** 验证码用途:绑定手机 */
+    private static final String VERIFY_TYPE_BIND = "bind";
+
+    @Resource
+    private UserService userService;
+
+    @Resource
+    private VerificationCodeService verificationCodeService;
+
+    /**
+     * 编辑入口:昵称 / 头像 / 绑定手机统一写入登录账号自身。
+     *
+     * @return 非法手机号 / 昵称边界返回 {@code Result.badRequest}(400),不落库
+     */
+    public Result<Boolean> editProfile(Long userId, AuthProfileEditDTO dto) {
+        if (userId == null || userId <= 0) {
+            return Result.error(401, "请先登录");
+        }
+        if (dto == null) {
+            return Result.badRequest("请求参数不能为空");
+        }
+        try {
+            validateNickname(dto.getNickname());
+            validatePhone(dto.getPhone());
+        } catch (IllegalArgumentException e) {
+            return Result.badRequest(e.getMessage());
+        }
+
+        String phone = dto.getPhone() == null ? null : dto.getPhone().trim();
+        if (phone != null && !phone.isEmpty()) {
+            User current = userService.getUserInfo(userId);
+            if (current == null) {
+                return Result.error("用户不存在");
+            }
+            if (!phone.equals(current.getPhone())
+                    && !confirmSensitiveChange(userId, phone, dto.getVerifyCode())) {
+                return Result.badRequest("手机号变更需短信验证码二次确认");
+            }
+        }
+
+        UpdateUserDTO updateDto = new UpdateUserDTO();
+        updateDto.setNickname(dto.getNickname());
+        updateDto.setAvatar(dto.getAvatar());
+        updateDto.setPhone(phone);
+        return userService.updateUserInfo(userId, updateDto)
+                ? Result.success(Boolean.TRUE)
+                : Result.error("更新失败");
+    }
+
+    /**
+     * 手机号格式校验:{@code ^1[3-9]\d{9}$};非法值抛异常(由入口转 400)。
+     *
+     * <p>声明为静态便于 UserService 在既有 {@code /api/user/update} 载体上复用同一校验口径。</p>
+     */
+    public static void validatePhone(String phone) {
+        if (phone == null || phone.trim().isEmpty()) {
+            throw new IllegalArgumentException("手机号不能为空");
+        }
+        if (!PHONE_PATTERN.matcher(phone).matches()) {
+            throw new IllegalArgumentException("手机号格式不正确");
+        }
+    }
+
+    /**
+     * 昵称边界校验:超长(&gt;20 字符)或含 emoji 抛异常;未提交昵称视为不修改。
+     */
+    public static void validateNickname(String nickname) {
+        if (nickname == null || nickname.trim().isEmpty()) {
+            return;
+        }
+        if (nickname.codePointCount(0, nickname.length()) > MAX_NICKNAME_CODEPOINTS) {
+            throw new IllegalArgumentException("昵称不能超过 " + MAX_NICKNAME_CODEPOINTS + " 个字符");
+        }
+        for (int offset = 0; offset < nickname.length(); ) {
+            int cp = nickname.codePointAt(offset);
+            boolean illegal = cp > 0xFFFF
+                    ? isEmojiCodePoint(cp)
+                    : Character.isSurrogate((char) cp) || isEmojiCodePoint(cp);
+            if (illegal) {
+                throw new IllegalArgumentException("昵称含非法字符(不支持 emoji)");
+            }
+            offset += Character.charCount(cp);
+        }
+    }
+
+    /** 敏感字段二次确认:校验绑定手机验证码。 */
+    public boolean confirmSensitiveChange(Long userId, String phone, String verifyCode) {
+        if (userId == null || phone == null || verifyCode == null || verifyCode.trim().isEmpty()) {
+            return false;
+        }
+        try {
+            return verificationCodeService.verifyCode(phone, verifyCode.trim(), VERIFY_TYPE_BIND);
+        } catch (RuntimeException e) {
+            log.warn("绑定手机验证码校验失败: userId={}, phone={}", userId, phone, e);
+            return false;
+        }
+    }
+
+    private static boolean isEmojiCodePoint(int cp) {
+        return (cp >= 0x1F000 && cp <= 0x1FAFF)
+                || (cp >= 0x2600 && cp <= 0x27BF)
+                || cp == 0xFE0F
+                || (cp >= 0x1F1E6 && cp <= 0x1F1FF);
+    }
+}

+ 9 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/UserService.java

@@ -1256,6 +1256,15 @@ FamilyMember child = familyMemberMapper.selectById(memberId);
         if ("admin".equals(user.getRole())) {
         if ("admin".equals(user.getRole())) {
             throw new RuntimeException("管理员角色不允许修改");
             throw new RuntimeException("管理员角色不允许修改");
         }
         }
+        // 资料编辑边界(SC-P-64 / auth.profile.edit):非法手机号 / emoji 超长昵称拦截且不落库
+        if (dto.getNickname() != null) {
+            AuthProfileService.validateNickname(dto.getNickname());
+        }
+        if (dto.getPhone() != null && !dto.getPhone().trim().isEmpty()) {
+            String phone = dto.getPhone().trim();
+            AuthProfileService.validatePhone(phone);
+            user.setPhone(phone);
+        }
         if (dto.getNickname() != null) {
         if (dto.getNickname() != null) {
             user.setNickname(dto.getNickname());
             user.setNickname(dto.getNickname());
         }
         }

File diff suppressed because it is too large
+ 0 - 0
docs/superpowers/PROJECT-OVERVIEW.md


+ 2 - 1
docs/superpowers/api/API_REFERENCE.md

@@ -159,6 +159,7 @@ find cfc-backend/src/main/java -name "*XxxService.java" -o -name "*XxxController
 | `POST /api/auth/set-password` | 设置密码 | password | — |
 | `POST /api/auth/set-password` | 设置密码 | password | — |
 | `POST /api/auth/verify` | 验证密码 | password | — |
 | `POST /api/auth/verify` | 验证密码 | password | — |
 | `POST /api/auth/check-phone` | 检查手机号是否存在 | phone | — |
 | `POST /api/auth/check-phone` | 检查手机号是否存在 | phone | — |
+| `POST /api/auth/profile/edit` | 个人资料编辑(昵称/头像/绑定手机,SC-P-64;非法手机/昵称 → 400,绑定手机变更需 verifyCode 二次确认) | nickname, avatar, phone, verifyCode | — |
 | `POST /api/auth/direct-register` | 直接注册(无邀请码) | — | — |
 | `POST /api/auth/direct-register` | 直接注册(无邀请码) | — | — |
 | `POST /api/auth/register-with-invite` | 邀请码注册 | inviteCode | — |
 | `POST /api/auth/register-with-invite` | 邀请码注册 | inviteCode | — |
 | `POST /api/auth/switch-role` | 切换角色 | role | — |
 | `POST /api/auth/switch-role` | 切换角色 | role | — |
@@ -3569,7 +3570,7 @@ W0.2 双配置中心合并:`product_config` 已并入 `service_config`(唯
 #### 4.63.8 设置与通知(SC-P-64/65/66/78/79)
 #### 4.63.8 设置与通知(SC-P-64/65/66/78/79)
 | 路径 | 说明 |
 | 路径 | 说明 |
 |------|------|
 |------|------|
-| `POST /api/user/update`、`/info`、`/profile/backfill`、`/profile-stats`;`POST /api/family/user/update`;`POST /api/profile/my` | 个人资料编辑(SC-P-64) |
+| `POST /api/user/update`、`/info`、`/profile/backfill`、`/profile-stats`;`POST /api/family/user/update`;`POST /api/profile/my`;`POST /api/auth/profile/edit`(`AuthProfileService` 统一入口,`UpdateUserDTO.phone` 透传绑定手机 + 手机号/昵称边界校验) | 个人资料编辑(SC-P-64) |
 | `POST /api/notification/list`、`/read` | 消息中心/通知查看 + 已读(SC-P-65/78,`UserNotificationController`) |
 | `POST /api/notification/list`、`/read` | 消息中心/通知查看 + 已读(SC-P-65/78,`UserNotificationController`) |
 | `POST /api/task/reminder-config/get`、`/save` | 提醒设置(开关/渠道/时机/偏移/完成表扬 + **日提醒开关+早晚时间**,SC-P-65/79) |
 | `POST /api/task/reminder-config/get`、`/save` | 提醒设置(开关/渠道/时机/偏移/完成表扬 + **日提醒开关+早晚时间**,SC-P-65/79) |
 | `POST /api/consent/record`、`/withdraw`、`/status`、`/check` | 隐私授权同意(SC-P-66,`ConsentController`) |
 | `POST /api/consent/record`、`/withdraw`、`/status`、`/check` | 隐私授权同意(SC-P-66,`ConsentController`) |

Some files were not shown because too many files changed in this diff