78 lines
3.4 KiB
Java
78 lines
3.4 KiB
Java
|
package com.changhu.enums.handler;
|
||
|
|
||
|
import cn.dev33.satoken.stp.SaTokenInfo;
|
||
|
import cn.hutool.extra.spring.SpringUtil;
|
||
|
import com.alibaba.fastjson2.JSONObject;
|
||
|
import com.changhu.common.db.enums.IsEnable;
|
||
|
import com.changhu.common.enums.ResultCode;
|
||
|
import com.changhu.common.exception.MessageException;
|
||
|
import com.changhu.common.pojo.vo.TokenInfo;
|
||
|
import com.changhu.common.utils.RsaUtil;
|
||
|
import com.changhu.common.utils.UserUtil;
|
||
|
import com.changhu.common.utils.ValidatorUtil;
|
||
|
import com.changhu.module.management.pojo.entity.ManagementSecurityUnitUser;
|
||
|
import com.changhu.module.management.pojo.entity.PoliceUnit;
|
||
|
import com.changhu.module.management.pojo.entity.SecurityUnit;
|
||
|
import com.changhu.module.management.service.ManagementSecurityUnitUserService;
|
||
|
import com.changhu.module.management.service.SecurityUnitService;
|
||
|
import com.changhu.pojo.params.ManagementSecurityUnitLoginParams;
|
||
|
import com.changhu.support.mybatisplus.pojo.entity.BaseEntity;
|
||
|
|
||
|
/**
|
||
|
* @author 20252
|
||
|
* @createTime 2024/9/2 下午5:48
|
||
|
* @desc ManagementSecurityUnitLogin...
|
||
|
*/
|
||
|
public class ManagementSecurityUnitLogin extends AbstractLoginHandler {
|
||
|
|
||
|
private final ManagementSecurityUnitUserService securityUnitUserService = SpringUtil.getBean(ManagementSecurityUnitUserService.class);
|
||
|
private final SecurityUnitService securityUnitService = SpringUtil.getBean(SecurityUnitService.class);
|
||
|
|
||
|
public static final ManagementSecurityUnitLogin instance = new ManagementSecurityUnitLogin();
|
||
|
|
||
|
private ManagementSecurityUnitLogin() {
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public TokenInfo login(JSONObject jsonObject) {
|
||
|
ManagementSecurityUnitLoginParams loginParams = jsonObject.to(ManagementSecurityUnitLoginParams.class);
|
||
|
ValidatorUtil.manual(loginParams);
|
||
|
|
||
|
String accountOrTelephone = loginParams.getAccountOrTelephone();
|
||
|
String password = RsaUtil.decrypt(loginParams.getPassword());
|
||
|
|
||
|
//查看 账号/手机号 是否存在
|
||
|
ManagementSecurityUnitUser managementSecurityUnitUser = securityUnitUserService.lambdaQuery()
|
||
|
.eq(ManagementSecurityUnitUser::getAccount, accountOrTelephone)
|
||
|
.or()
|
||
|
.eq(ManagementSecurityUnitUser::getTelephone, accountOrTelephone)
|
||
|
.oneOpt()
|
||
|
.orElseThrow(() -> new MessageException(ResultCode.ERROR));
|
||
|
|
||
|
//判断用户是否禁用
|
||
|
if (managementSecurityUnitUser.getIsEnable().equals(IsEnable.FALSE)) {
|
||
|
throw new MessageException(ResultCode.USER_IS_DISABLE);
|
||
|
}
|
||
|
|
||
|
//判断单位是否禁用
|
||
|
IsEnable unitEnable = securityUnitService.lambdaQuery()
|
||
|
.eq(BaseEntity::getSnowFlakeId, managementSecurityUnitUser.getSecurityUnitId())
|
||
|
.oneOpt()
|
||
|
.map(SecurityUnit::getIsEnable)
|
||
|
.orElseThrow(() -> new MessageException("单位不存在"));
|
||
|
if (unitEnable.equals(IsEnable.FALSE)) {
|
||
|
throw new MessageException("单位被禁用");
|
||
|
}
|
||
|
|
||
|
//判断密码是否正确
|
||
|
if (!UserUtil.verifyPassWord(password, managementSecurityUnitUser.getSalt(), managementSecurityUnitUser.getPassword())) {
|
||
|
throw new MessageException(ResultCode.PASSWORD_ERROR);
|
||
|
}
|
||
|
|
||
|
//登录
|
||
|
SaTokenInfo saTokenInfo = UserUtil.loginAndTokenInfo(managementSecurityUnitUser.getSnowFlakeId());
|
||
|
//返回token
|
||
|
return new TokenInfo(saTokenInfo.getTokenName(), saTokenInfo.getTokenValue());
|
||
|
}
|
||
|
}
|