From 2019ceb46c57194429669a762d5e66c8ef56c23a Mon Sep 17 00:00:00 2001 From: sunyuchao Date: Tue, 28 Jul 2020 13:29:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=95=E5=AE=A2=E6=88=B7-=E5=B7=A5=E4=BD=9C?= =?UTF-8?q?=E7=AB=AF=E6=A0=B9=E6=8D=AE=E6=89=8B=E6=9C=BA=E5=8F=B7=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E7=A0=81=E6=88=96=E5=AF=86=E7=A0=81=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E7=BB=84=E7=BB=87=E6=96=B0=E5=A2=9E=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ThirdLoginController.java | 31 +++ .../epmet/dto/form/ThirdStaffOrgsFormDTO.java | 48 +++++ .../com/epmet/service/ThirdLoginService.java | 20 ++ .../service/impl/ThirdLoginServiceImpl.java | 189 +++++++++++++----- .../main/resources/mapper/CustomerMpDao.xml | 3 + .../epmet/feign/GovOrgOpenFeignClient.java | 11 + .../GovOrgOpenFeignClientFallback.java | 6 + .../dto/form/ThirdCustomerStaffFormDTO.java | 25 +++ .../epmet/feign/EpmetUserOpenFeignClient.java | 9 + .../EpmetUserOpenFeignClientFallback.java | 5 + .../controller/CustomerStaffController.java | 11 + .../java/com/epmet/dao/CustomerStaffDao.java | 8 + .../epmet/service/CustomerStaffService.java | 8 + .../impl/CustomerStaffServiceImpl.java | 20 ++ .../resources/mapper/CustomerStaffDao.xml | 12 ++ 15 files changed, 354 insertions(+), 52 deletions(-) create mode 100644 epmet-auth/src/main/java/com/epmet/dto/form/ThirdStaffOrgsFormDTO.java create mode 100644 epmet-user/epmet-user-client/src/main/java/com/epmet/dto/form/ThirdCustomerStaffFormDTO.java diff --git a/epmet-auth/src/main/java/com/epmet/controller/ThirdLoginController.java b/epmet-auth/src/main/java/com/epmet/controller/ThirdLoginController.java index 461dd1ab48..924c1a4070 100644 --- a/epmet-auth/src/main/java/com/epmet/controller/ThirdLoginController.java +++ b/epmet-auth/src/main/java/com/epmet/controller/ThirdLoginController.java @@ -3,7 +3,10 @@ package com.epmet.controller; import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.validator.ValidatorUtils; import com.epmet.dto.form.LoginFormDTO; +import com.epmet.dto.form.StaffOrgsFormDTO; +import com.epmet.dto.form.ThirdStaffOrgsFormDTO; import com.epmet.dto.form.ThirdWxmpEnteOrgFormDTO; +import com.epmet.dto.result.StaffOrgsResultDTO; import com.epmet.dto.result.UserTokenResultDTO; import com.epmet.service.ThirdLoginService; import org.springframework.beans.factory.annotation.Autowired; @@ -12,6 +15,8 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.List; + /** * @Description 第三方-居民端、政府端登陆服务 * @author sun @@ -60,4 +65,30 @@ public class ThirdLoginController { return new Result().ok(userTokenResultDTO); } + /** + * @param formDTO + * @return + * @Author sun + * @Description 单客户-手机验证码获取组织 + **/ + @PostMapping(value = "/getmyorg") + public Result> getmyorg(@RequestBody ThirdStaffOrgsFormDTO formDTO) { + ValidatorUtils.validateEntity(formDTO, StaffOrgsFormDTO.AddUserShowGroup.class, StaffOrgsFormDTO.GetMyOrgByLoginWxmp.class); + List staffOrgs = thirdLoginService.getMyOrg(formDTO); + return new Result>().ok(staffOrgs); + } + + /** + * @param formDTO + * @return + * @author sun + * @description 单客户-手机号密码获取组织 + **/ + @PostMapping(value = "/getmyorgbypassword") + public Result> getMyOrgByPassword(@RequestBody ThirdStaffOrgsFormDTO formDTO) { + ValidatorUtils.validateEntity(formDTO, StaffOrgsFormDTO.AddUserShowGroup.class, StaffOrgsFormDTO.GetMyOrgByPassWordGroup.class); + List staffOrgs = thirdLoginService.getMyOrgByPassword(formDTO); + return new Result>().ok(staffOrgs); + } + } diff --git a/epmet-auth/src/main/java/com/epmet/dto/form/ThirdStaffOrgsFormDTO.java b/epmet-auth/src/main/java/com/epmet/dto/form/ThirdStaffOrgsFormDTO.java new file mode 100644 index 0000000000..c5027dcd82 --- /dev/null +++ b/epmet-auth/src/main/java/com/epmet/dto/form/ThirdStaffOrgsFormDTO.java @@ -0,0 +1,48 @@ +package com.epmet.dto.form; + +import com.epmet.commons.tools.validator.group.CustomerClientShowGroup; +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import java.io.Serializable; + +/** + * @Description appId、手机号、验证码获取组织-接口入参 + * @Author sun + */ +@Data +public class ThirdStaffOrgsFormDTO implements Serializable { + private static final long serialVersionUID = 4193133227120225342L; + /** + * 添加用户操作的用户可见异常分组 + * 该分组用于校验需要返回给前端错误信息提示的列,需要继承CustomerClientShowGroup + * 返回错误码为8999,提示信息为DTO中具体的列的校验注解message的内容 + */ + public interface AddUserShowGroup extends CustomerClientShowGroup { + } + + public interface GetMyOrgByPassWordGroup extends CustomerClientShowGroup { + } + public interface GetMyOrgByLoginWxmp extends CustomerClientShowGroup{} + /** + * 小程序appId + */ + @NotBlank(message = "appId不能为空", groups = {AddUserShowGroup.class}) + private String appId; + + /** + * 手机号 + */ + @NotBlank(message = "手机号不能为空", groups = {AddUserShowGroup.class}) + private String mobile; + + /** + * 验证码 + */ + @NotBlank(message="验证码不能为空", groups = {GetMyOrgByLoginWxmp.class}) + private String smsCode; + + @NotBlank(message = "密码不能为空",groups ={GetMyOrgByPassWordGroup.class}) + private String password; +} + diff --git a/epmet-auth/src/main/java/com/epmet/service/ThirdLoginService.java b/epmet-auth/src/main/java/com/epmet/service/ThirdLoginService.java index a532adec56..b343bf5a22 100644 --- a/epmet-auth/src/main/java/com/epmet/service/ThirdLoginService.java +++ b/epmet-auth/src/main/java/com/epmet/service/ThirdLoginService.java @@ -1,9 +1,13 @@ package com.epmet.service; import com.epmet.dto.form.LoginFormDTO; +import com.epmet.dto.form.ThirdStaffOrgsFormDTO; import com.epmet.dto.form.ThirdWxmpEnteOrgFormDTO; +import com.epmet.dto.result.StaffOrgsResultDTO; import com.epmet.dto.result.UserTokenResultDTO; +import java.util.List; + /** * @Description 第三方-居民端、政府端登陆服务 * @author sun @@ -33,4 +37,20 @@ public interface ThirdLoginService { * @Description 单客户-选择组织,进入首页 **/ UserTokenResultDTO enterOrg(ThirdWxmpEnteOrgFormDTO formDTO); + + /** + * @param formDTO + * @return + * @Author sun + * @Description 单客户-手机验证码获取组织 + **/ + List getMyOrg(ThirdStaffOrgsFormDTO formDTO); + + /** + * @param formDTO + * @return + * @author sun + * @description 单客户-手机号密码获取组织 + **/ + List getMyOrgByPassword(ThirdStaffOrgsFormDTO formDTO); } diff --git a/epmet-auth/src/main/java/com/epmet/service/impl/ThirdLoginServiceImpl.java b/epmet-auth/src/main/java/com/epmet/service/impl/ThirdLoginServiceImpl.java index df85d0ccd1..2bb6c09a7e 100644 --- a/epmet-auth/src/main/java/com/epmet/service/impl/ThirdLoginServiceImpl.java +++ b/epmet-auth/src/main/java/com/epmet/service/impl/ThirdLoginServiceImpl.java @@ -2,38 +2,36 @@ package com.epmet.service.impl; import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import com.epmet.common.token.constant.LoginConstant; +import com.epmet.commons.tools.constant.ServiceConstant; import com.epmet.commons.tools.exception.EpmetErrorCode; import com.epmet.commons.tools.exception.ExceptionUtils; import com.epmet.commons.tools.exception.RenException; import com.epmet.commons.tools.security.dto.GovTokenDto; import com.epmet.commons.tools.security.dto.TokenDto; +import com.epmet.commons.tools.security.password.PasswordUtils; import com.epmet.commons.tools.utils.ConvertUtils; 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.*; import com.epmet.dto.form.*; -import com.epmet.dto.result.DepartmentListResultDTO; -import com.epmet.dto.result.GridByStaffResultDTO; -import com.epmet.dto.result.StaffLatestAgencyResultDTO; -import com.epmet.dto.result.UserTokenResultDTO; +import com.epmet.dto.result.*; import com.epmet.feign.EpmetThirdFeignClient; import com.epmet.feign.EpmetUserOpenFeignClient; import com.epmet.feign.GovOrgOpenFeignClient; import com.epmet.jwt.JwtTokenProperties; import com.epmet.jwt.JwtTokenUtils; +import com.epmet.redis.CaptchaRedis; import com.epmet.service.ThirdLoginService; import lombok.extern.slf4j.Slf4j; +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 org.springframework.util.CollectionUtils; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; /** @@ -48,6 +46,8 @@ public class ThirdLoginServiceImpl implements ThirdLoginService { @Autowired private JwtTokenUtils jwtTokenUtils; @Autowired + private CaptchaRedis captchaRedis; + @Autowired private JwtTokenProperties jwtTokenProperties; @Autowired private CpUserDetailRedis cpUserDetailRedis; @@ -142,6 +142,7 @@ public class ThirdLoginServiceImpl implements ThirdLoginService { **/ @Override public UserTokenResultDTO workLogin(LoginFormDTO formDTO) { + //1.调用epmet_third服务,校验appId是否有效以及是否授权,校验通过的调用微信API获取用户基本信息 WxLoginFormDTO resiLoginFormDTO = new WxLoginFormDTO(); resiLoginFormDTO.setAppId(formDTO.getAppId()); @@ -379,66 +380,150 @@ public class ThirdLoginServiceImpl implements ThirdLoginService { wxMaJscode2SessionResult.setSessionKey(userWechatDTO.getSessionKey()); wxMaJscode2SessionResult.setUnionid(""); // end - //3、记录staff_wechat,并记录用户激活状态,激活时间 - this.savestaffwechat(customerStaff.getUserId(), wxMaJscode2SessionResult.getOpenid()); + this.savestaffwechat(customerStaff.getUserId(), userWechatDTO.getWxOpenId()); //4、记录登录日志 - this.saveGovStaffLoginRecord(formDTO, customerStaff.getUserId(), wxMaJscode2SessionResult.getOpenid()); + StaffLatestAgencyResultDTO staffLatestAgencyResultDTO = new StaffLatestAgencyResultDTO(); + staffLatestAgencyResultDTO.setCustomerId(formDTO.getCustomerId()); + staffLatestAgencyResultDTO.setStaffId(customerStaff.getUserId()); + staffLatestAgencyResultDTO.setWxOpenId(userWechatDTO.getWxOpenId()); + staffLatestAgencyResultDTO.setMobile(formDTO.getMobile()); + staffLatestAgencyResultDTO.setAgencyId(formDTO.getRootAgencyId()); + this.saveStaffLoginRecord(staffLatestAgencyResultDTO); //5.1、获取用户token String token = this.generateGovWxmpToken(customerStaff.getUserId()); //5.2、保存到redis - this.saveGovTokenDto(formDTO.getRootAgencyId(), formDTO.getCustomerId(), customerStaff.getUserId(), wxMaJscode2SessionResult, token); + StaffLatestAgencyResultDTO staffLatestAgency = new StaffLatestAgencyResultDTO(); + staffLatestAgency.setAgencyId(formDTO.getRootAgencyId()); + staffLatestAgency.setCustomerId(formDTO.getCustomerId()); + staffLatestAgency.setStaffId(customerStaff.getUserId()); + this.saveLatestGovTokenDto(staffLatestAgency, userWechatDTO, token); + UserTokenResultDTO userTokenResultDTO = new UserTokenResultDTO(); userTokenResultDTO.setToken(token); return userTokenResultDTO; } - //保存登录日志 - private Result saveGovStaffLoginRecord(ThirdWxmpEnteOrgFormDTO formDTO, String staffId, String openId) { - StaffLoginAgencyRecordFormDTO staffLoginAgencyRecordFormDTO = new StaffLoginAgencyRecordFormDTO(); - staffLoginAgencyRecordFormDTO.setCustomerId(formDTO.getCustomerId()); - staffLoginAgencyRecordFormDTO.setStaffId(staffId); - staffLoginAgencyRecordFormDTO.setWxOpenId(openId); - staffLoginAgencyRecordFormDTO.setMobile(formDTO.getMobile()); - staffLoginAgencyRecordFormDTO.setAgencyId(formDTO.getRootAgencyId()); - Result staffLoginRecordResult = epmetUserOpenFeignClient.saveStaffLoginRecord(staffLoginAgencyRecordFormDTO); - return staffLoginRecordResult; + /** + * @param formDTO + * @return + * @Author sun + * @Description 单客户-手机验证码获取组织 + **/ + @Override + public List getMyOrg(ThirdStaffOrgsFormDTO formDTO) { + //0、验证码是否正确 + String rightSmsCode = captchaRedis.getSmsCode(formDTO.getMobile()); + 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()); + } + //1.根据appId查询对应客户Id + Result resultDTO = epmetThirdFeignClient.getCustomerMsg(formDTO.getAppId()); + if (!resultDTO.success()) { + logger.error(String.format("根据appId查询客户Id失败,对应appId->" + formDTO.getAppId())); + throw new RenException(resultDTO.getMsg()); + } + PaCustomerDTO customer = resultDTO.getData().getCustomer(); + + //7.28 根据appId只能存在一个客户Id,后边的批量操作逻辑 + //2.根据手机号查询到用户信息 + ThirdCustomerStaffFormDTO dto = new ThirdCustomerStaffFormDTO(); + dto.setCustomerId(customer.getId()); + dto.setMobile(formDTO.getMobile()); + Result> customerStaffResult = epmetUserOpenFeignClient.getCustsomerStaffByIdAndPhone(dto); + if (!customerStaffResult.success()) { + logger.error(String.format("手机验证码登录异常,手机号[%s],code[%s],msg[%s]", formDTO.getMobile(), customerStaffResult.getCode(), customerStaffResult.getMsg())); + throw new RenException(customerStaffResult.getCode()); + } + + //3、查询用户所有的组织信息 + List customerIdList = new ArrayList<>(); + for (CustomerStaffDTO customerStaffDTO : customerStaffResult.getData()) { + customerIdList.add(customerStaffDTO.getCustomerId()); + } + StaffOrgFormDTO staffOrgFormDTO = new StaffOrgFormDTO(); + staffOrgFormDTO.setCustomerIdList(customerIdList); + Result> result = govOrgOpenFeignClient.getStaffOrgList(staffOrgFormDTO); + if(result.success()&&null!=result.getData()){ + return result.getData(); + } + logger.error(String .format("手机验证码获取组织,调用%s服务失败,入参手机号%s,验证码%s,返回错误码%s,错误提示信息%s", ServiceConstant.GOV_ORG_SERVER,formDTO.getMobile(),formDTO.getSmsCode(),result.getCode(),result.getMsg())); + return new ArrayList<>(); } /** - * @Description 生成token - * @Date 2020/4/18 23:04 + * @param formDTO + * @return + * @author sun + * @description 单客户-手机号密码获取组织 **/ - private void saveGovTokenDto(String orgId, - String customerId, - String staffId, - WxMaJscode2SessionResult wxMaJscode2SessionResult, - String token) { - int expire = jwtTokenProperties.getExpire(); - GovTokenDto govTokenDto = new GovTokenDto(); - govTokenDto.setApp(LoginConstant.APP_GOV); - govTokenDto.setClient(LoginConstant.CLIENT_WXMP); - govTokenDto.setUserId(staffId); - govTokenDto.setOpenId(wxMaJscode2SessionResult.getOpenid()); - govTokenDto.setSessionKey(wxMaJscode2SessionResult.getSessionKey()); - govTokenDto.setUnionId(null == wxMaJscode2SessionResult.getUnionid() ? "" : wxMaJscode2SessionResult.getUnionid()); - govTokenDto.setToken(token); - govTokenDto.setUpdateTime(System.currentTimeMillis()); - govTokenDto.setExpireTime(jwtTokenUtils.getExpiration(token).getTime()); - govTokenDto.setRootAgencyId(orgId); - govTokenDto.setCustomerId(customerId); + @Override + public List getMyOrgByPassword(ThirdStaffOrgsFormDTO formDTO) { + //0.根据appId查询对应客户Id + Result resultDTO = epmetThirdFeignClient.getCustomerMsg(formDTO.getAppId()); + if (!resultDTO.success()) { + logger.error(String.format("根据appId查询客户Id失败,对应appId->" + formDTO.getAppId())); + throw new RenException(resultDTO.getMsg()); + } + PaCustomerDTO customer = resultDTO.getData().getCustomer(); + //7.28 上边根据appId只能锁定一条客户id,后边的批量循环操作暂不做调整,还是使用之前的代码 sun + //1、根据手机号查询到用户信息 + ThirdCustomerStaffFormDTO dto = new ThirdCustomerStaffFormDTO(); + dto.setCustomerId(customer.getId()); + dto.setMobile(formDTO.getMobile()); + Result> customerStaffResult = epmetUserOpenFeignClient.getCustsomerStaffByIdAndPhone(dto); + if (!customerStaffResult.success()) { + logger.error(String.format("手机密码登录异常,手机号[%s],code[%s],msg[%s]", formDTO.getMobile(), customerStaffResult.getCode(), customerStaffResult.getMsg())); + throw new RenException(customerStaffResult.getCode()); + } + //2、密码是否正确 + List customerStaffList=customerStaffResult.getData(); + //3、查询用户所有的组织信息 + List customerIdList = new ArrayList<>(); + //是否设置过密码 + boolean havePasswordFlag=false; + //密码是否正确 + boolean passwordRightFlag=false; + for (CustomerStaffDTO customerStaffDTO : customerStaffList) { + if(StringUtils.isNotBlank(customerStaffDTO.getPassword())){ + havePasswordFlag=true; + }else{ + logger.warn(String.format("当前用户:手机号%s,客户Id%s下未设置密码.",formDTO.getMobile(),customerStaffDTO.getCustomerId())); + continue; + } + if (!PasswordUtils.matches(formDTO.getPassword(), customerStaffDTO.getPassword())) { + logger.warn(String.format("当前用户:手机号%s,客户Id%s密码匹配错误.",formDTO.getMobile(),customerStaffDTO.getCustomerId())); - //设置部门,网格,角色列表 - govTokenDto.setDeptIdList(getDeptartmentIdList(staffId)); - govTokenDto.setGridIdList(getGridIdList(staffId)); - CustomerAgencyDTO agency = getAgencyByStaffId(staffId); - if (agency != null) { - govTokenDto.setAgencyId(agency.getId()); - govTokenDto.setRoleList(queryGovStaffRoles(staffId, agency.getId())); + }else{ + logger.warn(String.format("当前用户:手机号%s,客户Id%s密码匹配正确.",formDTO.getMobile(),customerStaffDTO.getCustomerId())); + passwordRightFlag=true; + customerIdList.add(customerStaffDTO.getCustomerId()); + } } - govTokenDto.setOrgIdPath(getOrgIdPath(staffId)); - cpUserDetailRedis.set(govTokenDto, expire); - logger.info("截止时间:" + DateUtils.format(jwtTokenUtils.getExpiration(token), "yyyy-MM-dd HH:mm:ss")); + //根据手机号查出来所有用户,密码都为空,表明用户未激活账户,未设置密码 + if(!havePasswordFlag){ + logger.error(String.format("当前手机号(%s)下所有账户都未设置密码,请先使用验证码登录激活账户",formDTO.getMobile())); + throw new RenException(EpmetErrorCode.PASSWORD_ERROR.getCode()); + } + //密码错误 + if(!passwordRightFlag){ + logger.error(String.format("根据当前手机号(%s)密码未找到所属组织,密码错误",formDTO.getMobile())); + throw new RenException(EpmetErrorCode.PASSWORD_ERROR.getCode()); + } + StaffOrgFormDTO staffOrgFormDTO = new StaffOrgFormDTO(); + staffOrgFormDTO.setCustomerIdList(customerIdList); + Result> result = govOrgOpenFeignClient.getStaffOrgList(staffOrgFormDTO); + if(result.success()&&null!=result.getData()){ + return result.getData(); + } + logger.error(String .format("手机验证码获取组织,调用%s服务失败,入参手机号%s,密码%s,返回错误码%s,错误提示信息%s", + ServiceConstant.GOV_ORG_SERVER, + formDTO.getMobile(), + formDTO.getPassword(), + result.getCode(), + result.getMsg())); + return new ArrayList<>(); } } diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/CustomerMpDao.xml b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/CustomerMpDao.xml index 2024c79005..20632a7f3c 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/CustomerMpDao.xml +++ b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/CustomerMpDao.xml @@ -96,6 +96,9 @@ WHERE del_flag = '0' AND app_id = #{appId} + AND AUTHORIZATION_FLAG = '1' + ORDER BY CREATED_TIME DESC + LIMIT 1 + + \ No newline at end of file