| | |
| | | import com.iailab.framework.common.util.servlet.ServletUtils; |
| | | import com.iailab.framework.common.util.validation.ValidationUtils; |
| | | import com.iailab.module.system.api.logger.dto.LoginLogCreateReqDTO; |
| | | import com.iailab.module.system.api.sms.SmsCodeApi; |
| | | import com.iailab.module.system.api.social.dto.SocialUserBindReqDTO; |
| | | import com.iailab.module.system.api.social.dto.SocialUserRespDTO; |
| | | import com.iailab.module.system.controller.admin.auth.vo.*; |
| | | import com.iailab.module.system.convert.auth.AuthConvert; |
| | | import com.iailab.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO; |
| | |
| | | import com.iailab.module.system.service.logger.LoginLogService; |
| | | import com.iailab.module.system.service.member.MemberService; |
| | | import com.iailab.module.system.service.oauth2.OAuth2TokenService; |
| | | import com.iailab.module.system.service.social.SocialUserService; |
| | | import com.iailab.module.system.service.user.AdminUserService; |
| | | import com.google.common.annotations.VisibleForTesting; |
| | | import com.xingyuv.captcha.model.common.ResponseModel; |
| | |
| | | import java.util.Objects; |
| | | |
| | | import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static com.iailab.framework.common.util.servlet.ServletUtils.getClientIP; |
| | | import static com.iailab.module.system.enums.ErrorCodeConstants.*; |
| | | |
| | | /** |
| | |
| | | @Resource |
| | | private OAuth2TokenService oauth2TokenService; |
| | | @Resource |
| | | private SocialUserService socialUserService; |
| | | @Resource |
| | | private MemberService memberService; |
| | | @Resource |
| | | private Validator validator; |
| | | @Resource |
| | | private CaptchaService captchaService; |
| | | @Resource |
| | | private SmsCodeApi smsCodeApi; |
| | | |
| | | /** |
| | | * 验证码的开关,默认为 true |
| | |
| | | // 使用账号密码,进行登录 |
| | | AdminUserDO user = authenticate(reqVO.getUsername(), reqVO.getPassword()); |
| | | |
| | | // 如果 socialType 非空,说明需要绑定社交用户 |
| | | if (reqVO.getSocialType() != null) { |
| | | socialUserService.bindSocialUser(new SocialUserBindReqDTO(user.getId(), getUserType().getValue(), |
| | | reqVO.getSocialType(), reqVO.getSocialCode(), reqVO.getSocialState())); |
| | | } |
| | | // 创建 Token 令牌,记录登录日志 |
| | | return createTokenAfterLoginSuccess(user.getId(), reqVO.getUsername(), LoginLogTypeEnum.LOGIN_USERNAME); |
| | | } |
| | | |
| | | @Override |
| | | public void sendSmsCode(AuthSmsSendReqVO reqVO) { |
| | | // 登录场景,验证是否存在 |
| | | if (userService.getUserByMobile(reqVO.getMobile()) == null) { |
| | | throw exception(AUTH_MOBILE_NOT_EXISTS); |
| | | } |
| | | // 发送验证码 |
| | | smsCodeApi.sendSmsCode(AuthConvert.INSTANCE.convert(reqVO).setCreateIp(getClientIP())); |
| | | } |
| | | |
| | | @Override |
| | | public AuthLoginRespVO smsLogin(AuthSmsLoginReqVO reqVO) { |
| | | // 校验验证码 |
| | | smsCodeApi.useSmsCode(AuthConvert.INSTANCE.convert(reqVO, SmsSceneEnum.ADMIN_MEMBER_LOGIN.getScene(), getClientIP())).getCheckedData(); |
| | | |
| | | // 获得用户信息 |
| | | AdminUserDO user = userService.getUserByMobile(reqVO.getMobile()); |
| | | if (user == null) { |
| | | throw exception(USER_NOT_EXISTS); |
| | | } |
| | | |
| | | // 创建 Token 令牌,记录登录日志 |
| | | return createTokenAfterLoginSuccess(user.getId(), reqVO.getMobile(), LoginLogTypeEnum.LOGIN_MOBILE); |
| | | } |
| | | |
| | | private void createLoginLog(Long userId, String username, |
| | |
| | | if (userId != null && Objects.equals(LoginResultEnum.SUCCESS.getResult(), loginResult.getResult())) { |
| | | userService.updateUserLogin(userId, ServletUtils.getClientIP()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public AuthLoginRespVO socialLogin(AuthSocialLoginReqVO reqVO) { |
| | | // 使用 code 授权码,进行登录。然后,获得到绑定的用户编号 |
| | | SocialUserRespDTO socialUser = socialUserService.getSocialUserByCode(UserTypeEnum.ADMIN.getValue(), reqVO.getType(), |
| | | reqVO.getCode(), reqVO.getState()); |
| | | if (socialUser == null || socialUser.getUserId() == null) { |
| | | throw exception(AUTH_THIRD_LOGIN_NOT_BIND); |
| | | } |
| | | |
| | | // 获得用户 |
| | | AdminUserDO user = userService.getUser(socialUser.getUserId()); |
| | | if (user == null) { |
| | | throw exception(USER_NOT_EXISTS); |
| | | } |
| | | |
| | | // 创建 Token 令牌,记录登录日志 |
| | | return createTokenAfterLoginSuccess(user.getId(), user.getUsername(), LoginLogTypeEnum.LOGIN_SOCIAL); |
| | | } |
| | | |
| | | @VisibleForTesting |