云
5 天以前 d0cf2cb2c352bdeb146aad2bb57ba7d39cef3bbd
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/service/auth/AdminAuthServiceImpl.java
@@ -40,6 +40,7 @@
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
@@ -101,6 +102,10 @@
    private RoleService roleService;
    @Resource
    private PermissionService permissionService;
    @Autowired(required = false)
    private WxcpMembershipRegistration wxcpMembershipRegistration;
    @Resource
    private WxcpPasswordSetupGrant wxcpPasswordSetupGrant;
    /**
     * 验证码的开关,默认为 true
@@ -118,6 +123,7 @@
            createLoginLog(null, username, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
            throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
        }
        if (!userService.isPasswordMatch(password, user.getPassword())) {
            createLoginLog(user.getId(), username, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
            throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
@@ -141,6 +147,14 @@
        // 如果 socialType 非空,说明需要绑定社交用户
        if (reqVO.getSocialType() != null) {
            if (WECHAT_ENTERPRISE_TYPE.equals(reqVO.getSocialType())) {
                SocialUserRespDTO social = socialUserService.getSocialUserByCode(getUserType().getValue(),
                        reqVO.getSocialType(), reqVO.getSocialCode(), reqVO.getSocialState());
                if (wxcpMembershipRegistration == null || social == null
                        || wxcpMembershipRegistration.hasPendingApplication(social.getOpenid())) {
                    throw exception(AUTH_WXCP_MEMBERSHIP_PENDING);
                }
            }
            socialUserService.bindSocialUser(new SocialUserBindReqDTO(user.getId(), getUserType().getValue(),
                    reqVO.getSocialType(), reqVO.getSocialCode(), reqVO.getSocialState()));
        }
@@ -228,6 +242,11 @@
        AdminUserDO user = bindUserId != null ? userService.getUser(bindUserId) : null;
        if (user == null) {
            if (WECHAT_ENTERPRISE_TYPE.equals(reqVO.getType())) {
                if (wxcpMembershipRegistration == null) throw new IllegalStateException("会员注册服务不可用");
                wxcpMembershipRegistration.startIfAbsent(socialUser.getOpenid(), socialUser.getNickname(), socialUser.getMobile());
                return null; // 提交事务后由 Controller 返回待审核提示,不签发令牌
            }
            // 2.1 首次登录,或原绑定用户已被删除:自动注册新用户并(改)绑定
            //
            // 原绑定用户已删除时必须走这条路:否则该企业成员会永久卡在「用户不存在」——
@@ -245,8 +264,12 @@
                    user.getId(), user.getUsername(), reqVO.getType());
        }
        if (CommonStatusEnum.isDisable(user.getStatus())) throw exception(AUTH_LOGIN_USER_DISABLED);
        // 3. 创建 Token 令牌,记录登录日志
        AuthLoginRespVO result = createTokenAfterLoginSuccess(user.getId(), user.getUsername(), LoginLogTypeEnum.LOGIN_SOCIAL);
        if (WECHAT_ENTERPRISE_TYPE.equals(reqVO.getType())) {
            wxcpPasswordSetupGrant.issue(result.getAccessToken(), user.getId());
        }
        // 4. 发布社交登录事件(供其他模块监听,如发送企业微信通知)
        eventPublisher.publishEvent(new SocialLoginEvent(