diff --git a/epmet-auth/src/main/java/com/epmet/controller/GovLoginController.java b/epmet-auth/src/main/java/com/epmet/controller/GovLoginController.java deleted file mode 100644 index 4e16236906..0000000000 --- a/epmet-auth/src/main/java/com/epmet/controller/GovLoginController.java +++ /dev/null @@ -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 - * @Author yinzuomei - * @Description 手机验证码登录 - * @Date 2020/4/18 21:14 - **/ - @PostMapping(value = "loginbyphone") - public Result loginByPhone(LoginByPhoneFormDTO formDTO) { - ValidatorUtils.validateEntity(formDTO); - return govLoginService.loginByPhone(formDTO); - } -} diff --git a/epmet-auth/src/main/java/com/epmet/controller/LoginController.java b/epmet-auth/src/main/java/com/epmet/controller/LoginController.java index 10977f08b0..47c778977a 100644 --- a/epmet-auth/src/main/java/com/epmet/controller/LoginController.java +++ b/epmet-auth/src/main/java/com/epmet/controller/LoginController.java @@ -7,7 +7,9 @@ import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.validator.AssertUtils; import com.epmet.commons.tools.validator.ValidatorUtils; import com.epmet.dto.form.LoginByPassWordFormDTO; +import com.epmet.dto.form.LoginByPhoneFormDTO; import com.epmet.dto.form.LoginByWxCodeFormDTO; +import com.epmet.dto.form.SendSmsCodeFormDTO; import com.epmet.dto.result.UserTokenResultDTO; import com.epmet.service.CaptchaService; import com.epmet.service.LoginService; @@ -40,7 +42,7 @@ public class LoginController { * @param response * @param uuid * @Author yinzuomei - * @Description 生成验证码 + * @Description 运营端管理后台-生成验证码 * @Date 2020/3/17 16:08 **/ @GetMapping("captcha") @@ -60,10 +62,10 @@ public class LoginController { * @param formDTO * @return com.epmet.commons.tools.utils.Result * @Author yinzuomei - * @Description 微信小程序登录 + * @Description 居民端微信小程序登录 * @Date 2020/3/14 14:35 **/ - @PostMapping("loginbywxcode") + @PostMapping("/resiwxmp/loginbywxcode") public Result loginByWxCode(@RequestBody LoginByWxCodeFormDTO formDTO) { //效验数据 ValidatorUtils.validateEntity(formDTO); @@ -77,7 +79,7 @@ public class LoginController { * @Description 手机号+密码登录接口 * @Date 2020/3/14 19:46 **/ - @PostMapping("loginbypassword") + @PostMapping("/operweb/loginbypassword") public Result loginByPassword(@RequestBody LoginByPassWordFormDTO formDTO) { //效验数据 ValidatorUtils.validateEntity(formDTO); @@ -98,4 +100,29 @@ public class LoginController { return loginService.logoutByToken(tokenDto); } + /** + * @param formDTO 手机号 + * @return com.epmet.commons.tools.utils.Result + * @Author yinzuomei + * @Description 政府端微信小程序登录-发送验证码 + * @Date 2020/4/18 10:58 + **/ + @PostMapping(value = "/govwxmp/sendsmscode") + public Result sendSmsCode(@RequestBody SendSmsCodeFormDTO formDTO) { + ValidatorUtils.validateEntity(formDTO); + return loginService.sendSmsCode(formDTO); + } + + /** + * @param formDTO + * @return com.epmet.commons.tools.utils.Result + * @Author yinzuomei + * @Description 政府端微信小程序登录-手机验证码登录 + * @Date 2020/4/18 21:14 + **/ + @PostMapping(value = "/govwxmp/loginbyphone") + public Result loginByPhone(LoginByPhoneFormDTO formDTO) { + ValidatorUtils.validateEntity(formDTO); + return loginService.loginByPhone(formDTO); + } } diff --git a/epmet-auth/src/main/java/com/epmet/service/GovLoginService.java b/epmet-auth/src/main/java/com/epmet/service/GovLoginService.java deleted file mode 100644 index d8c24bf966..0000000000 --- a/epmet-auth/src/main/java/com/epmet/service/GovLoginService.java +++ /dev/null @@ -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 - * @Author yinzuomei - * @Description 手机验证码登录 - * @Date 2020/4/18 21:11 - **/ - Result loginByPhone(LoginByPhoneFormDTO formDTO); -} diff --git a/epmet-auth/src/main/java/com/epmet/service/LoginService.java b/epmet-auth/src/main/java/com/epmet/service/LoginService.java index 6beae83483..1433393413 100644 --- a/epmet-auth/src/main/java/com/epmet/service/LoginService.java +++ b/epmet-auth/src/main/java/com/epmet/service/LoginService.java @@ -4,7 +4,9 @@ import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import com.epmet.commons.tools.security.dto.TokenDto; import com.epmet.commons.tools.utils.Result; import com.epmet.dto.form.LoginByPassWordFormDTO; +import com.epmet.dto.form.LoginByPhoneFormDTO; import com.epmet.dto.form.LoginByWxCodeFormDTO; +import com.epmet.dto.form.SendSmsCodeFormDTO; import com.epmet.dto.result.UserTokenResultDTO; /** @@ -17,7 +19,7 @@ public interface LoginService { * @param formDTO * @return com.epmet.commons.tools.utils.Result * @Author yinzuomei - * @Description 微信小程序登录 + * @Description 居民端微信小程序登录 * @Date 2020/3/14 19:34 **/ Result loginByWxCode(LoginByWxCodeFormDTO formDTO); @@ -49,4 +51,22 @@ public interface LoginService { * @Date 2020/4/19 0:24 **/ WxMaJscode2SessionResult getWxMaUser(String app, String wxCode); + + /** + * @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 + * @Author yinzuomei + * @Description 政府端微信小程序登录-手机验证码登录 + * @Date 2020/4/18 21:11 + **/ + Result loginByPhone(LoginByPhoneFormDTO formDTO); } diff --git a/epmet-auth/src/main/java/com/epmet/service/impl/GovLoginServiceImpl.java b/epmet-auth/src/main/java/com/epmet/service/impl/GovLoginServiceImpl.java deleted file mode 100644 index 1b68026fa0..0000000000 --- a/epmet-auth/src/main/java/com/epmet/service/impl/GovLoginServiceImpl.java +++ /dev/null @@ -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 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> 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 - * @Author yinzuomei - * @Description 手机验证码登录 - * @Date 2020/4/18 21:11 - **/ - @Override - public Result loginByPhone(LoginByPhoneFormDTO formDTO) { - //1、根据手机号查询到用户信息 - Result 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().error(EpmetErrorCode.MOBILE_CODE_ERROR.getCode()); - } - //3、解析wxCode - UserTokenResultDTO userTokenResultDTO=this.getAuthorizationInfo(formDTO,customerStaffResult.getData()); - return new Result().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 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; - } - -} - diff --git a/epmet-auth/src/main/java/com/epmet/service/impl/LoginServiceImpl.java b/epmet-auth/src/main/java/com/epmet/service/impl/LoginServiceImpl.java index 127006790a..669c0596a5 100644 --- a/epmet-auth/src/main/java/com/epmet/service/impl/LoginServiceImpl.java +++ b/epmet-auth/src/main/java/com/epmet/service/impl/LoginServiceImpl.java @@ -3,23 +3,26 @@ package com.epmet.service.impl; import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import cn.binarywang.wx.miniapp.bean.WxMaUserInfo; import com.epmet.common.token.constant.LoginConstant; +import com.epmet.commons.tools.exception.EpmetErrorCode; import com.epmet.commons.tools.exception.ErrorCode; 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.commons.tools.validator.PhoneValidatorUtils; +import com.epmet.dto.CustomerStaffDTO; import com.epmet.dto.UserDTO; import com.epmet.dto.UserWechatDTO; -import com.epmet.dto.form.LoginByPassWordFormDTO; -import com.epmet.dto.form.LoginByWxCodeFormDTO; -import com.epmet.dto.form.PasswordLoginUserInfoFormDTO; -import com.epmet.dto.form.WxLoginUserInfoFormDTO; +import com.epmet.dto.form.*; import com.epmet.dto.result.PasswordLoginUserInfoResultDTO; 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.CaptchaService; import com.epmet.service.LoginService; import com.epmet.utils.WxMaServiceUtils; @@ -62,8 +65,14 @@ public class LoginServiceImpl implements LoginService { @Autowired private CaptchaService captchaService; + @Autowired + private CaptchaRedis captchaRedis; + + @Autowired + private MessageFeignClient messageFeignClient; + /** - * 微信小程序登录 + * 居民端微信小程序登录 * * @param formDTO * @return com.epmet.commons.tools.utils.Result @@ -72,6 +81,10 @@ public class LoginServiceImpl implements LoginService { */ @Override public Result loginByWxCode(LoginByWxCodeFormDTO formDTO) { + if(!(LoginConstant.APP_RESI.equals(formDTO.getApp())&&LoginConstant.CLIENT_WXMP.equals(formDTO.getClient()))){ + logger.error("当前接口只适用于居民端微信小程序登录"); + throw new RenException("参数错误"); + } //1、根据wxCode获取微信信息 WxMaJscode2SessionResult wxMaJscode2SessionResult = this.getWxMaUser(formDTO.getApp(),formDTO.getWxCode()); logger.info("openId=[" + wxMaJscode2SessionResult.getOpenid() + "]unionId=[" + wxMaJscode2SessionResult.getUnionid() + "]"); @@ -79,8 +92,10 @@ public class LoginServiceImpl implements LoginService { String userId = this.getUserId(formDTO, wxMaJscode2SessionResult); if (StringUtils.isNotBlank(userId)) { //3、封装token且存到redis + String token=this.generateToken(formDTO,userId); + this.saveTokenDto(formDTO,userId,wxMaJscode2SessionResult,token); UserTokenResultDTO userTokenResultDTO = new UserTokenResultDTO(); - userTokenResultDTO.setToken(this.packagingUserToken(formDTO, userId, wxMaJscode2SessionResult)); + userTokenResultDTO.setToken(token); return new Result().ok(userTokenResultDTO); }else{ logger.error("登录失败userId为空"); @@ -138,6 +153,7 @@ public class LoginServiceImpl implements LoginService { Result userResult = epmetUserFeignClient.selecWxLoginUserInfo(wxLoginUserInfoFormDTO); String userId = ""; if (!userResult.success()) { + logger.error("根据openId、app获取用户信息失败" + userResult.getMsg()); throw new RenException("获取用户信息失败" + userResult.getMsg()); } if(null!=userResult.getData()&&StringUtils.isNotBlank(userResult.getData().getId())){ @@ -225,42 +241,6 @@ public class LoginServiceImpl implements LoginService { return userWechatDTO; } - /** - * 封装用户token值 - * - * @param formDTO - * @param userId - * @param wxMaJscode2SessionResult - * @return java.lang.String - * @author yinzuomei - * @since 2020/3/14 19:34 - */ - private String packagingUserToken(LoginByWxCodeFormDTO formDTO, - String userId, - WxMaJscode2SessionResult wxMaJscode2SessionResult) { - // 生成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.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("token过期时间:"+tokenUtil.getExpire(tokenDto.getApp(),tokenDto.getClient(),tokenDto.getUserId())); -// logger.info("截止时间:"+ DateUtils.format(jwtTokenUtils.getExpiration(token),"yyyy-MM-dd HH:mm:ss")); - return token; - } /** * 手机号+密码登录接口 @@ -272,9 +252,9 @@ public class LoginServiceImpl implements LoginService { */ @Override public Result loginByPassword(LoginByPassWordFormDTO formDTO) { - if(LoginConstant.APP_RESI.equals(formDTO.getApp())){ - logger.error("居民端请使用微信登录"); - throw new RenException("居民端请使用微信登录"); + if(!(LoginConstant.APP_OPER.equals(formDTO.getApp())&&LoginConstant.CLIENT_WEB.equals(formDTO.getClient()))){ + logger.error("当前接口只适用于运营端管理后台"); + throw new RenException("当前接口只适用于运营端管理后台"); } //1、验证码是否正确 boolean flag = captchaService.validate(formDTO.getUuid(), formDTO.getCaptcha()); @@ -287,9 +267,8 @@ public class LoginServiceImpl implements LoginService { passwordLoginUserInfoFormDTO.setApp(formDTO.getApp()); passwordLoginUserInfoFormDTO.setPhone(formDTO.getPhone()); Result userInfoResult = epmetUserFeignClient.selectLoginUserInfoByPassword(passwordLoginUserInfoFormDTO); - logger.info(userInfoResult.getCode() + userInfoResult.getMsg()); if (!userInfoResult.success() || null == userInfoResult.getData()) { - logger.error("账号不存在"); + logger.error("根据手机号查询运营人员信息失败"); throw new RenException("账号不存在"); } //3、密码是否正确 @@ -320,7 +299,7 @@ public class LoginServiceImpl implements LoginService { 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+"]"); + logger.info("app:"+formDTO.getApp()+";client:"+formDTO.getClient()+";userId:"+userId+";生成token["+token+"]"); int expire = jwtTokenProperties.getExpire(); TokenDto tokenDto = new TokenDto(); tokenDto.setApp(formDTO.getApp()); @@ -330,8 +309,7 @@ public class LoginServiceImpl implements LoginService { tokenDto.setUpdateTime(System.currentTimeMillis()); tokenDto.setExpireTime(jwtTokenUtils.getExpiration(token).getTime()); cpUserDetailRedis.set(tokenDto, expire); -// logger.info("token过期时间:"+tokenUtil.getExpire(tokenDto.getApp(),tokenDto.getClient(),tokenDto.getUserId())); -// logger.info("截止时间:"+ DateUtils.format(jwtTokenUtils.getExpiration(token),"yyyy-MM-dd HH:mm:ss")); + logger.info("截止时间:"+ DateUtils.format(jwtTokenUtils.getExpiration(token),"yyyy-MM-dd HH:mm:ss")); return token; } @@ -344,10 +322,134 @@ public class LoginServiceImpl implements LoginService { logger.error("账号不存在"); throw new RenException("当前用户信息获取失败"); } - cpUserDetailRedis.logout(tokenDto.getApp() , tokenDto.getClient() , tokenDto.getUserId()); - - //web端清空菜单栏和权限,小程序目前又 + //web端清空菜单栏和权限 return new Result().ok("退出登录!"); } + + 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 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> 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 + * @Author yinzuomei + * @Description 政府端微信小程序登录-手机验证码登录 + * @Date 2020/4/18 21:11 + **/ + @Override + public Result loginByPhone(LoginByPhoneFormDTO formDTO) { + //1、根据手机号查询到用户信息 + Result 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().error(EpmetErrorCode.MOBILE_CODE_ERROR.getCode()); + } + //3、解析wxCode + UserTokenResultDTO userTokenResultDTO=this.getAuthorizationInfo(formDTO,customerStaffResult.getData()); + return new Result().ok(userTokenResultDTO); + } + + private UserTokenResultDTO getAuthorizationInfo(LoginByPhoneFormDTO formDTO, CustomerStaffDTO customerStaff) { + //1、解析微信用户 + WxMaJscode2SessionResult wxMaJscode2SessionResult=this.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 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; + } } diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/dao/UserDao.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/dao/UserDao.java index f3a02cf935..a015e34805 100644 --- a/epmet-user/epmet-user-server/src/main/java/com/epmet/dao/UserDao.java +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/dao/UserDao.java @@ -13,15 +13,6 @@ import org.apache.ibatis.annotations.Mapper; */ @Mapper public interface UserDao extends BaseDao { - /** - * @param phone - * @return com.epmet.dto.result.PasswordLoginUserInfoResultDTO - * @Author yinzuomei - * @Description 根据手机号查询政府工作人员信息 - * @Date 2020/3/17 11:01 - **/ - PasswordLoginUserInfoResultDTO selectCustomerStaffByPhone(String phone); - /** * @param phone * @return com.epmet.dto.result.PasswordLoginUserInfoResultDTO diff --git a/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/UserServiceImpl.java b/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/UserServiceImpl.java index ad18ef11d4..3d9e344e2a 100644 --- a/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/UserServiceImpl.java +++ b/epmet-user/epmet-user-server/src/main/java/com/epmet/service/impl/UserServiceImpl.java @@ -1,6 +1,5 @@ package com.epmet.service.impl; -import com.epmet.common.token.constant.LoginConstant; import com.epmet.commons.mybatis.service.impl.BaseServiceImpl; import com.epmet.commons.tools.utils.ConvertUtils; import com.epmet.commons.tools.utils.Result; @@ -85,12 +84,7 @@ public class UserServiceImpl extends BaseServiceImpl implem **/ @Override public Result selectLoginUserInfoByPassword(PasswordLoginUserInfoFormDTO formDTO) { - PasswordLoginUserInfoResultDTO passwordLoginUserInfoResultDTO = null; - if (LoginConstant.APP_GOV.equals(formDTO.getApp())) { - passwordLoginUserInfoResultDTO = baseDao.selectCustomerStaffByPhone(formDTO.getPhone()); - } else if (LoginConstant.APP_OPER.equals(formDTO.getApp())) { - passwordLoginUserInfoResultDTO = baseDao.selectOperUserByPhone(formDTO.getPhone()); - } + PasswordLoginUserInfoResultDTO passwordLoginUserInfoResultDTO = baseDao.selectOperUserByPhone(formDTO.getPhone()); return new Result().ok(passwordLoginUserInfoResultDTO); } diff --git a/epmet-user/epmet-user-server/src/main/resources/mapper/UserDao.xml b/epmet-user/epmet-user-server/src/main/resources/mapper/UserDao.xml index 6800fd862d..34b509b54d 100644 --- a/epmet-user/epmet-user-server/src/main/resources/mapper/UserDao.xml +++ b/epmet-user/epmet-user-server/src/main/resources/mapper/UserDao.xml @@ -2,19 +2,6 @@ - - - -