|
|
|
package com.epmet.service.impl;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
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.*;
|
|
|
|
import com.epmet.commons.tools.validator.PhoneValidatorUtils;
|
|
|
|
import com.epmet.constant.AuthHttpUrlConstant;
|
|
|
|
import com.epmet.constant.PublicUserLoginConstant;
|
|
|
|
import com.epmet.constant.SmsTemplateConstant;
|
|
|
|
import com.epmet.constant.ThirdApiConstant;
|
|
|
|
import com.epmet.dto.PaCustomerDTO;
|
|
|
|
import com.epmet.dto.PaUserDTO;
|
|
|
|
import com.epmet.dto.PaUserWechatDTO;
|
|
|
|
import com.epmet.dto.form.*;
|
|
|
|
import com.epmet.dto.result.*;
|
|
|
|
import com.epmet.feign.EpmetMessageOpenFeignClient;
|
|
|
|
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 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());
|
|
|
|
WxCodeToTokenFormDTO dto = new WxCodeToTokenFormDTO();
|
|
|
|
dto.setWxMpUser(wxMpUser);
|
|
|
|
dto.setSource(formDTO.getSource());
|
|
|
|
|
|
|
|
//2.将获取的用户基本信息初始化到数据库
|
|
|
|
String data = HttpClientManager.getInstance().sendPostByJSON(ThirdApiConstant.THIRD_PAUSER_SAVEUSER, JSON.toJSONString(dto)).getData();
|
|
|
|
JSONObject toResult = JSON.parseObject(data);
|
|
|
|
Result result = ConvertUtils.mapToEntity(toResult, Result.class);
|
|
|
|
if (null != toResult.get("code")) {
|
|
|
|
result.setCode(((Integer) toResult.get("code")).intValue());
|
|
|
|
}
|
|
|
|
if (!result.success()) {
|
|
|
|
throw new RenException(PublicUserLoginConstant.SAVE_USER_EXCEPTION);
|
|
|
|
}
|
|
|
|
Object RegisterResult = result.getData();
|
|
|
|
JSONObject jsonObject = JSON.parseObject(RegisterResult.toString());
|
|
|
|
SaveUserResultDTO resultDTO = ConvertUtils.mapToEntity(jsonObject, SaveUserResultDTO.class);
|
|
|
|
|
|
|
|
//3.获取用户token
|
|
|
|
String token = this.generateGovWxmpToken(resultDTO.getUserId());
|
|
|
|
//4.保存到redis
|
|
|
|
String openid = wxMpUser.getOpenId();
|
|
|
|
String unionId = (null == wxMpUser.getUnionId() ? "" : wxMpUser.getUnionId());
|
|
|
|
this.saveLatestGovTokenDto("", resultDTO.getUserId(), openid, unionId, 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.getOpenId())){
|
|
|
|
logger.error("wxMpUser.getOpenId() is null");
|
|
|
|
throw new RenException("解析微信用户信息失败");
|
|
|
|
}
|
|
|
|
/*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(String customerId, String userId, String openId, String unionId, String token) {
|
|
|
|
TokenDto tokenDTO = new TokenDto();
|
|
|
|
int expire = jwtTokenProperties.getExpire();
|
|
|
|
tokenDTO.setApp(LoginConstant.APP_PUBLIC);
|
|
|
|
tokenDTO.setClient(LoginConstant.CLIENT_MP);
|
|
|
|
tokenDTO.setOpenId(openId);
|
|
|
|
tokenDTO.setUnionId(unionId);
|
|
|
|
tokenDTO.setToken(token);
|
|
|
|
//首次初始化时还没有客户
|
|
|
|
tokenDTO.setCustomerId(customerId);
|
|
|
|
tokenDTO.setUserId(userId);
|
|
|
|
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、根据数据来源和手机号校验用户是否存在
|
|
|
|
CheckPaUserFormDTO dto = new CheckPaUserFormDTO();
|
|
|
|
dto.setPhone(formDTO.getPhone());
|
|
|
|
dto.setSource(formDTO.getSource());
|
|
|
|
String data = HttpClientManager.getInstance().sendPostByJSON(ThirdApiConstant.THIRD_PAUSER_CHECKPAUSER, JSON.toJSONString(dto)).getData();
|
|
|
|
JSONObject toResult = JSON.parseObject(data);
|
|
|
|
Result result = ConvertUtils.mapToEntity(toResult, Result.class);
|
|
|
|
if (null != toResult.get("code")) {
|
|
|
|
result.setCode(((Integer) toResult.get("code")).intValue());
|
|
|
|
}
|
|
|
|
if (!result.success()) {
|
|
|
|
logger.error(String.format(SEND_SMS_CODE_ERROR, formDTO.getPhone(), result.getCode(), result.getMsg()));
|
|
|
|
throw new RenException(result.getCode());
|
|
|
|
}
|
|
|
|
Object RegisterResult = result.getData();
|
|
|
|
JSONObject jsonObject = JSON.parseObject(RegisterResult.toString());
|
|
|
|
Map<String,Object> map = (Map)jsonObject.get("paUserResult");
|
|
|
|
PaUserDTO userDTO = ConvertUtils.mapToEntity(map, PaUserDTO.class);
|
|
|
|
//登陆
|
|
|
|
if (formDTO.getIsLogon() && null == userDTO) {
|
|
|
|
throw new RenException(EpmetErrorCode.PUBLIC_NOT_EXISTS.getCode());
|
|
|
|
}
|
|
|
|
//注册
|
|
|
|
if (!formDTO.getIsLogon() && null != userDTO) {
|
|
|
|
throw new RenException(EpmetErrorCode.MOBILE_USED.getCode());
|
|
|
|
}
|
|
|
|
//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.根据数据来源和手机号查询用户、客户信息
|
|
|
|
CheckPaUserFormDTO dto = new CheckPaUserFormDTO();
|
|
|
|
dto.setPhone(formDTO.getPhone());
|
|
|
|
dto.setSource(formDTO.getSource());
|
|
|
|
String data = HttpClientManager.getInstance().sendPostByJSON(ThirdApiConstant.THIRD_PAUSER_CHECKPAUSER, JSON.toJSONString(dto)).getData();
|
|
|
|
JSONObject toResult = JSON.parseObject(data);
|
|
|
|
Result result = ConvertUtils.mapToEntity(toResult, Result.class);
|
|
|
|
if (null != toResult.get("code")) {
|
|
|
|
result.setCode(((Integer) toResult.get("code")).intValue());
|
|
|
|
}
|
|
|
|
if (!result.success()) {
|
|
|
|
logger.error(String.format("手机验证码登录异常,手机号[%s],code[%s],msg[%s]", formDTO.getPhone(), result.getCode(), result.getMsg()));
|
|
|
|
throw new RenException(result.getCode());
|
|
|
|
}
|
|
|
|
Object RegisterResult = result.getData();
|
|
|
|
JSONObject jsonObject = JSON.parseObject(RegisterResult.toString());
|
|
|
|
|
|
|
|
//2.用户不存在时不允许登陆
|
|
|
|
Map<String,Object> map1 = (Map)jsonObject.get("paUserResult");
|
|
|
|
PaUserDTO userDTO = ConvertUtils.mapToEntity(map1, PaUserDTO.class);
|
|
|
|
if (null == userDTO || StringUtils.isBlank(userDTO.getId())) {
|
|
|
|
throw new RenException(EpmetErrorCode.PUBLIC_NOT_EXISTS.getCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
//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是根据登陆手机号对应的user生成的token,访问记录表记录的是那个user根据自己或他人的手机号登陆的
|
|
|
|
//4.直接生成一个新的token放入缓存中(不管缓存中是否存在旧的token,都重新生成)
|
|
|
|
//4-1.生成token
|
|
|
|
String token = this.generateGovWxmpToken(userDTO.getId());
|
|
|
|
//4-2.判断是否存在信息,给customerId赋值
|
|
|
|
Map<String,Object> map2 = (Map)jsonObject.get("paCustomerResult");
|
|
|
|
PaCustomerDTO customerDTO = ConvertUtils.mapToEntity(map2, PaCustomerDTO.class);
|
|
|
|
String customerId = "";
|
|
|
|
if (null != customerDTO && !StringUtils.isBlank(customerDTO.getId())) {
|
|
|
|
customerId = customerDTO.getId();
|
|
|
|
}
|
|
|
|
//4-3.token存入redis
|
|
|
|
Map<String,Object> map3 = (Map)jsonObject.get("paUserWechatResult");
|
|
|
|
PaUserWechatDTO wechatDTO = ConvertUtils.mapToEntity(map3, PaUserWechatDTO.class);
|
|
|
|
String openid = wechatDTO.getWxOpenId();
|
|
|
|
String unionId = (null == wechatDTO.getUnionId() ? "" : wechatDTO.getUnionId());
|
|
|
|
this.saveLatestGovTokenDto(customerId, userDTO.getId(), openid, unionId, token);
|
|
|
|
|
|
|
|
//5.登陆成功,访问记录表新增访问记录(访问记录新增失败不应影响用户登陆)
|
|
|
|
SaveUserVisitedFormDTO visited = new SaveUserVisitedFormDTO();
|
|
|
|
visited.setUserId(userDTO.getId());
|
|
|
|
visited.setLogonUserId(tokenDTO.getUserId());
|
|
|
|
visited.setPhone(formDTO.getPhone());
|
|
|
|
visited.setSource(formDTO.getSource());
|
|
|
|
String data1 = HttpClientManager.getInstance().sendPostByJSON(ThirdApiConstant.THIRD_PAUSERVISITED_SAVEUSERVISITED, JSON.toJSONString(visited)).getData();
|
|
|
|
JSONObject json = JSON.parseObject(data1);
|
|
|
|
Result visitedResult = ConvertUtils.mapToEntity(json, Result.class);
|
|
|
|
if (null != json.get("code")) {
|
|
|
|
visitedResult.setCode(((Integer) json.get("code")).intValue());
|
|
|
|
}
|
|
|
|
if (!visitedResult.success()) {
|
|
|
|
logger.error(PublicUserLoginConstant.SAVE_VISITED_EXCEPTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
//6.返回token
|
|
|
|
UserTokenResultDTO userTokenResultDTO = new UserTokenResultDTO();
|
|
|
|
userTokenResultDTO.setToken(token);
|
|
|
|
return userTokenResultDTO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param formDTO
|
|
|
|
* @return
|
|
|
|
* @Author sun
|
|
|
|
* @Description 公众号-手机号注册
|
|
|
|
**/
|
|
|
|
@Override
|
|
|
|
public UserTokenResultDTO register(RegisterFormDTO formDTO) {
|
|
|
|
//1.调用epmet-third服务,完成信息注册
|
|
|
|
String data = HttpClientManager.getInstance().sendPostByJSON(AuthHttpUrlConstant.REGISTER_URL, JSON.toJSONString(formDTO)).getData();
|
|
|
|
JSONObject toResult = JSON.parseObject(data);
|
|
|
|
Result result = ConvertUtils.mapToEntity(toResult, Result.class);
|
|
|
|
if (null != toResult.get("code")) {
|
|
|
|
result.setCode(((Integer) toResult.get("code")).intValue());
|
|
|
|
}
|
|
|
|
if (!result.success()) {
|
|
|
|
logger.error(String.format("调用epmet_third服务初始化用户信息失败,数据来源[%s],手机号[%s],userId:[%S]", formDTO.getSource(), formDTO.getPhone(), formDTO.getUserId()));
|
|
|
|
throw new RenException(result.getCode());
|
|
|
|
}
|
|
|
|
Object RegisterResult = result.getData();
|
|
|
|
JSONObject jsonObject = JSON.parseObject(RegisterResult.toString());
|
|
|
|
RegisterResultDTO resultDTO = ConvertUtils.mapToEntity(jsonObject, RegisterResultDTO.class);
|
|
|
|
|
|
|
|
//2.直接生成一个新的token放入缓存中(不管缓存中是否存在旧的token,都重新生成)
|
|
|
|
//2-1.生成token
|
|
|
|
String token = this.generateGovWxmpToken(resultDTO.getUserId());
|
|
|
|
//2-2.token存入redis
|
|
|
|
String openid = resultDTO.getOpenId();
|
|
|
|
String unionId = (null == resultDTO.getUnionId() ? "" : resultDTO.getUnionId());
|
|
|
|
this.saveLatestGovTokenDto("", resultDTO.getUserId(), openid, unionId, token);
|
|
|
|
|
|
|
|
//3.返回token
|
|
|
|
UserTokenResultDTO userTokenResultDTO = new UserTokenResultDTO();
|
|
|
|
userTokenResultDTO.setToken(token);
|
|
|
|
return userTokenResultDTO;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|