From d0792ac9952e9287657873b5b7d8ddb3206ca174 Mon Sep 17 00:00:00 2001 From: zxc <954985706@qq.com> Date: Fri, 24 Jul 2020 13:22:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E7=AC=AC=E4=B8=89=E6=96=B9?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0-=E6=8E=88=E6=9D=83=E5=9B=9E=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/form/AuthResultRecordFormDTO.java | 86 ++++++++++++++ .../com/epmet/constant/ModuleConstant.java | 3 + .../constant/ThirdRunTimeInfoConstant.java | 2 + .../com/epmet/dao/AuthResultRecordDao.java | 8 ++ .../java/com/epmet/dao/CustomerMpDao.java | 8 ++ .../ComponentVerifyTicketServiceImpl.java | 110 +++++++++++++----- .../resources/mapper/AuthResultRecordDao.xml | 21 ++++ .../main/resources/mapper/CustomerMpDao.xml | 12 ++ 8 files changed, 218 insertions(+), 32 deletions(-) create mode 100644 epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/AuthResultRecordFormDTO.java diff --git a/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/AuthResultRecordFormDTO.java b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/AuthResultRecordFormDTO.java new file mode 100644 index 0000000000..6223eafc66 --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/AuthResultRecordFormDTO.java @@ -0,0 +1,86 @@ +package com.epmet.dto.form; + +import lombok.Data; + +import java.io.Serializable; +import java.util.Date; + +/** + * @Author zxc + * @CreateTime 2020/7/24 9:22 + */ +@Data +public class AuthResultRecordFormDTO implements Serializable { + + private static final long serialVersionUID = -5359209459022630868L; + + /** + * ID + */ + private String id; + + /** + * 客户ID + */ + private String customerId; + + /** + * resi:居民端,work:工作端 + */ + private String clientType; + + /** + * 第三方平台AppId 第三方平台AppId + */ + private String componentAppId; + + /** + * 微信返回创建时间 微信返回创建时间 + */ + private Date wechatCreateTime; + + /** + * 通知类型 + */ + private String InfoType; + + /** + * 授权方AppId + */ + private String AuthorizerAppid; + + /** + * 授权码(auth_code) + */ + private String AuthorizationCode; + + /** + * 授权码过期时间 + */ + private Date ExpiredTime; + + /** + * 预授权码 + */ + private String PreAuthCode; + + /** + * 删除状态 + */ + private Integer delFlag = 0; + + /** + * 乐观锁 + */ + private Integer revision = 0; + + /** + * 创建人 + */ + private String createdBy = "APP_USER"; + + /** + * 更新人 + */ + private String updatedBy = "APP_USER"; +} diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/constant/ModuleConstant.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/constant/ModuleConstant.java index c157efc7ce..d787e30ed1 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/constant/ModuleConstant.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/constant/ModuleConstant.java @@ -22,9 +22,12 @@ public interface ModuleConstant { String AUTHORIZATION_CODE_HUMP = "AuthorizationCode"; String UNAUTHORIZED = "unauthorized"; String AUTHORIZED = "authorized"; + String UPDATE_AUTHORIZED = "updateauthorized"; String NULL_CHAR = ""; String SUCCESS = "success"; String AUTHORIZER_APP_ID_HUMP = "AuthorizerAppid"; + String PRE_AUTH_CODE_HUMP = "PreAuthCode"; + String AUTHORIZATION_CODE_EXPIRED_TIME = "AuthorizationCodeExpiredTime"; // 获取 component_access_token 如下 String COMPONENT_APPID = "component_appid"; diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/constant/ThirdRunTimeInfoConstant.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/constant/ThirdRunTimeInfoConstant.java index 2e4e5e7083..92f372b4fd 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/constant/ThirdRunTimeInfoConstant.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/constant/ThirdRunTimeInfoConstant.java @@ -82,4 +82,6 @@ public interface ThirdRunTimeInfoConstant { String VERIFY_TICKET = "msgSignature = %s, timeStamp = %s, nonce = %s, encryptType = %s, signature = %s"; + String REFRESH_AUTH_ACCESS_TOKEN = "查询到新的 授权方【access_token】"; + } diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/AuthResultRecordDao.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/AuthResultRecordDao.java index ca98874003..42bb397220 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/AuthResultRecordDao.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/AuthResultRecordDao.java @@ -18,6 +18,7 @@ package com.epmet.dao; import com.epmet.commons.mybatis.dao.BaseDao; +import com.epmet.dto.form.AuthResultRecordFormDTO; import com.epmet.entity.AuthResultRecordEntity; import org.apache.ibatis.annotations.Mapper; @@ -29,5 +30,12 @@ import org.apache.ibatis.annotations.Mapper; */ @Mapper public interface AuthResultRecordDao extends BaseDao { + + /** + * @Description 插入授权结果记录 + * @param formDTO + * @author zxc + */ + void insertAuthResultRecord(AuthResultRecordFormDTO formDTO); } \ No newline at end of file diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/CustomerMpDao.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/CustomerMpDao.java index b6c9376f6d..ae1fb6ebf7 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/CustomerMpDao.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/CustomerMpDao.java @@ -107,4 +107,12 @@ public interface CustomerMpDao extends BaseDao { * @Description 根据appId查询客户信息 **/ PaCustomerDTO selectCustomerByAppId(@Param("appId") String appId); + + /** + * @Description 校验此小程序是不是绑定别的客户端 + * @param authAppId + * @param clientType + * @author zxc + */ + Integer checkBind(String authAppId,String clientType); } \ No newline at end of file diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/ComponentVerifyTicketServiceImpl.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/ComponentVerifyTicketServiceImpl.java index 6fbac46cd3..1b1617d636 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/ComponentVerifyTicketServiceImpl.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/ComponentVerifyTicketServiceImpl.java @@ -1,7 +1,9 @@ package com.epmet.service.impl; +import cn.hutool.core.convert.Convert; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.TypeReference; import com.alibaba.nacos.client.config.utils.IOUtils; import com.epmet.commons.tools.constant.NumConstant; import com.epmet.commons.tools.exception.RenException; @@ -77,6 +79,8 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe private BusinessInfoDao businessInfoDao; @Autowired private EpmetUserOpenFeignClient epmetUserOpenFeignClient; + @Autowired + private AuthResultRecordDao authResultRecordDao; @Value("${third.platform.appId}") private String componentAppId; @@ -136,16 +140,16 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe log.info(ModuleConstant.TICKET_UNDERLINE_KEY+":"+ComponentVerifyTicket); break; case ModuleConstant.AUTHORIZED: //授权成功 - // 更改customer_mp 授权信息,appId,并绑定 - String authCode = result.get(ModuleConstant.AUTHORIZATION_CODE_HUMP); - String authAppId = result.get(ModuleConstant.AUTHORIZER_APP_ID_HUMP); - log.info("=============================="+authCode); - + this.disposeAuthResult(result); break; case ModuleConstant.UNAUTHORIZED://用户取消授权 //todo 取消授权 String authorizerAppId = result.get(ModuleConstant.AUTHORIZER_APP_ID_HUMP); customerMpDao.updateAuthorizationFlag(authorizerAppId); + this.disposeAuthResult(result); + break; + case ModuleConstant.UPDATE_AUTHORIZED://授权变更 + this.disposeAuthResult(result); break; } } catch (Exception e) { @@ -161,6 +165,24 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe return ModuleConstant.SUCCESS; } + /** + * @Description 授权结果插入 + * @param result + * @author zxc + */ + @Transactional(rollbackFor = Exception.class) + public void disposeAuthResult(Map result){ + Map tempMap = result; + Map data = tempMap; + AuthResultRecordFormDTO authResultRecord = mapToEntity(data, AuthResultRecordFormDTO.class); + authResultRecord.setWechatCreateTime(this.sToDate(result.get(ModuleConstant.CREATE_TIME))); + if (result.containsKey(ModuleConstant.AUTHORIZATION_CODE_EXPIRED_TIME)) { + authResultRecord.setExpiredTime(this.sToDate(result.get(ModuleConstant.AUTHORIZATION_CODE_EXPIRED_TIME))); + } + authResultRecord.setComponentAppId(componentAppId); + authResultRecordDao.insertAuthResultRecord(authResultRecord); + } + /** * @Description 定时获取 (令牌,component_access_token) 第三方与微信交互使用的component_access_token * 每十分钟执行一次,判断是否有马上超时的(15分钟以内算马上超时) @@ -329,28 +351,31 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe String componentAccessToken = redisThird.getComponentAccessToken(); String data = HttpClientManager.getInstance().sendPostByJSON(WxMaCodeConstant.API_AUTHORIZER_TOKEN_URL + componentAccessToken, JSON.toJSONString(jsonObject)).getData(); Map map = JSON.parseObject(data, HashMap.class); - //authorizer_access_token - String authorizerAccessToken = map.get(ModuleConstant.AUTHORIZER_ACCESS_TOKEN).toString(); - String expiresIn = map.get(ModuleConstant.EXPIRES_IN).toString(); - String authorizerRefreshToken = map.get(ModuleConstant.AUTHORIZER_REFRESH_TOKEN).toString(); - Date expiresInTime = this.countExpirationTime(expiresIn); - //更新DB - AuthorizerAccessTokenFormDTO formDTO = new AuthorizerAccessTokenFormDTO(); - AuthorizationInfoFormDTO authorizationInfo = new AuthorizationInfoFormDTO(); - BeanUtils.copyProperties(formDTO,authorizationInfo); - authorizationInfo.setAuthorizerAppid(authAppId); - formDTO.setAuthorizerAccessToken(authorizerAccessToken); - formDTO.setAuthorizerRefreshToken(authorizerRefreshToken); - formDTO.setExpiresInTime(expiresInTime); - formDTO.setCustomerId(customerId); - formDTO.setAuthAppid(authAppId); - formDTO.setClientType(clientType); - //先逻辑删除,在插入 - authorizationInfoDao.updateOldAuthorizerAccessToken(customerId,clientType); - authorizationInfoDao.insertAuthorizerAccessToken(formDTO); + if (!map.containsKey(ModuleConstant.ERR_CODE)) { + log.info(REFRESH_AUTH_ACCESS_TOKEN); + //authorizer_access_token + String authorizerAccessToken = map.get(ModuleConstant.AUTHORIZER_ACCESS_TOKEN).toString(); + String expiresIn = map.get(ModuleConstant.EXPIRES_IN).toString(); + String authorizerRefreshToken = map.get(ModuleConstant.AUTHORIZER_REFRESH_TOKEN).toString(); + Date expiresInTime = this.countExpirationTime(expiresIn); + //更新DB + AuthorizerAccessTokenFormDTO formDTO = new AuthorizerAccessTokenFormDTO(); + AuthorizationInfoFormDTO authorizationInfo = new AuthorizationInfoFormDTO(); + BeanUtils.copyProperties(formDTO, authorizationInfo); + authorizationInfo.setAuthorizerAppid(authAppId); + formDTO.setAuthorizerAccessToken(authorizerAccessToken); + formDTO.setAuthorizerRefreshToken(authorizerRefreshToken); + formDTO.setExpiresInTime(expiresInTime); + formDTO.setCustomerId(customerId); + formDTO.setAuthAppid(authAppId); + formDTO.setClientType(clientType); + //先逻辑删除,在插入 + authorizationInfoDao.updateOldAuthorizerAccessToken(customerId, clientType); + authorizationInfoDao.insertAuthorizerAccessToken(formDTO); - //缓存 refreshAuthorizerAccessToken - redisThird.setAuthorizerRefreshToken(authorizationInfo); + //缓存 refreshAuthorizerAccessToken + redisThird.setAuthorizerRefreshToken(authorizationInfo); + } }); log.info("更新authorizer_access_token成功"); } @@ -388,6 +413,7 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe redisThird.setAuthCode(formDTO); AuthorizationInfoResultDTO authorizationInfoResultDTO = this.authInfoByAuthCode(authCode, customerId, clientType); String authAppId = authorizationInfoResultDTO.getAuthorizer_appid(); + // todo ================================================ AuthCodeResultDTO authCodeResultDTO = authorizationInfoDao.selectCustomerIdByAuthAppId(authAppId); this.createAndBindOpenAccount(customerId,authAppId,clientType); this.updateCustomerMpAppId(customerId,authAppId,clientType); @@ -499,12 +525,15 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe @Transactional(rollbackFor = Exception.class) public void updateCustomerMpAppId(String customerId,String authAppId,String clientType){ log.info("==========回填customer_mp开始=========="); - AuthCodeFormDTO formDTO = new AuthCodeFormDTO(); - formDTO.setClientType(clientType); - formDTO.setAuthAppId(authAppId); - formDTO.setCustomerId(customerId); - //回填customer_mp的appId 只需以上三个字段 - customerMpDao.updateAppIDByCustomerIdAndClient(formDTO); + Integer checkBindCount = customerMpDao.checkBind(authAppId, clientType); + if (checkBindCount == NumConstant.ZERO) { + AuthCodeFormDTO formDTO = new AuthCodeFormDTO(); + formDTO.setClientType(clientType); + formDTO.setAuthAppId(authAppId); + formDTO.setCustomerId(customerId); + //回填customer_mp的appId 只需以上三个字段 + customerMpDao.updateAppIDByCustomerIdAndClient(formDTO); + } log.info("==========回填customer_mp结束=========="); } @@ -635,10 +664,27 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe return date; } + /** + * @Description 查询客户Id + * @param tokenDto + * @author zxc + */ public String getLoginUserCustomerId(TokenDto tokenDto){ LoginUserDetailsFormDTO dto = new LoginUserDetailsFormDTO(); BeanUtils.copyProperties(tokenDto,dto); LoginUserDetailsResultDTO data = epmetUserOpenFeignClient.getLoginUserDetails(dto).getData(); return data.getCustomerId(); } + + /** + * @Description 时间戳(秒级)转换 Date + * @param t + * @author zxc + */ + public Date sToDate(String t){ + Long aLong = Long.valueOf(t + "000"); + Date date = new Date(); + date.setTime(aLong); + return date; + } } diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/AuthResultRecordDao.xml b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/AuthResultRecordDao.xml index d0c2ba2654..9cc47df530 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/AuthResultRecordDao.xml +++ b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/AuthResultRecordDao.xml @@ -3,4 +3,25 @@ + + + INSERT INTO auth_result_record ( ID, COMPONENT_APP_ID, WECHAT_CREATE_TIME, INFO_TYPE, AUTHORIZER_APP_ID, AUTHORIZATION_CODE, EXPIRES_IN_TIME, PRE_AUTH_CODE, DEL_FLAG, REVISION, CREATED_BY, CREATED_TIME, UPDATED_BY, UPDATED_TIME ) + VALUES + ( + REPLACE(UUID(),'-',''), + #{componentAppId}, + #{wechatCreateTime}, + #{InfoType}, + #{AuthorizerAppid}, + #{AuthorizationCode}, + #{ExpiredTime}, + #{PreAuthCode}, + #{delFlag}, + #{revision}, + #{createdBy}, + NOW(), + #{updatedBy}, + NOW() + ) + \ No newline at end of file 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 bebcb23886..f4e6b6403a 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 @@ -115,4 +115,16 @@ AND cm.app_id = #{appId} + + + \ No newline at end of file