|
@@ -1,20 +1,33 @@
|
|
|
package com.citu.module.menduner.system.service.auth;
|
|
|
|
|
|
-import com.citu.module.menduner.system.controller.app.auth.vo.AppMdeAuthLoginReqVO;
|
|
|
+import cn.hutool.core.lang.Assert;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.baomidou.dynamic.datasource.annotation.DSTransactional;
|
|
|
+import com.citu.framework.common.enums.UserTypeEnum;
|
|
|
import com.citu.module.menduner.system.controller.app.auth.vo.AppMdeAuthLoginRespVO;
|
|
|
-import com.citu.module.menduner.system.controller.app.auth.vo.AppMdeAuthSmsLoginReqVO;
|
|
|
import com.citu.module.menduner.system.controller.app.auth.vo.enterprise.AppEnterpriseAuthLoginReqVO;
|
|
|
import com.citu.module.menduner.system.controller.app.auth.vo.enterprise.AppEnterpriseAuthSmsLoginReqVO;
|
|
|
+import com.citu.module.menduner.system.convert.MdeAuthConvert;
|
|
|
+import com.citu.module.menduner.system.dal.dataobject.enterprise.EnterpriseUserBindDO;
|
|
|
+import com.citu.module.menduner.system.dal.dataobject.user.MdeUserDO;
|
|
|
+import com.citu.module.menduner.system.enums.MendunerStatusEnum;
|
|
|
import com.citu.module.menduner.system.service.enterprise.bind.EnterpriseUserBindService;
|
|
|
-import com.citu.module.menduner.system.service.user.MdeUserService;
|
|
|
-import com.citu.module.system.api.logger.LoginLogApi;
|
|
|
-import com.citu.module.system.api.oauth2.OAuth2TokenApi;
|
|
|
-import com.citu.module.system.api.sms.SmsCodeApi;
|
|
|
+import com.citu.module.system.api.oauth2.dto.OAuth2AccessTokenCreateReqDTO;
|
|
|
+import com.citu.module.system.api.oauth2.dto.OAuth2AccessTokenRespDTO;
|
|
|
+import com.citu.module.system.api.social.dto.SocialUserBindReqDTO;
|
|
|
+import com.citu.module.system.enums.logger.LoginLogTypeEnum;
|
|
|
+import com.citu.module.system.enums.logger.LoginResultEnum;
|
|
|
+import com.citu.module.system.enums.oauth2.OAuth2ClientConstants;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
|
|
+import static com.citu.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
+import static com.citu.framework.common.util.servlet.ServletUtils.getClientIP;
|
|
|
+import static com.citu.framework.web.core.util.WebFrameworkUtils.getTerminal;
|
|
|
+import static com.citu.module.menduner.system.enums.ErrorCodeConstants.*;
|
|
|
+
|
|
|
/**
|
|
|
* menduner 的企业认证 Service 接口 实现
|
|
|
*
|
|
@@ -22,27 +35,96 @@ import javax.annotation.Resource;
|
|
|
**/
|
|
|
@Service
|
|
|
@Validated
|
|
|
-public class MdeEnterpriseAuthServiceImpl implements MdeEnterpriseAuthService{
|
|
|
+public class MdeEnterpriseAuthServiceImpl extends MdeAuthServiceImpl implements MdeEnterpriseAuthService {
|
|
|
+
|
|
|
|
|
|
- @Resource
|
|
|
- private MdeUserService userService;
|
|
|
- @Resource
|
|
|
- private SmsCodeApi smsCodeApi;
|
|
|
- @Resource
|
|
|
- private LoginLogApi loginLogApi;
|
|
|
- @Resource
|
|
|
- private OAuth2TokenApi oauth2TokenApi;
|
|
|
@Resource
|
|
|
private EnterpriseUserBindService bindService;
|
|
|
|
|
|
|
|
|
@Override
|
|
|
- public AppMdeAuthLoginRespVO login(AppEnterpriseAuthLoginReqVO reqVO) {
|
|
|
- return null;
|
|
|
+ @DSTransactional
|
|
|
+ public AppMdeAuthLoginRespVO loginEnterprise(AppEnterpriseAuthLoginReqVO reqVO) {
|
|
|
+ // 校验验证码
|
|
|
+ validateCaptcha(reqVO);
|
|
|
+ // 使用手机 + 密码,进行登录。
|
|
|
+ MdeUserDO user = check(reqVO.getPhone(), reqVO.getPassword());
|
|
|
+
|
|
|
+ // 如果 socialType 非空,说明需要绑定社交用户
|
|
|
+ String openid = null;
|
|
|
+ if (reqVO.getSocialType() != null) {
|
|
|
+ openid = socialUserApi.bindSocialUser(new SocialUserBindReqDTO(user.getId(), getUserType().getValue(),
|
|
|
+ reqVO.getSocialType(), reqVO.getSocialCode(), reqVO.getSocialState())).getCheckedData();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建 Token 令牌,记录登录日志
|
|
|
+ return createTokenAfterLoginSuccess(user, reqVO.getPhone(), LoginLogTypeEnum.LOGIN_MOBILE, openid);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @DSTransactional
|
|
|
+ public AppMdeAuthLoginRespVO smsLoginEnterprise(AppEnterpriseAuthSmsLoginReqVO reqVO) {
|
|
|
+ // 校验验证码
|
|
|
+ String userIp = getClientIP();
|
|
|
+// smsCodeApi.useSmsCode(MdeAuthConvert.INSTANCE.convert(reqVO, SmsSceneEnum.MENDUNER_LOGIN.getScene(), userIp).setMobile(reqVO.getPhone())).getCheckedData();
|
|
|
+
|
|
|
+ // 获得获得注册用户
|
|
|
+ MdeUserDO user = userService.createUserIfAbsent(reqVO.getPhone(), userIp, getTerminal().toString());
|
|
|
+ Assert.notNull(user, "获取用户失败,结果为空");
|
|
|
+
|
|
|
+ // 如果 socialType 非空,说明需要绑定社交用户
|
|
|
+ String openid = null;
|
|
|
+ if (reqVO.getSocialType() != null) {
|
|
|
+ openid = socialUserApi.bindSocialUser(new SocialUserBindReqDTO(user.getId(), getUserType().getValue(),
|
|
|
+ reqVO.getSocialType(), reqVO.getSocialCode(), reqVO.getSocialState())).getCheckedData();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建 Token 令牌,记录登录日志
|
|
|
+ return createTokenAfterLoginSuccess(user, reqVO.getPhone(), LoginLogTypeEnum.LOGIN_SMS, openid);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected MdeUserDO check(String mobile, String password) {
|
|
|
+ final LoginLogTypeEnum logTypeEnum = LoginLogTypeEnum.LOGIN_MOBILE;
|
|
|
+ // 校验账号是否存在
|
|
|
+ MdeUserDO user = userService.getUserByPhone(mobile);
|
|
|
+ if (user == null) {
|
|
|
+ createLoginLog(null, mobile, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
|
|
|
+ throw exception(MDE_AUTH_LOGIN_BAD_CREDENTIALS);
|
|
|
+ }
|
|
|
+ if (!userService.isPasswordMatch(password, user.getPassword())) {
|
|
|
+ createLoginLog(user.getId(), mobile, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
|
|
|
+ throw exception(MDE_AUTH_LOGIN_BAD_CREDENTIALS);
|
|
|
+ }
|
|
|
+ // 查询绑定用户
|
|
|
+ EnterpriseUserBindDO bind = bindService.getEnterpriseUserBind(user.getId());
|
|
|
+ if (null == bind) {
|
|
|
+ createLoginLog(user.getId(), mobile, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
|
|
|
+ throw exception(MDE_USER_ENTERPRISE_NOT_USER_NULL);
|
|
|
+ }
|
|
|
+ // 校验是否禁用
|
|
|
+ if (ObjectUtil.notEqual(bind.getStatus(), MendunerStatusEnum.ENABLE.getStatus())) {
|
|
|
+ createLoginLog(user.getId(), mobile, logTypeEnum, LoginResultEnum.USER_DISABLED);
|
|
|
+ throw exception(MDE_AUTH_LOGIN_USER_DISABLED);
|
|
|
+ }
|
|
|
+ return user;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ protected UserTypeEnum getUserType() {
|
|
|
+ return UserTypeEnum.ADMIN;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public AppMdeAuthLoginRespVO smsLogin(AppEnterpriseAuthSmsLoginReqVO reqVO) {
|
|
|
- return null;
|
|
|
+ protected AppMdeAuthLoginRespVO createTokenAfterLoginSuccess(MdeUserDO user, String phone, LoginLogTypeEnum logType, String openid) {
|
|
|
+ // 插入登陆日志
|
|
|
+ createLoginLog(user.getId(), phone, logType, LoginResultEnum.SUCCESS);
|
|
|
+ // 创建 Token 令牌
|
|
|
+ OAuth2AccessTokenRespDTO accessTokenRespDTO = oauth2TokenApi.createAccessToken(new OAuth2AccessTokenCreateReqDTO()
|
|
|
+ .setUserId(user.getId()).setUserType(getUserType().getValue())
|
|
|
+ .setClientId(OAuth2ClientConstants.CLIENT_ID_MENDUNER)).getCheckedData();
|
|
|
+ // 构建返回结果
|
|
|
+ return MdeAuthConvert.INSTANCE.convert(accessTokenRespDTO, openid);
|
|
|
}
|
|
|
}
|