|
@@ -4,15 +4,20 @@ import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.abi.base.foundation.util.RedisClient;
|
|
|
import com.abi.qms.platform.dao.entity.UserInfo;
|
|
|
import com.abi.qms.platform.dao.entity.UserRole;
|
|
|
+import com.abi.qms.platform.dao.entity.UserRoleDetail;
|
|
|
+import com.abi.qms.platform.dao.enums.FactoryTypeEnum;
|
|
|
+import com.abi.qms.platform.dao.enums.TrueFalseEnum;
|
|
|
import com.abi.qms.platform.dao.mapper.UserInfoMapper;
|
|
|
import com.abi.qms.platform.dao.mapper.UserRoleMapper;
|
|
|
import com.abi.qms.platform.dto.req.LoginReq;
|
|
|
import com.abi.qms.platform.dto.req.ResetPasswordReq;
|
|
|
import com.abi.qms.platform.dto.req.TokenReq;
|
|
|
import com.abi.qms.platform.dto.req.UpdatePasswordReq;
|
|
|
+import com.abi.qms.platform.dto.res.LoginActivateRes;
|
|
|
import com.abi.qms.platform.dto.res.LoginRes;
|
|
|
import com.abi.qms.platform.infrastructure.util.UserUtil;
|
|
|
import com.abi.qms.platform.service.LoginService;
|
|
|
+import com.abi.qms.platform.service.RoleManagerService;
|
|
|
import com.abi.task.common.api.exception.BusinessException;
|
|
|
import com.abi.task.common.utils.AESEncodeTwoUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
@@ -54,36 +59,128 @@ public class LoginServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> impl
|
|
|
@Autowired
|
|
|
private UserUtil userUtil;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private RoleManagerService roleManagerService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 登录console
|
|
|
+ * @param req
|
|
|
+ * @return
|
|
|
+ */
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public LoginRes login(LoginReq req) {
|
|
|
- UserInfo one = lambdaQuery().eq(UserInfo::getUserName, req.getUserName().trim()).eq(UserInfo::getPassword, req.getPassword()).eq(UserInfo::getIsEnable, 1).eq(UserInfo::getIsDelete, 0).select(UserInfo::getId).one();
|
|
|
- if (Objects.nonNull(one)) {
|
|
|
- UserInfo userInfo = userInfoMapper.selectById(one.getId());
|
|
|
- QueryWrapper<UserRole> roleQueryWrapper = new QueryWrapper<>();
|
|
|
- roleQueryWrapper.eq("role_code", userInfo.getRoleCode());
|
|
|
- roleQueryWrapper.eq("is_delete", 0);
|
|
|
- UserRole userRole = userRoleMapper.selectOne(roleQueryWrapper);
|
|
|
- if (Objects.isNull(userRole)) {
|
|
|
- throw new BusinessException("该用户的角色不存在");
|
|
|
- }
|
|
|
- userUtil.setUser(userInfo);
|
|
|
- String token = getToken(userInfo);
|
|
|
- String redisTokenKey = MessageFormat.format("{0}:{1}",TOKEN_KEY,token);
|
|
|
- redisClient.set(redisTokenKey,true,2, TimeUnit.HOURS);
|
|
|
- redisClient.set(TOKEN_KEY_USER_ID+userInfo.getId(),true,2,TimeUnit.DAYS);
|
|
|
- LoginRes build = LoginRes.builder()
|
|
|
- .token(MessageFormat.format("Bearer {0}",token))
|
|
|
- .id(one.getId())
|
|
|
- .roleCode(userInfo.getRoleCode())
|
|
|
- .factoryId(userInfo.getFactoryId())
|
|
|
- .build();
|
|
|
- return build;
|
|
|
- } else {
|
|
|
+ public LoginRes loginConsole(LoginReq req) {
|
|
|
+ //1-查询用户并校验
|
|
|
+ UserInfo userInfo = getAndCheckUserByNameAndPasswd(req.getUserName(),req.getPassword());
|
|
|
+ //是否啤酒厂用户
|
|
|
+ if(FactoryTypeEnum.BEER.is(userInfo.getFactoryType())){
|
|
|
+ throw new BusinessException("啤酒厂用户不能登录console");
|
|
|
+ }
|
|
|
+
|
|
|
+ //是否异常数据没有角色
|
|
|
+ QueryWrapper<UserRole> roleQueryWrapper = new QueryWrapper<>();
|
|
|
+ roleQueryWrapper.eq("role_code", userInfo.getRoleCode());
|
|
|
+ roleQueryWrapper.eq("is_delete", 0);
|
|
|
+ UserRole userRole = userRoleMapper.selectOne(roleQueryWrapper);
|
|
|
+ if (Objects.isNull(userRole)) {
|
|
|
+ throw new BusinessException("该用户的角色不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ //2-存token
|
|
|
+ String token = createToken(userInfo);
|
|
|
+
|
|
|
+ //返回传参
|
|
|
+ LoginRes build = LoginRes.builder()
|
|
|
+ .token(MessageFormat.format("Bearer {0}",token))
|
|
|
+ .id(userInfo.getId())
|
|
|
+ .roleCode(userInfo.getRoleCode())
|
|
|
+ .factoryId(userInfo.getFactoryId())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ return build;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public LoginActivateRes loginActivate(LoginReq req) {
|
|
|
+ //1-查询用户并校验
|
|
|
+ UserInfo userInfo = getAndCheckUserByNameAndPasswd(req.getUserName(),req.getPassword());
|
|
|
+ //是否啤酒厂用户
|
|
|
+ if(!FactoryTypeEnum.BEER.is(userInfo.getFactoryType())){
|
|
|
+ throw new BusinessException("非啤酒厂用户不能登录激活小程序");
|
|
|
+ }
|
|
|
+
|
|
|
+ //是否异常数据没有角色
|
|
|
+ QueryWrapper<UserRole> roleQueryWrapper = new QueryWrapper<>();
|
|
|
+ roleQueryWrapper.eq("role_code", userInfo.getRoleCode());
|
|
|
+ roleQueryWrapper.eq("is_delete", 0);
|
|
|
+ UserRole userRole = userRoleMapper.selectOne(roleQueryWrapper);
|
|
|
+ if (Objects.isNull(userRole)) {
|
|
|
+ throw new BusinessException("该用户的角色不存在");
|
|
|
+ }
|
|
|
+ //查询是否主子账号
|
|
|
+ UserRoleDetail roleDetail = roleManagerService.getRoleDetail(userRole.getRoleCode());
|
|
|
+ if(roleDetail==null
|
|
|
+ ||(!TrueFalseEnum.TRUE.is(roleDetail.getIsActivateMiniAppSubAccount())
|
|
|
+ &&!TrueFalseEnum.TRUE.is(roleDetail.getIsActivateMiniAppMainAccount()))){
|
|
|
+ throw new BusinessException("该用户的角色不包含登录小程序权限");
|
|
|
+ }
|
|
|
+
|
|
|
+ //是否主账号
|
|
|
+ int isMainAccount = (TrueFalseEnum.TRUE.is(roleDetail.getIsActivateMiniAppMainAccount()))?1:0;
|
|
|
+
|
|
|
+ //2-存token
|
|
|
+ String token = createToken(userInfo);
|
|
|
+
|
|
|
+ //返回传参
|
|
|
+ LoginActivateRes build = LoginActivateRes.builder()
|
|
|
+ .token(MessageFormat.format("Bearer {0}",token))
|
|
|
+ .id(userInfo.getId())
|
|
|
+ .isMainAccount(isMainAccount)
|
|
|
+ .factoryId(userInfo.getFactoryId())
|
|
|
+ .build();
|
|
|
+
|
|
|
+ return build;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询用户并校验
|
|
|
+ * @param userName
|
|
|
+ * @param passWd
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private UserInfo getAndCheckUserByNameAndPasswd(String userName,String passWd){
|
|
|
+ UserInfo userInfo = lambdaQuery()
|
|
|
+ .eq(UserInfo::getUserName, userName.trim())
|
|
|
+ .eq(UserInfo::getIsDelete, 0)
|
|
|
+ .select(UserInfo::getId).one();
|
|
|
+
|
|
|
+ //密码校验
|
|
|
+ if(userInfo==null || !passWd.equals(userInfo.getPassword())){
|
|
|
throw new BusinessException("登录账号或密码错误");
|
|
|
}
|
|
|
+ //是否启用
|
|
|
+ if(userInfo.getIsEnable()!=1){
|
|
|
+ throw new BusinessException("账号已禁用,请联系管理员");
|
|
|
+ }
|
|
|
+
|
|
|
+ return userInfo;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 存token
|
|
|
+ * @param userInfo
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String createToken(UserInfo userInfo){
|
|
|
+ String token = getToken(userInfo);
|
|
|
+ String redisTokenKey = MessageFormat.format("{0}:{1}",TOKEN_KEY,token);
|
|
|
+ redisClient.set(redisTokenKey,true,2, TimeUnit.HOURS);
|
|
|
+ redisClient.set(TOKEN_KEY_USER_ID+userInfo.getId(),true,2,TimeUnit.DAYS);
|
|
|
+
|
|
|
+ return token;
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public void resetPassword(ResetPasswordReq req) {
|