|
|
|
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.utils.CpUserDetailRedis;
|
|
|
|
import com.epmet.commons.tools.utils.DateUtils;
|
|
|
|
import com.epmet.commons.tools.utils.Result;
|
|
|
|
import com.epmet.commons.tools.validator.PhoneValidatorUtils;
|
|
|
|
import com.epmet.constant.PublicUserLoginConstant;
|
|
|
|
import com.epmet.constant.SmsTemplateConstant;
|
|
|
|
import com.epmet.dto.PaCustomerDTO;
|
|
|
|
import com.epmet.dto.PaUserDTO;
|
|
|
|
import com.epmet.dto.form.*;
|
|
|
|
import com.epmet.dto.result.CustomerUserResultDTO;
|
|
|
|
import com.epmet.dto.result.SaveUserResultDTO;
|
|
|
|
import com.epmet.dto.result.SendVerificationCodeResultDTO;
|
|
|
|
import com.epmet.dto.result.UserTokenResultDTO;
|
|
|
|
import com.epmet.feign.EpmetMessageOpenFeignClient;
|
|
|
|
import com.epmet.feign.EpmetThirdFeignClient;
|
|
|
|
import com.epmet.jwt.JwtTokenProperties;
|
|
|
|
import com.epmet.jwt.JwtTokenUtils;
|
|
|
|
import com.epmet.redis.CaptchaRedis;
|
|
|
|
import com.epmet.service.PublicUserLoginService;
|
|
|
|
import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
|
import me.chanjar.weixin.mp.api.WxMpService;
|
|
|
|
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
|
|
|
|
import me.chanjar.weixin.mp.bean.result.WxMpUser;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
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 yinzuomei@elink-cn.com
|
|
|
|
* @date 2020/7/8 18:31
|
|
|
|
*/
|
|
|
|
@Service
|
|
|
|
public class PublicUserLoginServiceImpl implements PublicUserLoginService {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(PublicUserLoginServiceImpl.class);
|
|
|
|
private static final String SEND_SMS_CODE_ERROR = "发送短信验证码异常,手机号[%s],code[%s],msg[%s]";
|
|
|
|
@Autowired
|
|
|
|
private WxMpService wxMpService;
|
|
|
|
@Autowired
|
|
|
|
private EpmetThirdFeignClient epmetThirdFeignClient;
|
|
|
|
@Autowired
|
|
|
|
private JwtTokenUtils jwtTokenUtils;
|
|
|
|
@Autowired
|
|
|
|
private JwtTokenProperties jwtTokenProperties;
|
|
|
|
@Autowired
|
|
|
|
private CpUserDetailRedis cpUserDetailRedis;
|
|
|
|
@Autowired
|
|
|
|
private EpmetMessageOpenFeignClient epmetMessageOpenFeignClient;
|
|
|
|
@Autowired
|
|
|
|
private CaptchaRedis captchaRedis;
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public UserTokenResultDTO wxCodeToToken(PaWxCodeFormDTO formDTO) {
|
|
|
|
//1.通过微信code获取用户基本信息
|
|
|
|
WxMpUser wxMpUser = this.getWxMpUser(formDTO.getWxCode());
|
|
|
|
|
|
|
|
//2.将获取的用户基本信息初始化到数据库
|
|
|
|
Result<SaveUserResultDTO> result = epmetThirdFeignClient.saveUser(wxMpUser);
|
|
|
|
if (!result.success()) {
|
|
|
|
throw new RenException(PublicUserLoginConstant.SAVE_USER_EXCEPTION);
|
|
|
|
}
|
|
|
|
SaveUserResultDTO resultDTO = result.getData();
|
|
|
|
|
|
|
|
//3.获取用户token
|
|
|
|
String token = this.generateGovWxmpToken(resultDTO.getUserId());
|
|
|
|
//4.保存到redis
|
|
|
|
this.saveLatestGovTokenDto(resultDTO, wxMpUser, token);
|
|
|
|
UserTokenResultDTO userTokenResultDTO = new UserTokenResultDTO();
|
|
|
|
userTokenResultDTO.setToken(token);
|
|
|
|
return userTokenResultDTO;
|
|
|
|
}
|
|
|
|
|
|
|
|
private WxMpUser getWxMpUser(String wxCode) {
|
|
|
|
WxMpUser wxMpUser = null;
|
|
|
|
try {
|
|
|
|
WxMpOAuth2AccessToken wxMpOAuth2AccessToken = wxMpService.oauth2getAccessToken(wxCode);
|
|
|
|
wxMpUser = wxMpService.oauth2getUserInfo(wxMpOAuth2AccessToken, null);
|
|
|
|
} catch (WxErrorException e) {
|
|
|
|
logger.error("->[getWxMpUser]::error[{}]", "解析微信用户信息失败", e.getMessage());
|
|
|
|
e.printStackTrace();
|
|
|
|
throw new RenException("解析微信用户信息失败" + e.getMessage());
|
|
|
|
}
|
|
|
|
if (null == wxMpUser ) {
|
|
|
|
logger.error("wxMpUser is null");
|
|
|
|
throw new RenException("解析微信用户信息失败 wxMpUser is null");
|
|
|
|
}
|
|
|
|
/*if(StringUtils.isBlank(wxMpUser.getUnionId())){
|
|
|
|
logger.error("wxMpUser.getUnionId() is null");
|
|
|
|
// throw new RenException("解析微信用户信息失败");
|
|
|
|
}*/
|
|
|
|
return wxMpUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Description 生成token
|
|
|
|
**/
|
|
|
|
private String generateGovWxmpToken(String userId) {
|
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
|
map.put("app", LoginConstant.APP_PUBLIC);
|
|
|
|
map.put("client", LoginConstant.CLIENT_MP);
|
|
|
|
map.put("userId", userId);
|
|
|
|
String token = jwtTokenUtils.createToken(map);
|
|
|
|
logger.info("app:" + LoginConstant.APP_PUBLIC + ";client:" + LoginConstant.CLIENT_MP + ";userId:" + userId + ";生成token[" + token + "]");
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
//保存tokenDto到redis
|
|
|
|
private void saveLatestGovTokenDto(SaveUserResultDTO resultDTO, WxMpUser wxMpUser, String token) {
|
|
|
|
TokenDto tokenDTO = new TokenDto();
|
|
|
|
int expire = jwtTokenProperties.getExpire();
|
|
|
|
tokenDTO.setApp(LoginConstant.APP_PUBLIC);
|
|
|
|
tokenDTO.setClient(LoginConstant.CLIENT_MP);
|
|
|
|
tokenDTO.setOpenId(wxMpUser.getOpenId());
|
|
|
|
tokenDTO.setUnionId(null == wxMpUser.getUnionId() ? "" : wxMpUser.getUnionId());
|
|
|
|
tokenDTO.setToken(token);
|
|
|
|
//首次初始化时还没有客户
|
|
|
|
tokenDTO.setCustomerId("");
|
|
|
|
tokenDTO.setUserId(resultDTO.getUserId());
|
|
|
|
tokenDTO.setExpireTime(jwtTokenUtils.getExpiration(token).getTime());
|
|
|
|
tokenDTO.setUpdateTime(System.currentTimeMillis());
|
|
|
|
cpUserDetailRedis.set(tokenDTO, expire);
|
|
|
|
logger.info("截止时间:" + DateUtils.format(jwtTokenUtils.getExpiration(token), "yyyy-MM-dd HH:mm:ss"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param formDTO 手机号
|
|
|
|
* @return com.epmet.commons.tools.utils.Result
|
|
|
|
* @Author sun
|
|
|
|
* @Description 公众号登录-发送验证码
|
|
|
|
**/
|
|
|
|
@Override
|
|
|
|
public void sendSmsCode(PublicSendSmsCodeFormDTO formDTO) {
|
|
|
|
//1、校验手机号是否符合规范
|
|
|
|
if (!PhoneValidatorUtils.isMobile(formDTO.getPhone())) {
|
|
|
|
logger.error(String.format(SEND_SMS_CODE_ERROR, formDTO.getPhone(), EpmetErrorCode.ERROR_PHONE.getCode(), EpmetErrorCode.ERROR_PHONE.getMsg()));
|
|
|
|
throw new RenException(EpmetErrorCode.ERROR_PHONE.getCode());
|
|
|
|
}
|
|
|
|
//2、根据手机号校验用户是否存在
|
|
|
|
Result<CustomerUserResultDTO> Result = epmetThirdFeignClient.checkPaUser(formDTO.getPhone());
|
|
|
|
if (!Result.success()) {
|
|
|
|
logger.error(String.format(SEND_SMS_CODE_ERROR, formDTO.getPhone(), Result.getCode(), Result.getMsg()));
|
|
|
|
throw new RenException(Result.getCode());
|
|
|
|
}
|
|
|
|
CustomerUserResultDTO ResultDTO = Result.getData();
|
|
|
|
//登陆
|
|
|
|
if (formDTO.getIsLogon() && null == ResultDTO.getPaUserResult()) {
|
|
|
|
throw new RenException(PublicUserLoginConstant.LOGON_EXCEPTION);
|
|
|
|
}
|
|
|
|
//注册
|
|
|
|
if (!formDTO.getIsLogon() && null != ResultDTO.getPaUserResult()) {
|
|
|
|
throw new RenException(PublicUserLoginConstant.ZHU_CE_EXCEPTION);
|
|
|
|
}
|
|
|
|
//3、发送短信验证码
|
|
|
|
SendVerificationCodeFormDTO sendVerificationCodeFormDTO = new SendVerificationCodeFormDTO();
|
|
|
|
sendVerificationCodeFormDTO.setMobile(formDTO.getPhone());
|
|
|
|
//登陆或注册对应的短息模板
|
|
|
|
sendVerificationCodeFormDTO.setAliyunTemplateCode(formDTO.getIsLogon() ? SmsTemplateConstant.LGOIN_CONFIRM : SmsTemplateConstant.USER_REGISTER);
|
|
|
|
Result<SendVerificationCodeResultDTO> smsCodeResult = epmetMessageOpenFeignClient.sendVerificationCode(sendVerificationCodeFormDTO);
|
|
|
|
if (!smsCodeResult.success()) {
|
|
|
|
logger.error(String.format(SEND_SMS_CODE_ERROR, formDTO.getPhone(), smsCodeResult.getCode(), smsCodeResult.getMsg()));
|
|
|
|
throw new RenException(smsCodeResult.getCode());
|
|
|
|
}
|
|
|
|
//4、保存短信验证码(删除现有短信验证码、将新的短信验证码存入Redis)
|
|
|
|
captchaRedis.savePublicSmsCode(formDTO, smsCodeResult.getData().getCode());
|
|
|
|
logger.info(String.format("发送短信验证码成功,手机号[%s]", formDTO.getPhone()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param formDTO
|
|
|
|
* @return com.epmet.commons.tools.utils.Result
|
|
|
|
* @Author sun
|
|
|
|
* @Description 公众号-手机验证码登陆
|
|
|
|
**/
|
|
|
|
@Override
|
|
|
|
public UserTokenResultDTO loginByPhone(TokenDto tokenDTO, LoginByPhoneFormDTO formDTO) {
|
|
|
|
//1.根据手机号查询到用户、客户信息
|
|
|
|
Result<CustomerUserResultDTO> result = epmetThirdFeignClient.checkPaUser(formDTO.getPhone());
|
|
|
|
if (!result.success()) {
|
|
|
|
logger.error(String.format("手机验证码登录异常,手机号[%s],code[%s],msg[%s]", formDTO.getPhone(), result.getCode(), result.getMsg()));
|
|
|
|
throw new RenException(result.getCode());
|
|
|
|
}
|
|
|
|
CustomerUserResultDTO resultDTO = result.getData();
|
|
|
|
|
|
|
|
//2.用户不存在时不允许登陆
|
|
|
|
PaUserDTO userDTO = resultDTO.getPaUserResult();
|
|
|
|
if (null == userDTO || StringUtils.isBlank(userDTO.getId())) {
|
|
|
|
throw new RenException(PublicUserLoginConstant.LOGON_EXCEPTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
//3.校验验证码是否正确
|
|
|
|
String rightSmsCode = captchaRedis.getPublicSmsCode(formDTO.getPhone());
|
|
|
|
if (!formDTO.getSmsCode().equals(rightSmsCode)) {
|
|
|
|
logger.error(String.format("验证码错误code[%s],msg[%s]", EpmetErrorCode.MOBILE_CODE_ERROR.getCode(), EpmetErrorCode.MOBILE_CODE_ERROR.getMsg()));
|
|
|
|
throw new RenException(EpmetErrorCode.MOBILE_CODE_ERROR.getCode());
|
|
|
|
}
|
|
|
|
//获取缓存中的token
|
|
|
|
TokenDto redisTokenDTO = cpUserDetailRedis.get(LoginConstant.APP_PUBLIC, LoginConstant.CLIENT_MP, userDTO.getId(), TokenDto.class);
|
|
|
|
if (redisTokenDTO == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
//4.判断是否存在客户信息,是否需要生成新的token
|
|
|
|
PaCustomerDTO customerDTO = resultDTO.getPaCustomerResult();
|
|
|
|
if (null != customerDTO && !StringUtils.isBlank(customerDTO.getId())) {
|
|
|
|
redisTokenDTO.setCustomerId(customerDTO.getId());
|
|
|
|
int expire = jwtTokenProperties.getExpire();
|
|
|
|
cpUserDetailRedis.set(redisTokenDTO, expire);
|
|
|
|
}
|
|
|
|
|
|
|
|
//5.登陆成功,访问记录表新增访问记录(访问记录新增失败不应影响用户登陆)
|
|
|
|
SaveUserVisitedFormDTO visited = new SaveUserVisitedFormDTO();
|
|
|
|
visited.setUserId(userDTO.getId());
|
|
|
|
visited.setLogonUserId(tokenDTO.getUserId());
|
|
|
|
visited.setPhone(formDTO.getPhone());
|
|
|
|
Result visitedResult = epmetThirdFeignClient.saveUserVisited(visited);
|
|
|
|
if(!visitedResult.success()){
|
|
|
|
logger.error(PublicUserLoginConstant.SAVE_VISITED_EXCEPTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
//6.返回token
|
|
|
|
UserTokenResultDTO userTokenResultDTO = new UserTokenResultDTO();
|
|
|
|
userTokenResultDTO.setToken(redisTokenDTO.getToken());
|
|
|
|
return userTokenResultDTO;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|