forked from luyan/epmet-cloud-lingshan
9 changed files with 208 additions and 352 deletions
@ -1,52 +0,0 @@ |
|||||
package com.epmet.controller; |
|
||||
|
|
||||
import com.epmet.commons.tools.utils.Result; |
|
||||
import com.epmet.commons.tools.validator.ValidatorUtils; |
|
||||
import com.epmet.dto.form.LoginByPhoneFormDTO; |
|
||||
import com.epmet.dto.form.SendSmsCodeFormDTO; |
|
||||
import com.epmet.dto.result.UserTokenResultDTO; |
|
||||
import com.epmet.service.GovLoginService; |
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||
import org.springframework.web.bind.annotation.PostMapping; |
|
||||
import org.springframework.web.bind.annotation.RequestBody; |
|
||||
import org.springframework.web.bind.annotation.RequestMapping; |
|
||||
import org.springframework.web.bind.annotation.RestController; |
|
||||
|
|
||||
/** |
|
||||
* @Description 政府端登录api入口 |
|
||||
* @Author yinzuomei |
|
||||
* @Date 2020/4/18 10:32 |
|
||||
*/ |
|
||||
@RestController |
|
||||
@RequestMapping("/gov/login") |
|
||||
public class GovLoginController { |
|
||||
|
|
||||
@Autowired |
|
||||
private GovLoginService govLoginService; |
|
||||
|
|
||||
/** |
|
||||
* @param formDTO 手机号 |
|
||||
* @return com.epmet.commons.tools.utils.Result |
|
||||
* @Author yinzuomei |
|
||||
* @Description 登录-发送验证码 |
|
||||
* @Date 2020/4/18 10:58 |
|
||||
**/ |
|
||||
@PostMapping(value = "sendsmscode") |
|
||||
public Result sendSmsCode(@RequestBody SendSmsCodeFormDTO formDTO) { |
|
||||
ValidatorUtils.validateEntity(formDTO); |
|
||||
return govLoginService.sendSmsCode(formDTO); |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* @param formDTO |
|
||||
* @return com.epmet.commons.tools.utils.Result<com.epmet.common.token.dto.result.UserTokenResultDTO> |
|
||||
* @Author yinzuomei |
|
||||
* @Description 手机验证码登录 |
|
||||
* @Date 2020/4/18 21:14 |
|
||||
**/ |
|
||||
@PostMapping(value = "loginbyphone") |
|
||||
public Result<UserTokenResultDTO> loginByPhone(LoginByPhoneFormDTO formDTO) { |
|
||||
ValidatorUtils.validateEntity(formDTO); |
|
||||
return govLoginService.loginByPhone(formDTO); |
|
||||
} |
|
||||
} |
|
@ -1,32 +0,0 @@ |
|||||
package com.epmet.service; |
|
||||
|
|
||||
import com.epmet.commons.tools.utils.Result; |
|
||||
import com.epmet.dto.form.LoginByPhoneFormDTO; |
|
||||
import com.epmet.dto.form.SendSmsCodeFormDTO; |
|
||||
import com.epmet.dto.result.UserTokenResultDTO; |
|
||||
|
|
||||
/** |
|
||||
* @Description 政府端登录 |
|
||||
* @Author yinzuomei |
|
||||
* @Date 2020/4/18 10:32 |
|
||||
*/ |
|
||||
public interface GovLoginService { |
|
||||
|
|
||||
/** |
|
||||
* @param formDTO |
|
||||
* @return com.epmet.commons.tools.utils.Result |
|
||||
* @Author yinzuomei |
|
||||
* @Description 登录-发送验证码 |
|
||||
* @Date 2020/4/18 10:59 |
|
||||
**/ |
|
||||
Result sendSmsCode(SendSmsCodeFormDTO formDTO); |
|
||||
|
|
||||
/** |
|
||||
* @param formDTO |
|
||||
* @return com.epmet.commons.tools.utils.Result<com.epmet.common.token.dto.result.UserTokenResultDTO> |
|
||||
* @Author yinzuomei |
|
||||
* @Description 手机验证码登录 |
|
||||
* @Date 2020/4/18 21:11 |
|
||||
**/ |
|
||||
Result<UserTokenResultDTO> loginByPhone(LoginByPhoneFormDTO formDTO); |
|
||||
} |
|
@ -1,181 +0,0 @@ |
|||||
package com.epmet.service.impl; |
|
||||
|
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; |
|
||||
import com.epmet.commons.tools.exception.EpmetErrorCode; |
|
||||
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.dto.CustomerStaffDTO; |
|
||||
import com.epmet.dto.form.LoginByPhoneFormDTO; |
|
||||
import com.epmet.dto.form.LoginCommonFormDTO; |
|
||||
import com.epmet.dto.form.SendSmsCodeFormDTO; |
|
||||
import com.epmet.dto.form.StaffWechatFormDTO; |
|
||||
import com.epmet.dto.result.UserTokenResultDTO; |
|
||||
import com.epmet.feign.EpmetUserFeignClient; |
|
||||
import com.epmet.feign.MessageFeignClient; |
|
||||
import com.epmet.jwt.JwtTokenProperties; |
|
||||
import com.epmet.jwt.JwtTokenUtils; |
|
||||
import com.epmet.redis.CaptchaRedis; |
|
||||
import com.epmet.service.GovLoginService; |
|
||||
import com.epmet.service.LoginService; |
|
||||
import org.apache.logging.log4j.LogManager; |
|
||||
import org.apache.logging.log4j.Logger; |
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
|
||||
import org.springframework.stereotype.Service; |
|
||||
|
|
||||
import java.util.HashMap; |
|
||||
import java.util.Map; |
|
||||
|
|
||||
/** |
|
||||
* @Description 政府端登录 |
|
||||
* @Author yinzuomei |
|
||||
* @Date 2020/4/18 10:33 |
|
||||
*/ |
|
||||
@Service |
|
||||
public class GovLoginServiceImpl implements GovLoginService { |
|
||||
private Logger logger = LogManager.getLogger(getClass()); |
|
||||
@Autowired |
|
||||
private EpmetUserFeignClient epmetUserFeignClient; |
|
||||
@Autowired |
|
||||
private MessageFeignClient messageFeignClient; |
|
||||
@Autowired |
|
||||
private CaptchaRedis captchaRedis; |
|
||||
@Autowired |
|
||||
private LoginService loginService; |
|
||||
@Autowired |
|
||||
private JwtTokenUtils jwtTokenUtils; |
|
||||
@Autowired |
|
||||
private JwtTokenProperties jwtTokenProperties; |
|
||||
@Autowired |
|
||||
private CpUserDetailRedis cpUserDetailRedis; |
|
||||
|
|
||||
private static final String SEND_SMS_CODE_ERROR="发送短信验证码异常,手机号[%s],code[%s],msg[%s]"; |
|
||||
|
|
||||
/** |
|
||||
* @param formDTO |
|
||||
* @return com.epmet.commons.tools.utils.Result |
|
||||
* @Author yinzuomei |
|
||||
* @Description 登录-发送验证码 |
|
||||
* @Date 2020/4/18 10:59 |
|
||||
**/ |
|
||||
@Override |
|
||||
public Result sendSmsCode(SendSmsCodeFormDTO 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())); |
|
||||
return new Result().error(EpmetErrorCode.ERROR_PHONE.getCode()); |
|
||||
} |
|
||||
//2、根据手机号校验用户是否存在
|
|
||||
Result<CustomerStaffDTO> customerStaffResult=epmetUserFeignClient.checkCustomerStaff(formDTO.getPhone()); |
|
||||
if(!customerStaffResult.success()){ |
|
||||
logger.error(String.format(SEND_SMS_CODE_ERROR,formDTO.getPhone(),customerStaffResult.getCode(),customerStaffResult.getMsg())); |
|
||||
return new Result().error(customerStaffResult.getCode()); |
|
||||
} |
|
||||
//3、发送短信验证码
|
|
||||
Result<Map<String, String>> smsCodeResult=messageFeignClient.sendSmsCaptcha(formDTO.getPhone()); |
|
||||
if(!smsCodeResult.success()){ |
|
||||
logger.error(String.format(SEND_SMS_CODE_ERROR,formDTO.getPhone(),smsCodeResult.getCode(),smsCodeResult.getMsg())); |
|
||||
return new Result().error(smsCodeResult.getCode()); |
|
||||
} |
|
||||
//4、保存短信验证码(删除现有短信验证码、将新的短信验证码存入Redis)
|
|
||||
captchaRedis.saveSmsCode(formDTO,smsCodeResult.getData().get("code")); |
|
||||
logger.info(String.format("发送短信验证码成功,手机号[%s]",formDTO.getPhone())); |
|
||||
return new Result(); |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* @param formDTO |
|
||||
* @return com.epmet.commons.tools.utils.Result<com.epmet.common.token.dto.result.UserTokenResultDTO> |
|
||||
* @Author yinzuomei |
|
||||
* @Description 手机验证码登录 |
|
||||
* @Date 2020/4/18 21:11 |
|
||||
**/ |
|
||||
@Override |
|
||||
public Result<UserTokenResultDTO> loginByPhone(LoginByPhoneFormDTO formDTO) { |
|
||||
//1、根据手机号查询到用户信息
|
|
||||
Result<CustomerStaffDTO> customerStaffResult=epmetUserFeignClient.checkCustomerStaff(formDTO.getPhone()); |
|
||||
if(!customerStaffResult.success()){ |
|
||||
logger.error(String.format("手机验证码登录异常,手机号[%s],code[%s],msg[%s]",formDTO.getPhone(),customerStaffResult.getCode(),customerStaffResult.getMsg())); |
|
||||
return new Result().error(customerStaffResult.getCode()); |
|
||||
} |
|
||||
//2、验证码是否正确
|
|
||||
String rightSmsCode=captchaRedis.getSmsCode(formDTO); |
|
||||
if(!formDTO.getSmsCode().equals(rightSmsCode)){ |
|
||||
return new Result<UserTokenResultDTO>().error(EpmetErrorCode.MOBILE_CODE_ERROR.getCode()); |
|
||||
} |
|
||||
//3、解析wxCode
|
|
||||
UserTokenResultDTO userTokenResultDTO=this.getAuthorizationInfo(formDTO,customerStaffResult.getData()); |
|
||||
return new Result<UserTokenResultDTO>().ok(userTokenResultDTO); |
|
||||
} |
|
||||
|
|
||||
private UserTokenResultDTO getAuthorizationInfo(LoginByPhoneFormDTO formDTO, CustomerStaffDTO customerStaff) { |
|
||||
//1、解析微信用户
|
|
||||
WxMaJscode2SessionResult wxMaJscode2SessionResult=loginService.getWxMaUser(formDTO.getApp(),formDTO.getWxCode()); |
|
||||
//2、记录staff_wechat
|
|
||||
this.savestaffwechat(customerStaff.getUserId(),wxMaJscode2SessionResult.getOpenid()); |
|
||||
//3、获取用户token
|
|
||||
String token=this.generateToken(formDTO,customerStaff.getUserId()); |
|
||||
//4、保存到redis
|
|
||||
this.saveTokenDto(formDTO,customerStaff.getUserId(),wxMaJscode2SessionResult,token); |
|
||||
UserTokenResultDTO userTokenResultDTO = new UserTokenResultDTO(); |
|
||||
userTokenResultDTO.setToken(token); |
|
||||
return userTokenResultDTO; |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* @return com.epmet.commons.tools.utils.Result |
|
||||
* @param userId |
|
||||
* @param openid |
|
||||
* @Author yinzuomei |
|
||||
* @Description 保存微信和当前登录用户关系 |
|
||||
* @Date 2020/4/18 22:54 |
|
||||
**/ |
|
||||
private Result savestaffwechat(String userId, String openid) { |
|
||||
StaffWechatFormDTO staffWechatFormDTO = new StaffWechatFormDTO(); |
|
||||
staffWechatFormDTO.setUserId(userId); |
|
||||
staffWechatFormDTO.setWxOpenId(openid); |
|
||||
return epmetUserFeignClient.savestaffwechat(staffWechatFormDTO); |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* @Description 生成token |
|
||||
* @Date 2020/4/18 23:04 |
|
||||
**/ |
|
||||
private String generateToken(LoginCommonFormDTO formDTO,String userId){ |
|
||||
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+"]"); |
|
||||
return token; |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* @Description 生成token |
|
||||
* @Date 2020/4/18 23:04 |
|
||||
**/ |
|
||||
private String saveTokenDto(LoginCommonFormDTO formDTO, |
|
||||
String userId, |
|
||||
WxMaJscode2SessionResult wxMaJscode2SessionResult, |
|
||||
String token) { |
|
||||
int expire = jwtTokenProperties.getExpire(); |
|
||||
TokenDto tokenDto = new TokenDto(); |
|
||||
tokenDto.setApp(formDTO.getApp()); |
|
||||
tokenDto.setClient(formDTO.getClient()); |
|
||||
tokenDto.setUserId(userId); |
|
||||
tokenDto.setOpenId(wxMaJscode2SessionResult.getOpenid()); |
|
||||
tokenDto.setSessionKey(wxMaJscode2SessionResult.getSessionKey()); |
|
||||
tokenDto.setUnionId(wxMaJscode2SessionResult.getUnionid()); |
|
||||
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; |
|
||||
} |
|
||||
|
|
||||
} |
|
||||
|
|
Loading…
Reference in new issue