mirror of
				https://gitee.com/hhyykk/ipms-sjy.git
				synced 2025-11-04 12:18:42 +08:00 
			
		
		
		
	增加用户注册功能
This commit is contained in:
		@@ -164,8 +164,7 @@ public class AuthController {
 | 
			
		||||
    @PostMapping("/register")
 | 
			
		||||
    @PermitAll
 | 
			
		||||
    @Operation(summary = "注册用户")
 | 
			
		||||
    @OperateLog(enable = false) // 避免 Post 请求被记录操作日志
 | 
			
		||||
    public CommonResult<Long> register(@RequestBody @Valid AuthRegisterReqVO reqVO) {
 | 
			
		||||
        return success(authService.register(reqVO));
 | 
			
		||||
    public CommonResult<AuthLoginRespVO> register(@RequestBody @Valid AuthRegisterReqVO registerReqVO) {
 | 
			
		||||
        return success(authService.register(registerReqVO));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,22 +1,16 @@
 | 
			
		||||
package cn.iocoder.yudao.module.system.controller.admin.auth.vo;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.core.util.ObjectUtil;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.validation.Mobile;
 | 
			
		||||
import com.fasterxml.jackson.annotation.JsonIgnore;
 | 
			
		||||
 | 
			
		||||
import io.swagger.v3.oas.annotations.media.Schema;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import org.hibernate.validator.constraints.Length;
 | 
			
		||||
 | 
			
		||||
import jakarta.validation.constraints.*;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
 | 
			
		||||
@Schema(description = "管理后台 - Register Request VO")
 | 
			
		||||
@Data
 | 
			
		||||
public class AuthRegisterReqVO {
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "用户编号", example = "1024")
 | 
			
		||||
    private Long id;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "用户账号", requiredMode = Schema.RequiredMode.REQUIRED, example = "yudao")
 | 
			
		||||
    @NotBlank(message = "用户账号不能为空")
 | 
			
		||||
    @Pattern(regexp = "^[a-zA-Z0-9]{4,30}$", message = "用户账号由 数字、字母 组成")
 | 
			
		||||
@@ -27,41 +21,13 @@ public class AuthRegisterReqVO {
 | 
			
		||||
    @Size(max = 30, message = "用户昵称长度不能超过30个字符")
 | 
			
		||||
    private String nickname;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "备注", example = "我是一个用户")
 | 
			
		||||
    private String remark;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "部门ID", example = "我是一个用户")
 | 
			
		||||
    private Long deptId;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "岗位编号数组", example = "1")
 | 
			
		||||
    private Set<Long> postIds;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "用户邮箱", example = "yudao@iocoder.cn")
 | 
			
		||||
    @Email(message = "邮箱格式不正确")
 | 
			
		||||
    @Size(max = 50, message = "邮箱长度不能超过 50 个字符")
 | 
			
		||||
    private String email;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "手机号码", example = "15601691300")
 | 
			
		||||
    @Mobile
 | 
			
		||||
    private String mobile;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "用户性别,参见 SexEnum 枚举类", example = "1")
 | 
			
		||||
    private Integer sex;
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "用户头像", example = "https://www.iocoder.cn/xxx.png")
 | 
			
		||||
    private String avatar;
 | 
			
		||||
 | 
			
		||||
    // ========== 仅【创建】时,需要传递的字段 ==========
 | 
			
		||||
 | 
			
		||||
    @Schema(description = "密码", requiredMode = Schema.RequiredMode.REQUIRED, example = "123456")
 | 
			
		||||
    @Length(min = 4, max = 16, message = "密码长度为 4-16 位")
 | 
			
		||||
    private String password;
 | 
			
		||||
 | 
			
		||||
    @AssertTrue(message = "密码不能为空")
 | 
			
		||||
    @JsonIgnore
 | 
			
		||||
    public boolean isPasswordValid() {
 | 
			
		||||
        return id != null // 修改时,不需要传递
 | 
			
		||||
                || (ObjectUtil.isAllNotEmpty(password)); // 新增时,必须都传递 password
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // ========== 图片验证码相关 ==========
 | 
			
		||||
    @Schema(description = "验证码,验证码开启时,需要传递", requiredMode = Schema.RequiredMode.REQUIRED,
 | 
			
		||||
            example = "PfcH6mgr8tpXuMWFjvW6YVaqrswIuwmWI5dsVZSg7sGpWtDCUbHuDEXl3cFB1+VvCC/rAkSwK8Fad52FSuncVg==")
 | 
			
		||||
    @NotEmpty(message = "验证码不能为空", groups = AuthLoginReqVO.CodeEnableGroup.class)
 | 
			
		||||
    private String captchaVerification;
 | 
			
		||||
}
 | 
			
		||||
@@ -70,13 +70,11 @@ public interface AdminAuthService {
 | 
			
		||||
     */
 | 
			
		||||
    AuthLoginRespVO refreshToken(String refreshToken);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 用户注册
 | 
			
		||||
     *
 | 
			
		||||
     * @param createReqVO 注册用户
 | 
			
		||||
     * @return 注册结果
 | 
			
		||||
     */
 | 
			
		||||
    Long register(AuthRegisterReqVO createReqVO);
 | 
			
		||||
 | 
			
		||||
    AuthLoginRespVO register(AuthRegisterReqVO createReqVO);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,25 +1,20 @@
 | 
			
		||||
package cn.iocoder.yudao.module.system.service.auth;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.core.collection.CollectionUtil;
 | 
			
		||||
import cn.hutool.core.util.ObjectUtil;
 | 
			
		||||
import cn.hutool.core.util.StrUtil;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
 | 
			
		||||
import cn.iocoder.yudao.framework.common.util.validation.ValidationUtils;
 | 
			
		||||
import cn.iocoder.yudao.framework.datapermission.core.util.DataPermissionUtils;
 | 
			
		||||
import cn.iocoder.yudao.module.system.api.logger.dto.LoginLogCreateReqDTO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.api.sms.SmsCodeApi;
 | 
			
		||||
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserRespDTO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.*;
 | 
			
		||||
import cn.iocoder.yudao.module.system.convert.auth.AuthConvert;
 | 
			
		||||
import cn.iocoder.yudao.module.system.dal.dataobject.dept.UserPostDO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
 | 
			
		||||
import cn.iocoder.yudao.module.system.dal.mysql.dept.UserPostMapper;
 | 
			
		||||
import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper;
 | 
			
		||||
import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum;
 | 
			
		||||
import cn.iocoder.yudao.module.system.enums.logger.LoginResultEnum;
 | 
			
		||||
@@ -44,10 +39,8 @@ import org.springframework.stereotype.Service;
 | 
			
		||||
import jakarta.annotation.Resource;
 | 
			
		||||
import jakarta.validation.Validator;
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
 | 
			
		||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
 | 
			
		||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
 | 
			
		||||
import static cn.iocoder.yudao.framework.common.util.servlet.ServletUtils.getClientIP;
 | 
			
		||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
 | 
			
		||||
 | 
			
		||||
@@ -88,9 +81,6 @@ public class AdminAuthServiceImpl implements AdminAuthService {
 | 
			
		||||
     */
 | 
			
		||||
    @Value("${yudao.captcha.enable:true}")
 | 
			
		||||
    private Boolean captchaEnable;
 | 
			
		||||
    @Resource
 | 
			
		||||
    private UserPostMapper userPostMapper;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public AdminUserDO authenticate(String username, String password) {
 | 
			
		||||
@@ -269,7 +259,10 @@ public class AdminAuthServiceImpl implements AdminAuthService {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public Long register(AuthRegisterReqVO registerReqVO) {
 | 
			
		||||
    public AuthLoginRespVO register(AuthRegisterReqVO registerReqVO) {
 | 
			
		||||
        // 校验验证码
 | 
			
		||||
        AuthLoginReqVO loginReqVO = BeanUtils.toBean(registerReqVO, AuthLoginReqVO.class);
 | 
			
		||||
        validateCaptcha(loginReqVO);
 | 
			
		||||
        // 校验账户配合
 | 
			
		||||
        tenantService.handleTenantInfo(tenant -> {
 | 
			
		||||
            long count = userMapper.selectCount();
 | 
			
		||||
@@ -277,60 +270,17 @@ public class AdminAuthServiceImpl implements AdminAuthService {
 | 
			
		||||
                throw exception(USER_COUNT_MAX, tenant.getAccountCount());
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
        // 校验正确性
 | 
			
		||||
        validateUserForRegister(null, registerReqVO.getUsername(),
 | 
			
		||||
                registerReqVO.getMobile(), registerReqVO.getEmail(), registerReqVO.getDeptId(), registerReqVO.getPostIds());
 | 
			
		||||
        // 校验用户名是否已存在
 | 
			
		||||
        if (userMapper.selectByUsername(registerReqVO.getUsername()) != null) {
 | 
			
		||||
            throw exception(USER_USERNAME_EXISTS);
 | 
			
		||||
        }
 | 
			
		||||
        // 插入用户
 | 
			
		||||
        AdminUserDO user = BeanUtils.toBean(registerReqVO, AdminUserDO.class);
 | 
			
		||||
        user.setStatus(CommonStatusEnum.ENABLE.getStatus()); // 默认开启
 | 
			
		||||
        user.setPassword(encodePassword(registerReqVO.getPassword())); // 加密密码
 | 
			
		||||
        userMapper.insert(user);
 | 
			
		||||
        // 插入关联岗位
 | 
			
		||||
        if (CollectionUtil.isNotEmpty(user.getPostIds())) {
 | 
			
		||||
            userPostMapper.insertBatch(convertList(user.getPostIds(),
 | 
			
		||||
                    postId -> new UserPostDO().setUserId(user.getId()).setPostId(postId)));
 | 
			
		||||
        }
 | 
			
		||||
        return user.getId();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void validateUserForRegister(Long id, String username, String mobile, String email,
 | 
			
		||||
                                               Long deptId, Set<Long> postIds) {
 | 
			
		||||
        // 关闭数据权限,避免因为没有数据权限,查询不到数据,进而导致唯一校验不正确
 | 
			
		||||
        DataPermissionUtils.executeIgnore(() -> {
 | 
			
		||||
            // 校验用户存在
 | 
			
		||||
            validateUserExists(id);
 | 
			
		||||
            // 校验用户名唯一
 | 
			
		||||
            validateUsernameUnique(id, username);
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @VisibleForTesting
 | 
			
		||||
    void validateUserExists(Long id) {
 | 
			
		||||
        if (id == null) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        AdminUserDO user = userMapper.selectById(id);
 | 
			
		||||
        if (user == null) {
 | 
			
		||||
            throw exception(USER_NOT_EXISTS);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @VisibleForTesting
 | 
			
		||||
    void validateUsernameUnique(Long id, String username) {
 | 
			
		||||
        if (StrUtil.isBlank(username)) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        AdminUserDO user = userMapper.selectByUsername(username);
 | 
			
		||||
        if (user == null) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        // 如果 id 为空,说明不用比较是否为相同 id 的用户
 | 
			
		||||
        if (id == null) {
 | 
			
		||||
            throw exception(USER_USERNAME_EXISTS);
 | 
			
		||||
        }
 | 
			
		||||
        if (!user.getId().equals(id)) {
 | 
			
		||||
            throw exception(USER_USERNAME_EXISTS);
 | 
			
		||||
        }
 | 
			
		||||
        return createTokenAfterLoginSuccess(user.getId(), registerReqVO.getUsername(), LoginLogTypeEnum.LOGIN_USERNAME);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user