diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpResiUserSubscribeDao.java b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpResiUserSubscribeDao.java index a59a55e67e..3465d0d90f 100644 --- a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpResiUserSubscribeDao.java +++ b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpResiUserSubscribeDao.java @@ -53,7 +53,7 @@ public interface WxmpResiUserSubscribeDao extends BaseDao msgList) { logger.info("待发送订阅消息数量:{}", msgList.size()); - if (!(msgList.size()>NumConstant.ZERO)){ + if (!(msgList.size() > NumConstant.ZERO)) { throw new RenException("待发送消息不能为空"); } int succecssCount = 0; @@ -129,7 +130,8 @@ public class WxmpMessageServiceImpl implements WxmpMessageService { customerTemplateListFormDTO.setCustomerId(msgList.get(0).getCustomerId()); customerTemplateListFormDTO.setPublicId(WxmpMessageConstant.TEMPLATE_TYPE); List customerTemplateListResultDTOS = customerTemplateList(customerTemplateListFormDTO); - +// 获取AccessToken + Map accessTokenMap = getAccessToken(msgList.get(0).getCustomerId()); for (WxSubscribeMessageFormDTO msg : msgList) { try { String userId = msg.getUserId(); @@ -151,37 +153,16 @@ public class WxmpMessageServiceImpl implements WxmpMessageService { throw new WxSubscribeException("clientType有误", "", openId); } } catch (Exception e) { - throw new WxSubscribeException("连接User服务失败"+e.getMessage(), "", ""); + throw new WxSubscribeException("连接User服务失败" + e.getMessage(), "", ""); } if (StringUtils.isBlank(openId)) { throw new WxSubscribeException("openId获取失败", "", ""); } - - //获取accessToken - String clientTypeRedis = null; - if (WxmpMessageConstant.RESI.equals(msg.getClientType())) { - clientTypeRedis = WxmpMessageConstant.RESI; - } - if (WxmpMessageConstant.GOV_DB.equals(msg.getClientType())) { - clientTypeRedis = WxmpMessageConstant.GOV_REDIS; - } - StringBuilder key = new StringBuilder(msg.getCustomerId()).append(":").append(clientTypeRedis); - Map authorizerRefreshToken = new HashMap<>(); - try { - authorizerRefreshToken = wxmpMessageRedis.getAuthorizerRefreshToken(key.toString()); - } catch (Exception e) { - throw new WxSubscribeException("连接缓存服务器失败"+e.getMessage(), "", openId); - } - String accessToken = (String) authorizerRefreshToken.get(WxmpMessageConstant.AUTHORIZER_ACCESS_TOKEN); - if (StringUtils.isBlank(accessToken)) { - throw new WxSubscribeException("accessToken获取失败", "", openId); - } - //获取模板id if (customerTemplateListResultDTOS.size() > NumConstant.ZERO) { for (CustomerTemplateListResultDTO customerTemplateListResultDTO : customerTemplateListResultDTOS) { - if (WxmpMessageConstant.GOV_REDIS.equals(customerTemplateListResultDTO.getClientType())){ + if (WxmpMessageConstant.GOV_REDIS.equals(customerTemplateListResultDTO.getClientType())) { customerTemplateListResultDTO.setClientType(WxmpMessageConstant.GOV_DB); } if (clientType.equals(customerTemplateListResultDTO.getClientType())) { @@ -197,9 +178,9 @@ public class WxmpMessageServiceImpl implements WxmpMessageService { //判断用户是否有次数 Integer count = null; if (WxmpMessageConstant.RESI.equals(clientType)) { - count = wxmpResiUserSubscribeDao.getResiSubscribeInfo(openId, templateId, customerId,userId); + count = wxmpResiUserSubscribeDao.getResiSubscribeInfo(openId, templateId, customerId, userId); } else if (WxmpMessageConstant.GOV_DB.equals(clientType)) { - count = wxmpWorkUserSubscribeDao.getWorkSubscribeInfo(openId, templateId, customerId,userId); + count = wxmpWorkUserSubscribeDao.getWorkSubscribeInfo(openId, templateId, customerId, userId); } if (count == null) { //用户未订阅 @@ -209,6 +190,17 @@ public class WxmpMessageServiceImpl implements WxmpMessageService { throw new WxSubscribeException("用户可用额度不足", templateId, openId); } + //获取accessToken + String accessToken = null; + if (WxmpMessageConstant.RESI.equals(clientType)) { + accessToken = accessTokenMap.get("resiToken"); + } else if (WxmpMessageConstant.GOV_DB.equals(clientType)) { + accessToken = accessTokenMap.get("govToken"); + } + if (StringUtils.isBlank(accessToken)) { + throw new WxSubscribeException("accessToken获取失败"+accessTokenMap.get("errorMsg"), "", openId); + } + //发送消息 JSONObject jsonObject = new JSONObject(); JSONObject data = new JSONObject(); @@ -259,9 +251,9 @@ public class WxmpMessageServiceImpl implements WxmpMessageService { //订阅条数-1 int decrease = 0; if (WxmpMessageConstant.RESI.equals(clientType)) { - decrease = wxmpResiUserSubscribeDao.decreaseResiSubscribeCount(openId, templateId, customerId, 1); + decrease = wxmpResiUserSubscribeDao.decreaseResiSubscribeCount(openId, templateId, customerId, 1,userId); } else if (WxmpMessageConstant.GOV_DB.equals(clientType)) { - decrease = wxmpWorkUserSubscribeDao.decreaseWorkSubscribeCount(openId, templateId, customerId, 1); + decrease = wxmpWorkUserSubscribeDao.decreaseWorkSubscribeCount(openId, templateId, customerId, 1,userId); } if (decrease == 0) { logger.error("消息{}发送成功但订阅条数-1失败", JSON.toJSONString(msg)); @@ -277,9 +269,9 @@ public class WxmpMessageServiceImpl implements WxmpMessageService { if (errcode.equals(WxmpMessageConstant.USER_REFUSED)) { int clear = 0; if (WxmpMessageConstant.RESI.equals(clientType)) { - clear = wxmpResiUserSubscribeDao.clearResiSubscribeCount(openId, templateId, customerId); + clear = wxmpResiUserSubscribeDao.clearResiSubscribeCount(openId, templateId, customerId,userId); } else if (WxmpMessageConstant.GOV_DB.equals(clientType)) { - clear = wxmpWorkUserSubscribeDao.clearWorkSubscribeCount(openId, templateId, customerId); + clear = wxmpWorkUserSubscribeDao.clearWorkSubscribeCount(openId, templateId, customerId,userId); } if (clear == 0) { logger.error("消息{}发送失败且清空订阅条数失败", JSON.toJSONString(msg)); @@ -319,7 +311,7 @@ public class WxmpMessageServiceImpl implements WxmpMessageService { wxmpMsgSendRecordEntity.setCustomerId(msg.getCustomerId()); wxmpMsgSendRecordEntity.setClientType(msg.getClientType()); wxmpMsgSendRecordEntity.setTemplateId(templateId); - wxmpMsgSendRecordEntity.setGridId(msg.getGridId()==null?"":msg.getGridId()); + wxmpMsgSendRecordEntity.setGridId(msg.getGridId() == null ? "" : msg.getGridId()); wxmpMsgSendRecordEntity.setUserId(msg.getUserId()); wxmpMsgSendRecordEntity.setWxOpenId(openId); wxmpMsgSendRecordEntity.setBehaviorType(msg.getBehaviorType()); @@ -330,6 +322,57 @@ public class WxmpMessageServiceImpl implements WxmpMessageService { return wxmpMsgSendRecordEntity; } + //获取AccessToken + private Map getAccessToken(String customerId) { + EnvEnum envEnum = EnvEnum.getCurrentEnv(); + String resiAccessToken = null; + String govAccessToken = null; + String errorMsg = null; + if (EnvEnum.PROD.getCode().equals(envEnum.getCode())) { + //居民端 + StringBuilder resiKey = new StringBuilder(customerId).append(":").append(WxmpMessageConstant.RESI); + Map authorizerRefreshToken = new HashMap<>(); + try { + authorizerRefreshToken = wxmpMessageRedis.getAuthorizerRefreshToken(resiKey.toString()); + } catch (Exception e) { + errorMsg = e.getMessage(); + } + resiAccessToken = (String) authorizerRefreshToken.get(WxmpMessageConstant.AUTHORIZER_ACCESS_TOKEN); + //政府端 + StringBuilder govKey = new StringBuilder(customerId).append(":").append(WxmpMessageConstant.GOV_REDIS); + authorizerRefreshToken = new HashMap<>(); + try { + authorizerRefreshToken = wxmpMessageRedis.getAuthorizerRefreshToken(resiKey.toString()); + } catch (Exception e) { + errorMsg = e.getMessage(); + } + govAccessToken = (String) authorizerRefreshToken.get(WxmpMessageConstant.AUTHORIZER_ACCESS_TOKEN); + } else { + String url = "https://epmet-cloud.elinkservice.cn/api/third/pacustomer/tokenlist"; + JSONObject postData = new JSONObject(); + postData.put("customerId", customerId); + String data = HttpClientManager.getInstance().sendPostByJSON(url, JSON.toJSONString(postData)).getData(); + JSONObject toResult = JSON.parseObject(data); + Result mapToResult = ConvertUtils.mapToEntity(toResult, Result.class); + if (null != toResult.get("code")) { + mapToResult.setCode(((Integer) toResult.get("code")).intValue()); + } + if (!mapToResult.success()) { + errorMsg = StringUtils.isBlank(mapToResult.getMsg()) ? mapToResult.getInternalMsg() : mapToResult.getMsg(); + } + Object CustomerTokensResultDTO = mapToResult.getData(); + JSONObject json = JSON.parseObject(CustomerTokensResultDTO.toString()); + CustomerTokensResultDTO customerTokensResultDTO = ConvertUtils.mapToEntity(json, CustomerTokensResultDTO.class); + resiAccessToken = customerTokensResultDTO.getResiAuthorizerToken(); + govAccessToken = customerTokensResultDTO.getWorkAuthorizerToken(); + } + Map resultMap = new HashMap(); + resultMap.put("resiToken",resiAccessToken); + resultMap.put("govToken",govAccessToken); + resultMap.put("errorMsg",errorMsg); + return resultMap; + } + /** * @return * @Description 居民端、工作端-获取客户小程序模板列表 @@ -385,7 +428,7 @@ public class WxmpMessageServiceImpl implements WxmpMessageService { List resultList = (List) mapToResult.getData(); List list = new ArrayList<>(); //必须转换 - for(Object f:resultList){ + for (Object f : resultList) { JSONObject json = JSON.parseObject(f.toString()); CustomerTemplateListResultDTO ff = ConvertUtils.mapToEntity(json, CustomerTemplateListResultDTO.class); list.add(ff); diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpResiUserSubscribeDao.xml b/epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpResiUserSubscribeDao.xml index 52d7fd71fc..fa3c599a83 100644 --- a/epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpResiUserSubscribeDao.xml +++ b/epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpResiUserSubscribeDao.xml @@ -24,6 +24,7 @@ and customer_id = #{customerId} and template_id = #{templateId} and wx_open_id = #{openId} + and user_id = #{userId} and wx_subscribe_status = 'subscribe' @@ -36,20 +37,9 @@ and customer_id = #{customerId} and template_id = #{templateId} and wx_open_id = #{openId} + and user_id = #{userId} - - update wxmp_resi_user_subscribe diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpWorkUserSubscribeDao.xml b/epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpWorkUserSubscribeDao.xml index cd25e8c5d2..5d82cbf369 100644 --- a/epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpWorkUserSubscribeDao.xml +++ b/epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpWorkUserSubscribeDao.xml @@ -33,6 +33,7 @@ and customer_id = #{customerId} and template_id = #{templateId} and wx_open_id = #{openId} + and user_id = #{userId} and wx_subscribe_status = 'subscribe' @@ -44,18 +45,8 @@ and customer_id = #{customerId} and template_id = #{templateId} and wx_open_id = #{openId} + and user_id = #{userId} - update wxmp_work_user_subscribe