|
@@ -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("手机号格式不正确");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 昵称边界校验:超长(>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);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|