日照智慧社区接口服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

219 lines
10 KiB

package com.epmet.service.impl;
import com.epmet.auth.constants.AuthOperationConstants;
import com.epmet.auth.dto.result.BlockChainStaffAuthResultDTO;
import com.epmet.common.token.constant.LoginConstant;
import com.epmet.commons.tools.constant.AppClientConstant;
import com.epmet.commons.tools.constant.ServiceConstant;
import com.epmet.commons.tools.dto.result.CustomerStaffInfoCacheResult;
import com.epmet.commons.tools.exception.EpmetErrorCode;
import com.epmet.commons.tools.exception.EpmetException;
import com.epmet.commons.tools.exception.RenException;
import com.epmet.commons.tools.feign.ResultDataResolver;
import com.epmet.commons.tools.redis.common.CustomerStaffRedis;
import com.epmet.commons.tools.security.dto.GovTokenDto;
import com.epmet.commons.tools.security.password.PasswordUtils;
import com.epmet.commons.tools.utils.CpUserDetailRedis;
import com.epmet.commons.tools.utils.DateUtils;
import com.epmet.commons.tools.utils.Result;
import com.epmet.commons.tools.utils.SpringContextUtils;
import com.epmet.dto.form.GovWebLoginFormDTO;
import com.epmet.dto.form.GovWebOperLoginFormDTO;
import com.epmet.dto.result.GovWebOperLoginResultDTO;
import com.epmet.dto.result.UserTokenResultDTO;
import com.epmet.feign.EpmetUserFeignClient;
import com.epmet.jwt.JwtTokenProperties;
import com.epmet.jwt.JwtTokenUtils;
import com.epmet.service.CaptchaService;
import com.epmet.service.GovWebService;
import com.epmet.service.ThirdLoginService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* @author sun
* @Description 第三方-居民端、政府端登陆服务
*/
@Slf4j
@Service
public class GovWebServiceImpl implements GovWebService, ResultDataResolver {
private static final Logger logger = LoggerFactory.getLogger(GovWebServiceImpl.class);
@Autowired
private CaptchaService captchaService;
@Autowired
private JwtTokenUtils jwtTokenUtils;
@Autowired
private JwtTokenProperties jwtTokenProperties;
@Autowired
private CpUserDetailRedis cpUserDetailRedis;
@Autowired
private EpmetUserFeignClient epmetUserFeignClient;
@Autowired
private ThirdLoginServiceImpl thirdLoginService;
/**
* @param formDTO
* @return
* @Author sun
* @Description PC工作端-工作人员登录
**/
@Override
public UserTokenResultDTO login(GovWebLoginFormDTO formDTO) {
//1.参数校验
if (!(LoginConstant.APP_GOV.equals(formDTO.getApp()) && LoginConstant.CLIENT_WEB.equals(formDTO.getClient()))) {
logger.error("当前接口只适用于PC工作端运营管理后台");
throw new RenException("当前接口只适用于PC工作端运营管理后台");
}
//2.验证码校验
boolean flag = captchaService.validate(formDTO.getUuid(), formDTO.getCaptcha());
if (!flag) {
logger.warn(String.format("用户%s登录,验证码输入错误", formDTO.getPhone()));
//开启验证码校验
throw new RenException(EpmetErrorCode.ERR10019.getCode());
}
//3.校验登陆账号是否存在
//根据客户Id和手机号查询登陆用户信息(此处不需要判断登陆人是否是有效客户以及是否是客户的根管理员,前一接口获取登陆手机号对应客户列表已经判断了)
GovWebOperLoginFormDTO form = new GovWebOperLoginFormDTO();
form.setCustomerId(formDTO.getCustomerId());
form.setMobile(formDTO.getPhone());
Result<GovWebOperLoginResultDTO> result = epmetUserFeignClient.getStaffIdAndPwd(form);
if (!result.success() || null == result.getData() || null == result.getData().getUserId()) {
logger.warn("根据手机号查询PC工作端登陆人员信息失败,返回10003账号不存在");
throw new RenException(EpmetErrorCode.ERR10003.getCode());
}
//未禁用enable,已禁用disabled
if ("disabled".equals(result.getData().getEnableFlag())) {
throw new EpmetException(EpmetErrorCode.GOV_STAFF_DISABLED.getCode(),
String.format("当前账号已被禁用staffId:%s", result.getData().getUserId()),
EpmetErrorCode.GOV_STAFF_DISABLED.getMsg());
}
GovWebOperLoginResultDTO resultDTO = result.getData();
//4.密码是否正确
//密码错误
if (!PasswordUtils.matches(formDTO.getPassword(), resultDTO.getPassWord())) {
logger.warn("登陆密码错误");
throw new RenException(EpmetErrorCode.ERR10004.getCode());
}
//5.生成token存到redis并返回
UserTokenResultDTO userTokenResultDTO = new UserTokenResultDTO();
userTokenResultDTO.setCustomerId(formDTO.getCustomerId());
userTokenResultDTO.setToken(this.packagingUserToken(formDTO, resultDTO.getUserId()));
// 6.发送登录事件
try {
SpringContextUtils.getBean(ThirdLoginService.class).sendLoginEvent(resultDTO.getUserId(), "数字社区登录",
AppClientConstant.APP_GOV,
AppClientConstant.CLIENT_WEB,
AuthOperationConstants.LOGIN);
} catch (Exception e) {
log.error("【数字社区web端登录】发送登录事件失败,程序继续执行。错误信息");
}
return userTokenResultDTO;
}
@Override
public UserTokenResultDTO loginByThirdPlatform(GovWebLoginFormDTO formDTO) {
formDTO.setApp(LoginConstant.APP_GOV);
formDTO.setClient(LoginConstant.CLIENT_WEB);
// //1.参数校验
// if (!(LoginConstant.APP_GOV.equals(formDTO.getApp()) && LoginConstant.CLIENT_WEB.equals(formDTO.getClient()))) {
// logger.error("当前接口只适用于PC工作端运营管理后台");
// throw new RenException("当前接口只适用于PC工作端运营管理后台");
// }
//3.校验登陆账号是否存在
//根据客户Id和手机号查询登陆用户信息(此处不需要判断登陆人是否是有效客户以及是否是客户的根管理员,前一接口获取登陆手机号对应客户列表已经判断了)
GovWebOperLoginFormDTO form = new GovWebOperLoginFormDTO();
form.setCustomerId(formDTO.getCustomerId());
form.setMobile(formDTO.getPhone());
Result<GovWebOperLoginResultDTO> result = epmetUserFeignClient.getStaffIdAndPwd(form);
if (!result.success() || null == result.getData() || null == result.getData().getUserId()) {
logger.warn("根据手机号查询PC工作端登陆人员信息失败,返回10003账号不存在");
throw new RenException(EpmetErrorCode.ERR10003.getCode());
}
GovWebOperLoginResultDTO resultDTO = result.getData();
//5.生成token存到redis并返回
UserTokenResultDTO userTokenResultDTO = new UserTokenResultDTO();
userTokenResultDTO.setToken(this.packagingUserToken(formDTO, resultDTO.getUserId()));
return userTokenResultDTO;
}
/**
* 生成PC工作端token
* @author sun
*/
private String packagingUserToken(GovWebLoginFormDTO formDTO, String userId) {
// 生成token
Map<String, Object> map = new HashMap<>();
map.put("app", formDTO.getApp());
map.put("client", formDTO.getClient());
map.put("userId", userId);
String token = jwtTokenUtils.createToken(map);
logger.info("app:" + formDTO.getApp() + ";client:" + formDTO.getClient() + ";userId:" + userId + ";生成token[" + token + "]");
int expire = jwtTokenProperties.getExpire();
String orgIdPath = thirdLoginService.getOrgIdPath(userId);
String[] orgIdPathParts = orgIdPath.split(":");
GovTokenDto tokenDto = new GovTokenDto();
tokenDto.setCustomerId(formDTO.getCustomerId());
tokenDto.setApp(formDTO.getApp());
tokenDto.setClient(formDTO.getClient());
tokenDto.setUserId(userId);
tokenDto.setToken(token);
tokenDto.setUpdateTime(System.currentTimeMillis());
tokenDto.setExpireTime(jwtTokenUtils.getExpiration(token).getTime());
tokenDto.setAgencyId(orgIdPathParts[orgIdPathParts.length - 1]);
tokenDto.setRootAgencyId(orgIdPathParts[0]);
tokenDto.setDeptIdList(thirdLoginService.getDeptartmentIdList(userId));
tokenDto.setGridIdList(thirdLoginService.getGridIdList(userId));
tokenDto.setRoleList(thirdLoginService.queryGovStaffRoles(userId, orgIdPathParts[orgIdPathParts.length - 1]));
tokenDto.setOrgIdPath(orgIdPath);
cpUserDetailRedis.set(tokenDto, expire);
logger.info("截止时间:" + DateUtils.format(jwtTokenUtils.getExpiration(token), "yyyy-MM-dd HH:mm:ss"));
return token;
}
@Override
public BlockChainStaffAuthResultDTO blockChainStaffAuthenticationByPwd(String customerId, String mobile, String password) {
GovWebOperLoginFormDTO form = new GovWebOperLoginFormDTO();
form.setCustomerId(customerId);
form.setMobile(mobile);
// 用户认证
GovWebOperLoginResultDTO staff = getResultDataOrThrowsException(epmetUserFeignClient.getStaffIdAndPwd(form), ServiceConstant.EPMET_USER_SERVER,
EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(),
String.format("【区块链用户认证】根据手机号%s和指定客户ID:%s认证用户失败", mobile, customerId),
"认证失败");
if (staff == null) {
throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(),
String.format("【区块链用户认证】根据手机号%s和指定客户ID:%s认证用户失败", mobile, customerId));
}
if (!PasswordUtils.matches(password, staff.getPassWord())) {
// 密码不匹配
throw new EpmetException(EpmetErrorCode.ERR10004.getCode(), String.format("【区块链用户认证】密码不匹配,手机号:%s", mobile));
}
// 用户基础信息
CustomerStaffInfoCacheResult staffInfo = CustomerStaffRedis.getStaffInfo(customerId, staff.getUserId());
BlockChainStaffAuthResultDTO r = new BlockChainStaffAuthResultDTO(staff.getUserId(), staffInfo.getRealName(),
staffInfo.getMobile(), staffInfo.getHeadPhoto(), staffInfo.getAgencyId(), staffInfo.getAgencyName());
return r;
}
}