package com.epmet.service.impl; import com.epmet.common.token.constant.LoginConstant; import com.epmet.commons.tools.exception.EpmetErrorCode; import com.epmet.commons.tools.exception.RenException; import com.epmet.commons.tools.security.dto.TokenDto; 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.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 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 { 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; /** * @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 result = epmetUserFeignClient.getStaffIdAndPwd(form); if (!result.success() || null == result.getData() || null == result.getData().getUserId()) { logger.error("根据手机号查询PC工作端登陆人员信息失败,返回10003账号不存在"); throw new RenException(EpmetErrorCode.ERR10003.getCode()); } 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.setToken(this.packagingUserToken(formDTO, resultDTO.getUserId())); return userTokenResultDTO; } /** * 生成PC工作端token * @author sun */ private String packagingUserToken(GovWebLoginFormDTO formDTO, String userId) { // 生成token Map 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(); TokenDto tokenDto = new TokenDto(); tokenDto.setApp(formDTO.getApp()); tokenDto.setClient(formDTO.getClient()); tokenDto.setUserId(userId); tokenDto.setToken(token); tokenDto.setUpdateTime(System.currentTimeMillis()); tokenDto.setExpireTime(jwtTokenUtils.getExpiration(token).getTime()); cpUserDetailRedis.set(tokenDto, expire); logger.info("截止时间:" + DateUtils.format(jwtTokenUtils.getExpiration(token), "yyyy-MM-dd HH:mm:ss")); return token; } }