Эх сурвалжийг харах

fix(cfc-backend): prevent auto-create user when openid not found and no phoneCode

User 2 сар өмнө
parent
commit
7acebeec74

+ 7 - 3
cfc-backend/src/main/java/com/etotem/cfc/service/UserService.java

@@ -86,7 +86,11 @@ private FamilyInvitationService familyInvitationService;
             }
             }
 
 
             if (user == null) {
             if (user == null) {
-                // 5. 全新用户,创建新家庭和用户
+                // 5. 没有phoneCode也无法通过手机号找到用户 → 不能自动创建(避免mock→真实openid迁移时产生重复用户)
+                if (phone == null) {
+                    throw new RuntimeException("用户不存在,请先授权手机号后登录");
+                }
+                // 6. 有phoneCode且查不到 → 全新用户,创建新家庭和用户
                 isNewUser = true;
                 isNewUser = true;
 
 
                 Family family = new Family();
                 Family family = new Family();
@@ -101,7 +105,7 @@ private FamilyInvitationService familyInvitationService;
                 user.setUnionid(unionid != null ? unionid : "");
                 user.setUnionid(unionid != null ? unionid : "");
                 user.setFamilyId(family.getId());
                 user.setFamilyId(family.getId());
                 user.setRole("parent");
                 user.setRole("parent");
-                user.setNickname(dto.getNickname() != null ? dto.getNickname() : (phone != null ? "用户" + phone.substring(7) : "微信用户"));
+                user.setNickname(dto.getNickname() != null ? dto.getNickname() : "用户" + phone.substring(7));
                 user.setAvatar(dto.getAvatar());
                 user.setAvatar(dto.getAvatar());
                 user.setPhone(phone);
                 user.setPhone(phone);
                 user.setPassword("");
                 user.setPassword("");
@@ -110,7 +114,7 @@ private FamilyInvitationService familyInvitationService;
                 userMapper.insert(user);
                 userMapper.insert(user);
                 try { onboardingService.initForUser(user.getId()); onboardingService.completeTask(user.getId(), "REGISTER"); } catch (Exception e) { /* onboarding init failure should not block login */ }
                 try { onboardingService.initForUser(user.getId()); onboardingService.completeTask(user.getId(), "REGISTER"); } catch (Exception e) { /* onboarding init failure should not block login */ }
             } else {
             } else {
-                // 6. 手机号已存在但openid不同,绑定openid
+                // 7. 手机号已存在但openid不同,绑定openid
                 user.setOpenid(openid);
                 user.setOpenid(openid);
                 user.setUnionid(unionid != null ? unionid : "");
                 user.setUnionid(unionid != null ? unionid : "");
                 user.setUpdatedAt(new Date());
                 user.setUpdatedAt(new Date());