| | |
| | | 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; |
| | |
| | | private RoleService roleService; |
| | | @Resource |
| | | private PermissionService permissionService; |
| | | @Autowired(required = false) |
| | | private WxcpMembershipRegistration wxcpMembershipRegistration; |
| | | @Resource |
| | | private WxcpPasswordSetupGrant wxcpPasswordSetupGrant; |
| | | |
| | | /** |
| | | * 验证码的开关,默认为 true |
| | |
| | | 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); |
| | |
| | | |
| | | // 如果 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())); |
| | | } |
| | |
| | | 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 首次登录,或原绑定用户已被删除:自动注册新用户并(改)绑定 |
| | | // |
| | | // 原绑定用户已删除时必须走这条路:否则该企业成员会永久卡在「用户不存在」—— |
| | |
| | | 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( |