|
|
@@ -269,22 +269,10 @@ public class UserService {
|
|
|
if (StrUtil.isBlank(invitationCode))
|
|
|
throw new BusinessException(400, "邀请码不能为空");
|
|
|
|
|
|
- // 先校验邀请码有效性,同时获取邀请码实体(用于推导租户)
|
|
|
- InvitationCodeEntity codeEntity = invitationCodeService.validate(invitationCode);
|
|
|
-
|
|
|
- // 通过邀请码创建者推导租户 ID
|
|
|
- Long tenantId = null;
|
|
|
- if (codeEntity.getCreatedBy() != null) {
|
|
|
- UserEntity creator = userMapper.selectById(codeEntity.getCreatedBy());
|
|
|
- if (creator != null && creator.getTenantId() != null) {
|
|
|
- tenantId = creator.getTenantId();
|
|
|
- }
|
|
|
- }
|
|
|
- if (tenantId == null) {
|
|
|
- throw new BusinessException(500, "邀请码未关联租户,无法注册");
|
|
|
- }
|
|
|
+ // 校验邀请码有效性
|
|
|
+ invitationCodeService.validate(invitationCode);
|
|
|
|
|
|
- // 检查用户名是否已存在(按 username 或 mobile)
|
|
|
+ // 检查用户名是否已存在
|
|
|
UserEntity existByUsername = userMapper.selectOne(
|
|
|
new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getUsername, username));
|
|
|
if (existByUsername != null)
|
|
|
@@ -297,6 +285,18 @@ public class UserService {
|
|
|
throw new BusinessException(400, "手机号已存在");
|
|
|
}
|
|
|
|
|
|
+ // 创建租户 — 新用户即租户(对应 Python register_user_service)
|
|
|
+ String tenantCode = StrUtil.isNotBlank(mobile) ? mobile : username;
|
|
|
+ if (tenantMapper.exists(new LambdaQueryWrapper<TenantEntity>().eq(TenantEntity::getCode, tenantCode)))
|
|
|
+ throw new BusinessException(400, "注册失败,手机号已被使用");
|
|
|
+
|
|
|
+ TenantEntity tenant = new TenantEntity();
|
|
|
+ tenant.setName(tenantCode);
|
|
|
+ tenant.setCode(tenantCode);
|
|
|
+ tenant.setStatus("0");
|
|
|
+ tenantMapper.insert(tenant);
|
|
|
+
|
|
|
+ // 创建用户(作为该租户的管理员)
|
|
|
UserEntity entity = new UserEntity();
|
|
|
entity.setUuid(UUID.randomUUID().toString());
|
|
|
entity.setUsername(username);
|
|
|
@@ -305,10 +305,10 @@ public class UserService {
|
|
|
entity.setName(StrUtil.isNotBlank(name) ? name : username);
|
|
|
entity.setStatus("0");
|
|
|
entity.setIsSuperuser(false);
|
|
|
- entity.setTenantId(tenantId);
|
|
|
+ entity.setTenantId(tenant.getId());
|
|
|
userMapper.insert(entity);
|
|
|
|
|
|
- // 分配默认角色 (role_id=1)
|
|
|
+ // 分配租户管理员角色 (role_id=2)
|
|
|
userMapper.insertUserRole(entity.getId(), 2L);
|
|
|
|
|
|
// 原子认领邀请码
|