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 593194e2c7..b6c9376f6d 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 @@ -58,7 +58,7 @@ public interface CustomerMpDao extends BaseDao { * @param customerId * @author zxc */ - Integer selectAuthCount(@Param("customerId")String customerId); + List selectAuthCount(@Param("customerId")String customerId); /** * @Description 回填customer_mp的appId 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 2204c77aa4..43e251dd5e 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 @@ -143,7 +143,8 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe AuthCodeResultDTO authCodeResultDTO = authorizationInfoDao.selectCustomerIdByAuthAppId(authAppId); String clientType = authCodeResultDTO.getClientType(); String customerId = authCodeResultDTO.getCustomerId(); - this.updateCustomerMpAppIdAndCreateOpenPlatform(customerId,authAppId,clientType); + this.createAndBindOpenAccount(customerId,authAppId,clientType); + this.updateCustomerMpAppId(customerId,authAppId,clientType); authCodeDao.updateAppId(customerId,clientType,authAppId); this.saveAuthAccountInfo(customerId,authAppId,clientType); break; @@ -495,7 +496,7 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe * @author zxc */ @Transactional(rollbackFor = Exception.class) - public void updateCustomerMpAppIdAndCreateOpenPlatform(String customerId,String authAppId,String clientType){ + public void updateCustomerMpAppId(String customerId,String authAppId,String clientType){ log.info("==========回填customer_mp开始=========="); AuthCodeFormDTO formDTO = new AuthCodeFormDTO(); formDTO.setClientType(clientType); @@ -506,6 +507,82 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe log.info("==========回填customer_mp结束=========="); } + public void createAndBindOpenAccount(String customerId,String authAppId,String clientType){ + log.info("开始创建开放平台账号并绑定"); + List authCount = customerMpDao.selectAuthCount(customerId); + String openPlatformId = null; + if (authCount.size()>0){ + openPlatformId = openPlatformAccountDao.selectOpenAppIdByCustomerId(customerId); + } + Map authorizerRefreshToken = redisThird.getAuthorizerRefreshToken(customerId + ThirdRedisKeyConstant.COLON + clientType); + String authorizerAccessToken = authorizerRefreshToken.get("authorizerAccessToken").toString(); + if (authCount.size()==NumConstant.ZERO){ + log.info("未查询到该客户授权信息,先创建开放平台账号,再绑定"); + //没有任何一个小程序/公众号授权,【先创建,再绑定】 + JSONObject jsonObject = new JSONObject(); + jsonObject.put(ModuleConstant.LOW_APP_ID,authAppId); + // 此处的 access_token 为 【authorizer_access_token】 + String data = HttpClientManager.getInstance().sendPostByJSON(WxMaCodeConstant.API_CREATE_OPEN + authorizerAccessToken, JSON.toJSONString(jsonObject)).getData(); + Map map = JSON.parseObject(data, Map.class); + openPlatformId = map.get(ModuleConstant.OPEN_APP_ID).toString(); + CreateOpenResultDTO createOpen = new CreateOpenResultDTO(); + createOpen.setErrCode((Integer)map.get(ModuleConstant.ERR_CODE)); + createOpen.setErrMsg(map.get(ModuleConstant.ERR_MSG).toString()); + createOpen.setOpenAppId(map.get(ModuleConstant.OPEN_APP_ID).toString()); + switch (createOpen.getErrCode()){ + case NumConstant.ZERO: + log.info(CREATE_AND_BIND_SUCCESS); + CreateOpenFormDTO coForm = new CreateOpenFormDTO(); + coForm.setOpenid(map.get(ModuleConstant.OPEN_APP_ID).toString()); + coForm.setCustomerId(customerId); + //插入 open_platform_account 表 + openPlatformAccountDao.insertOpenPlatFormAccount(coForm); + break; + case NumConstant.ONE_NEG: + throw new RenException(SYSTEM_ERROR); + case ModuleConstant.FORTY_THOUSAND_AND_THIRTEEN: + throw new RenException(INVALID_APP_ID); + case ModuleConstant.EIGHTY_NINE_THOUSAND: + throw new RenException(ACCOUNT_HAS_BOUND_OPEN); + } + }else if (authCount.size()>NumConstant.ZERO && !authCount.contains(authAppId)){ + log.info("该客户已创建过开放平台账号,直接绑定"); + JSONObject jsonObject = new JSONObject(); + jsonObject.put(ModuleConstant.LOW_APP_ID,authAppId); + jsonObject.put(ModuleConstant.OPEN_APP_ID,openPlatformId); + String data = HttpClientManager.getInstance().sendPostByJSON(WxMaCodeConstant.API_BIND_OPEN + authorizerAccessToken, JSON.toJSONString(jsonObject)).getData(); + Map map = JSON.parseObject(data, Map.class); + CreateOpenResultDTO createOpen = new CreateOpenResultDTO(); + createOpen.setErrCode((Integer)map.get(ModuleConstant.ERR_CODE)); + createOpen.setErrMsg(map.get(ModuleConstant.ERR_MSG).toString()); + switch (createOpen.getErrCode()){ + case NumConstant.ZERO: + log.info(BIND_SUCCESS); + break; + case NumConstant.ONE_NEG: + throw new RenException(SYSTEM_ERROR); + case ModuleConstant.FORTY_THOUSAND_AND_THIRTEEN: + throw new RenException(INVALID_APP_ID); + case ModuleConstant.EIGHTY_NINE_THOUSAND: + throw new RenException(ACCOUNT_HAS_BOUND_OPEN); + case ModuleConstant.EIGHTY_NINE_THOUSAND_AND_ONE: + throw new RenException(NOT_SAME_CONTRACTOR); + case ModuleConstant.EIGHTY_NINE_THOUSAND_AND_THREE: + throw new RenException(NOT_ALLOWED_OPERATE); + case ModuleConstant.EIGHTY_NINE_THOUSAND_AND_FOUR: + throw new RenException(TO_LIMIT); + } + } + //插入 binding_account + BindingAccountFormDTO bindingAccount = new BindingAccountFormDTO(); + bindingAccount.setOpenPlatformAccountId(openPlatformId); + bindingAccount.setAuthAppId(authAppId); + bindingAccount.setClientType(clientType); + bindingAccount.setCustomerId(customerId); + bindingAccountDao.insertBindingAccount(bindingAccount); + log.info("创建绑定账号结束"); + } + /** * @Description map 转 Entity * @param map diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/AuthorizationInfoDao.xml b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/AuthorizationInfoDao.xml index cee136fb42..1bdc899f4f 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/AuthorizationInfoDao.xml +++ b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/AuthorizationInfoDao.xml @@ -68,6 +68,7 @@ update authorization_info set del_flag = 1 where customer_id = #{customerId} AND client_type = #{clientType} + - SELECT - COUNT(*) AS notAuthCount + app_id AS authAppId FROM customer_mp WHERE diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/OpenFlatformAccountDao.xml b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/OpenFlatformAccountDao.xml index efaa55adad..3cc916bb31 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/OpenFlatformAccountDao.xml +++ b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/OpenFlatformAccountDao.xml @@ -12,7 +12,7 @@ VALUES ( REPLACE ( UUID(), '-', '' ), - #{openAppId}, + #{openid}, #{customerId}, #{delFlag}, #{revision}, @@ -28,7 +28,7 @@ SELECT open_app_id AS openAppId FROM - open_flatform_account + open_platform_account WHERE del_flag = 0 AND customer_id = #{customerId}