From f4a64a8999c705bb18ddde6cb4a7e0800af684c5 Mon Sep 17 00:00:00 2001 From: liushaowen <565850092@qq.com> Date: Thu, 22 Oct 2020 14:41:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E9=80=81=E8=AE=A2=E9=98=85=E6=B6=88?= =?UTF-8?q?=E6=81=AF=20=E5=88=9D=E7=89=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/epmet/dto/WxmpMsgSendRecordDTO.java | 4 +- .../dto/form/WxSubscribeMessageFormDTO.java | 80 +++++++ .../feign/EpmetMessageOpenFeignClient.java | 10 + .../EpmetMessageOpenFeignClientFallback.java | 6 + .../epmet/constant/WxmpMessageConstant.java | 50 +++++ .../controller/WxmpMessageController.java | 11 +- .../com/epmet/dao/WxmpMsgSendRecordDao.java | 5 +- .../epmet/dao/WxmpResiUserSubscribeDao.java | 39 +++- .../epmet/dao/WxmpWorkUserSubscribeDao.java | 40 +++- .../epmet/entity/WxmpMsgSendRecordEntity.java | 2 +- .../epmet/exception/WxSubscribeException.java | 32 +++ .../com/epmet/redis/WxmpMessageRedis.java | 30 +++ .../com/epmet/service/WxmpMessageService.java | 15 +- .../service/WxmpMsgSendRecordService.java | 12 +- .../service/WxmpResiUserSubscribeService.java | 2 +- .../service/impl/WxmpMessageServiceImpl.java | 208 +++++++++++++++++- .../impl/WxmpMsgSendRecordServiceImpl.java | 7 +- .../resources/mapper/WxmpMsgSendRecordDao.xml | 44 +++- .../mapper/WxmpResiUserSubscribeDao.xml | 34 ++- .../mapper/WxmpWorkUserSubscribeDao.xml | 33 ++- 20 files changed, 642 insertions(+), 22 deletions(-) create mode 100644 epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/form/WxSubscribeMessageFormDTO.java create mode 100644 epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/constant/WxmpMessageConstant.java create mode 100644 epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/exception/WxSubscribeException.java create mode 100644 epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/redis/WxmpMessageRedis.java diff --git a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/WxmpMsgSendRecordDTO.java b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/WxmpMsgSendRecordDTO.java index 20d23aab47..99149fde28 100644 --- a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/WxmpMsgSendRecordDTO.java +++ b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/WxmpMsgSendRecordDTO.java @@ -81,7 +81,7 @@ public class WxmpMsgSendRecordDTO implements Serializable { /** * 消息时间 消息时间 */ - private Date messageTitme; + private Date messageTime; /** * 发送结果(成功:success 失败:error) @@ -123,4 +123,4 @@ public class WxmpMsgSendRecordDTO implements Serializable { */ private Date updatedTime; -} \ No newline at end of file +} diff --git a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/form/WxSubscribeMessageFormDTO.java b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/form/WxSubscribeMessageFormDTO.java new file mode 100644 index 0000000000..26f718715f --- /dev/null +++ b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/form/WxSubscribeMessageFormDTO.java @@ -0,0 +1,80 @@ +package com.epmet.dto.form; + +import com.alibaba.fastjson.annotation.JSONField; +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import java.io.Serializable; +import java.util.Date; + +/** + * @description: 微信订阅消息FormDTO + * @author: liushaowen + * @date: 2020/10/21 14:29 + */ +@Data +public class WxSubscribeMessageFormDTO implements Serializable { + private static final long serialVersionUID = 1L; + + /** + * 客户id + */ + @NotBlank(message = "客户id不能为空") + private String customerId; + /** + * 客户端类型 居民端:resi 工作端:work + */ + @NotBlank(message = "客户端类型不能为空") + private String clientType; + + /** + * 接收者(用户)的 userId + */ + @NotBlank(message = "接收用户id不能为空") + private String userId; + + /** + * 行为类型(存title字段的中间值) 入组申请、党员认证等 + */ + @NotBlank(message = "行为类型不能为空") + private String behaviorType; + + /** + * 消息内容 + */ + @NotBlank(message = "消息内容不能为空") + private String messageContent; + + /** + * 消息时间 + */ + @NotNull(message = "消息时间不能为空") + @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") + @JSONField(format = "yyyy-MM-dd HH:mm:ss") + private Date messageTime; + + /** + * 所需下发的订阅模板id + */ + @NotBlank(message = "模板id不能为空") + private String templateId; + + /** + * 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。 + */ + private String page; + + /** + * 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版 + */ + private String miniprogramState; + + /** + * 进入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN + */ + private String lang; + +} diff --git a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/feign/EpmetMessageOpenFeignClient.java b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/feign/EpmetMessageOpenFeignClient.java index b8f1def9d3..1035ab1552 100644 --- a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/feign/EpmetMessageOpenFeignClient.java +++ b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/feign/EpmetMessageOpenFeignClient.java @@ -5,6 +5,7 @@ import com.epmet.commons.tools.utils.Result; import com.epmet.dto.SysSmsDTO; import com.epmet.dto.form.SendVerificationCodeFormDTO; import com.epmet.dto.form.UserMessageFormDTO; +import com.epmet.dto.form.WxSubscribeMessageFormDTO; import com.epmet.dto.result.SendVerificationCodeResultDTO; import com.epmet.feign.fallback.EpmetMessageOpenFeignClientFallback; import org.springframework.cloud.openfeign.FeignClient; @@ -67,4 +68,13 @@ public interface EpmetMessageOpenFeignClient { **/ @PostMapping(value = "message/usermessage/saveusermessagelist", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE) Result saveUserMessageList(List msgList); + + /** + * @param msgList + * @return com.epmet.commons.tools.utils.Result + * @Author liushaowen + * @Description 发送微信订阅消息 + **/ + @PostMapping(value = "message/wxmpmessage/sendwxsubscribemessage", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE) + Result sendWxSubscribeMessage(List msgList); } diff --git a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/feign/fallback/EpmetMessageOpenFeignClientFallback.java b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/feign/fallback/EpmetMessageOpenFeignClientFallback.java index 3cc5a5c5eb..889569147f 100644 --- a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/feign/fallback/EpmetMessageOpenFeignClientFallback.java +++ b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/feign/fallback/EpmetMessageOpenFeignClientFallback.java @@ -6,6 +6,7 @@ import com.epmet.commons.tools.utils.Result; import com.epmet.dto.SysSmsDTO; import com.epmet.dto.form.SendVerificationCodeFormDTO; import com.epmet.dto.form.UserMessageFormDTO; +import com.epmet.dto.form.WxSubscribeMessageFormDTO; import com.epmet.dto.result.SendVerificationCodeResultDTO; import com.epmet.feign.EpmetMessageOpenFeignClient; import org.springframework.stereotype.Component; @@ -45,4 +46,9 @@ public class EpmetMessageOpenFeignClientFallback implements EpmetMessageOpenFeig public Result saveUserMessageList(List msgList) { return ModuleUtils.feignConError(ServiceConstant.EPMET_MESSAGE_SERVER, "saveUserMessageList", msgList); } + + @Override + public Result sendWxSubscribeMessage(List msgList) { + return ModuleUtils.feignConError(ServiceConstant.EPMET_MESSAGE_SERVER, "sendWxSubscribeMessage", msgList); + } } diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/constant/WxmpMessageConstant.java b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/constant/WxmpMessageConstant.java new file mode 100644 index 0000000000..ea44ee4861 --- /dev/null +++ b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/constant/WxmpMessageConstant.java @@ -0,0 +1,50 @@ +package com.epmet.constant; + +import java.util.HashMap; +import java.util.Map; + +/** + * @description: 微信订阅消息常量 + * @author: liushaowen + * @date: 2020/10/21 17:45 + */ + +public interface WxmpMessageConstant { + String SEND_MESSAGE = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="; + + String ERR_CODE = "errcode"; + + String ERR_MSG = "errmsg"; + + int USER_REFUSED = 43101; + + String AUTHORIZER_ACCESS_TOKEN = "authorizer_access_token"; + + String RESI = "resi"; + + String WORK = "work"; + + String ACCESS_TOKEN = "access_token"; + + String TOUSER = "touser"; + + String TEMPLATE_ID = "template_id"; + + String PAGE = "page"; + + String TITLE = "title"; + + String MESSAGE_CONTENT = "message_content"; + + String MESSAGE_TIME = "message_time"; + + String DATA = "data"; + + String MINIPROGRAM_STATE = "miniprogram_state"; + + String LANG = "lang"; + + String SUCCESS = "success"; + + String ERROR = "error"; +} diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/controller/WxmpMessageController.java b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/controller/WxmpMessageController.java index 57441016b3..05887e3e54 100644 --- a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/controller/WxmpMessageController.java +++ b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/controller/WxmpMessageController.java @@ -21,10 +21,13 @@ import com.epmet.commons.tools.security.user.LoginUserUtil; import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.validator.ValidatorUtils; import com.epmet.dto.form.WxMsgAuthInfoFormDTO; +import com.epmet.dto.form.WxSubscribeMessageFormDTO; import com.epmet.service.WxmpMessageService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import java.util.List; + /** * 微信消息订阅controller @@ -61,5 +64,9 @@ public class WxmpMessageController { return new Result(); } - -} \ No newline at end of file + @PostMapping("sendwxsubscribemessage") + public Result sendWxSubscribeMessage(@RequestBody List msgList){ + wxmpMessageService.sendWxSubscribeMessage(msgList); + return new Result(); + } +} diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpMsgSendRecordDao.java b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpMsgSendRecordDao.java index e20aa97fd6..17aacf2a79 100644 --- a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpMsgSendRecordDao.java +++ b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpMsgSendRecordDao.java @@ -29,5 +29,6 @@ import org.apache.ibatis.annotations.Mapper; */ @Mapper public interface WxmpMsgSendRecordDao extends BaseDao { - -} \ No newline at end of file + + int saveRecord(WxmpMsgSendRecordEntity entity); +} 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 5a26cc19f7..d9ae74e313 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 @@ -18,8 +18,10 @@ package com.epmet.dao; import com.epmet.commons.mybatis.dao.BaseDao; +import com.epmet.dto.WxmpResiUserSubscribeDTO; import com.epmet.entity.WxmpResiUserSubscribeEntity; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; /** * 居民端用户订阅模板消息次数记录表 @@ -29,5 +31,38 @@ import org.apache.ibatis.annotations.Mapper; */ @Mapper public interface WxmpResiUserSubscribeDao extends BaseDao { - -} \ No newline at end of file + + /** + * @Description 根据openId获取剩余订阅条数 + * @param openId + * @param templateId + * @param customerId + * @return java.lang.Integer + * @Author liushaowen + * @Date 2020/10/22 9:30 + */ + Integer getResiSubscribeInfo(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId); + + /** + * @Description 减少订阅条数 + * @param openId + * @param customerId + * @param templateId + * @param i 减少的数量 + * @return int + * @Author liushaowen + * @Date 2020/10/22 9:38 + */ + int decreaseResiSubscribeCount(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId,@Param("num") int i); + + /** + * @Description 清空订阅条数,修改订阅状态 + * @param openId + * @param customerId + * @param templateId + * @return int + * @Author liushaowen + * @Date 2020/10/22 13:23 + */ + int clearResiSubscribeCount(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId); +} diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpWorkUserSubscribeDao.java b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpWorkUserSubscribeDao.java index 81a2659e04..2628f5c085 100644 --- a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpWorkUserSubscribeDao.java +++ b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/dao/WxmpWorkUserSubscribeDao.java @@ -20,14 +20,48 @@ package com.epmet.dao; import com.epmet.commons.mybatis.dao.BaseDao; import com.epmet.entity.WxmpWorkUserSubscribeEntity; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; /** - * 工作端用户订阅模板消息有效次数记录表 + * 工作端用户订阅模板消息有效次数记录表 * * @author generator generator@elink-cn.com * @since v1.0.0 2020-10-21 */ @Mapper public interface WxmpWorkUserSubscribeDao extends BaseDao { - -} \ No newline at end of file + + /** + * @Description 根据openId获取剩余订阅条数 + * @param openId + * @param templateId + * @param customerId + * @return java.lang.Integer + * @Author liushaowen + * @Date 2020/10/22 9:31 + */ + Integer getWorkSubscribeInfo(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId); + + /** + * @Description 减少订阅条数 + * @param openId + * @param templateId + * @param customerId + * @param i 减少的数量 + * @return int + * @Author liushaowen + * @Date 2020/10/22 9:38 + */ + int decreaseWorkSubscribeCount(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId, @Param("num") int i); + + /** + * @Description 清空订阅数,修改订阅状态 + * @param openId + * @param customerId + * @param templateId + * @return int + * @Author liushaowen + * @Date 2020/10/22 13:22 + */ + int clearWorkSubscribeCount(@Param("openId") String openId, @Param("templateId") String templateId, @Param("customerId") String customerId); +} diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/entity/WxmpMsgSendRecordEntity.java b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/entity/WxmpMsgSendRecordEntity.java index d7242fb4b8..89e1837475 100644 --- a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/entity/WxmpMsgSendRecordEntity.java +++ b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/entity/WxmpMsgSendRecordEntity.java @@ -81,7 +81,7 @@ public class WxmpMsgSendRecordEntity extends BaseEpmetEntity { /** * 消息时间 消息时间 */ - private Date messageTitme; + private Date messageTime; /** * 发送结果(成功:success 失败:error) diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/exception/WxSubscribeException.java b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/exception/WxSubscribeException.java new file mode 100644 index 0000000000..aa813bad2f --- /dev/null +++ b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/exception/WxSubscribeException.java @@ -0,0 +1,32 @@ +package com.epmet.exception; + +/** + * @description: + * @author: liushaowen + * @date: 2020/10/22 11:07 + */ + +public class WxSubscribeException extends Exception { + private String openId; + + /** + * Constructs a new exception with the specified detail message. The + * cause is not initialized, and may subsequently be initialized by + * a call to {@link #initCause}. + * + * @param message the detail message. The detail message is saved for + * later retrieval by the {@link #getMessage()} method. + */ + public WxSubscribeException(String message, String openId) { + super(message); + this.openId = openId; + } + + public String getOpenId() { + return openId; + } + + public void setOpenId(String openId) { + this.openId = openId; + } +} diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/redis/WxmpMessageRedis.java b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/redis/WxmpMessageRedis.java new file mode 100644 index 0000000000..a0672e7d58 --- /dev/null +++ b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/redis/WxmpMessageRedis.java @@ -0,0 +1,30 @@ +package com.epmet.redis; + +import com.epmet.commons.tools.redis.RedisUtils; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.Map; + +/** + * @description: 微信订阅Redis + * @author: liushaowen + * @date: 2020/10/21 15:28 + */ +@Slf4j +@Component +public class WxmpMessageRedis { + @Autowired + private RedisUtils redisUtils; + + /** + * @Description 获取刷新 + * @param key = epmet:wechartthird:authorizerrefreshtoken:customerId:clientType 前缀+客户ID+客户端类型 + * @author zxc + */ + public Map getAuthorizerRefreshToken(String key){ + Map result = redisUtils.hGetAll("epmet:wechartthird:authorizerrefreshtoken:" + key); + return result; + } +} diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/WxmpMessageService.java b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/WxmpMessageService.java index 25d631e1c9..ec571d4867 100644 --- a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/WxmpMessageService.java +++ b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/WxmpMessageService.java @@ -17,6 +17,10 @@ package com.epmet.service; +import com.epmet.dto.form.WxSubscribeMessageFormDTO; + +import java.util.List; + /** * 微信消息订阅Service * @@ -26,4 +30,13 @@ package com.epmet.service; public interface WxmpMessageService { void saveSysAuthorizeInfo(String customerId, String clientType, String alwaysVisit, String subscribeStatus, String userId); -} \ No newline at end of file + + /** + * @Description 发送订阅消息 + * @param msgList + * @return void + * @Author liushaowen + * @Date 2020/10/21 15:34 + */ + void sendWxSubscribeMessage(List msgList); +} diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/WxmpMsgSendRecordService.java b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/WxmpMsgSendRecordService.java index 54dead18cc..882848002b 100644 --- a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/WxmpMsgSendRecordService.java +++ b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/WxmpMsgSendRecordService.java @@ -92,4 +92,14 @@ public interface WxmpMsgSendRecordService extends BaseService msgList) { + logger.info("待发送订阅消息数量:{}", msgList.size()); + int succecssCount = 0; + for (WxSubscribeMessageFormDTO msg : msgList) { + try { + ValidatorUtils.validateEntity(msg); + + String userId = msg.getUserId(); + String clientType = msg.getClientType(); + String customerId = msg.getCustomerId(); + String templateId = msg.getTemplateId(); + String openId = null; + //通过userId获取openId + try { + if (WxmpMessageConstant.RESI.equals(clientType)) { + UserBasicInfoFormDTO userBasicInfoFormDTO = new UserBasicInfoFormDTO(); + userBasicInfoFormDTO.setUserId(userId); + openId = epmetUserOpenFeignClient.getUserBasicInfo(userBasicInfoFormDTO).getData().getOpenId(); + } else if (WxmpMessageConstant.WORK.equals(clientType)) { + StaffBasicInfoFormDTO staffBasicInfoFormDTO = new StaffBasicInfoFormDTO(); + staffBasicInfoFormDTO.setStaffId(userId); + openId = epmetUserOpenFeignClient.getStaffBasicInfo(staffBasicInfoFormDTO).getData().getOpenId(); + } else { + throw new WxSubscribeException("clientType有误",openId); + } + }catch (Exception e){ + throw new WxSubscribeException("连接User服务失败",""); + } + + if (StringUtils.isBlank(openId)){ + throw new WxSubscribeException("openId获取失败",""); + } + + //获取accessToken + StringBuilder key = new StringBuilder(msg.getCustomerId()).append(":").append(msg.getClientType()); + Map authorizerRefreshToken = new HashMap<>(); + try { + authorizerRefreshToken = wxmpMessageRedis.getAuthorizerRefreshToken(key.toString()); + }catch (Exception e){ + throw new WxSubscribeException("连接缓存服务器失败",openId); + } + String accessToken = (String) authorizerRefreshToken.get(WxmpMessageConstant.AUTHORIZER_ACCESS_TOKEN); + if (StringUtils.isBlank(accessToken)) { + throw new WxSubscribeException("accessToken获取失败",openId); + } + + //判断用户是否有次数 + Integer count = null; + if (WxmpMessageConstant.RESI.equals(clientType)) { + count = wxmpResiUserSubscribeDao.getResiSubscribeInfo(openId, customerId, templateId); + } else if (WxmpMessageConstant.WORK.equals(clientType)) { + count = wxmpWorkUserSubscribeDao.getWorkSubscribeInfo(openId, customerId, templateId); + } + if (count == null) { + //用户未订阅 + throw new WxSubscribeException("用户未订阅",openId); + } + if (count == 0) { + throw new WxSubscribeException("用户可用额度不足",openId); + } + + //发送消息 + JSONObject jsonObject = new JSONObject(); + JSONObject data = new JSONObject(); + //必填项 + jsonObject.put(WxmpMessageConstant.ACCESS_TOKEN, accessToken); + jsonObject.put(WxmpMessageConstant.TOUSER, openId); + jsonObject.put(WxmpMessageConstant.TEMPLATE_ID, templateId); + data.put(WxmpMessageConstant.TITLE, new JSONObject().put("value", "您有一条" + msg.getBehaviorType())); + data.put(WxmpMessageConstant.MESSAGE_CONTENT, new JSONObject().put("value", msg.getMessageContent())); + data.put(WxmpMessageConstant.MESSAGE_TIME, new JSONObject().put("value", msg.getMessageTime())); + jsonObject.put(WxmpMessageConstant.DATA, data); + //选填项 + if (StringUtils.isNotBlank(msg.getPage())) { + jsonObject.put(WxmpMessageConstant.PAGE, msg.getPage()); + } + if (StringUtils.isNotBlank(msg.getMiniprogramState())) { + jsonObject.put(WxmpMessageConstant.MINIPROGRAM_STATE, msg.getMiniprogramState()); + } + if (StringUtils.isNotBlank(msg.getLang())) { + jsonObject.put(WxmpMessageConstant.LANG, msg.getLang()); + } + String resultStr = HttpClientManager.getInstance().sendPostByJSON(WxmpMessageConstant.SEND_MESSAGE + accessToken, JSON.toJSONString(jsonObject)).getData(); + Map resultMap = JSON.parseObject(resultStr, Map.class); + Object errcode = resultMap.get(WxmpMessageConstant.ERR_CODE); + if (errcode.equals(NumConstant.ZERO)) { + //发送成功 + + //订阅条数-1 + int decrease = 0; + if (WxmpMessageConstant.RESI.equals(clientType)) { + decrease = wxmpResiUserSubscribeDao.decreaseResiSubscribeCount(openId, customerId, templateId, 1); + } else if (WxmpMessageConstant.WORK.equals(clientType)) { + decrease = wxmpWorkUserSubscribeDao.decreaseWorkSubscribeCount(openId, customerId, templateId, 1); + } + if (decrease == 0) { + logger.error("消息{}发送成功但订阅条数-1失败", JSON.toJSONString(msg)); + } + //存表 + int saveRes = wxmpMsgSendRecordService.saveRecord(initRecord(msg,openId,WxmpMessageConstant.SUCCESS)); + if (saveRes == 0) { + logger.error("消息{}发送成功但存入记录表失败", JSON.toJSONString(msg)); + } + } else { + //发送失败 + //用户拒绝,需清空订阅表条数,修改订阅状态 + if (errcode.equals(WxmpMessageConstant.USER_REFUSED)) { + int clear = 0; + if (WxmpMessageConstant.RESI.equals(clientType)) { + clear = wxmpResiUserSubscribeDao.clearResiSubscribeCount(openId, customerId, templateId); + } else if (WxmpMessageConstant.WORK.equals(clientType)) { + clear = wxmpWorkUserSubscribeDao.clearWorkSubscribeCount(openId, customerId, templateId); + } + if (clear == 0) { + logger.error("消息{}发送失败且清空订阅条数失败", JSON.toJSONString(msg)); + } + } + + //抛出错误 + throw new WxSubscribeException(String.valueOf(resultMap.get(WxmpMessageConstant.ERR_MSG)),openId); + } + + succecssCount++; + } catch (Exception e) { + String errMsg = e.getMessage(); + //ValidateException错误信息为getMsg + if (StringUtils.isBlank(errMsg) && e instanceof ValidateException) { + errMsg = ((ValidateException) e).getMsg(); + } + if (e instanceof WxSubscribeException){ + //存表 + WxmpMsgSendRecordEntity wxmpMsgSendRecordEntity = initRecord(msg, ((WxSubscribeException) e).getOpenId(), WxmpMessageConstant.ERROR); + wxmpMsgSendRecordEntity.setReason(errMsg); + int saveRes = wxmpMsgSendRecordService.saveRecord(wxmpMsgSendRecordEntity); + if (saveRes == 0) { + logger.error("消息{}发送失败且存入记录表失败", JSON.toJSONString(msg)); + } + } + + logger.error("消息:{}发送失败,原因是:{}", JSON.toJSONString(msg), errMsg); + continue; + } + } + logger.info("{}条消息中的{}条发送成功", msgList.size(), succecssCount); + } + private WxmpMsgSendRecordEntity initRecord(WxSubscribeMessageFormDTO msg, String openId, String status){ + WxmpMsgSendRecordEntity wxmpMsgSendRecordEntity = new WxmpMsgSendRecordEntity(); + wxmpMsgSendRecordEntity.setCustomerId(msg.getCustomerId()); + wxmpMsgSendRecordEntity.setClientType(msg.getClientType()); + wxmpMsgSendRecordEntity.setTemplateId(msg.getTemplateId()); + wxmpMsgSendRecordEntity.setUserId(msg.getUserId()); + wxmpMsgSendRecordEntity.setWxOpenId(openId); + wxmpMsgSendRecordEntity.setBehaviorType(msg.getBehaviorType()); + wxmpMsgSendRecordEntity.setTitle("您有一条" + msg.getBehaviorType()); + wxmpMsgSendRecordEntity.setMessageContent(msg.getMessageContent()); + wxmpMsgSendRecordEntity.setMessageTime(msg.getMessageTime()); + wxmpMsgSendRecordEntity.setResult(status); + return wxmpMsgSendRecordEntity; + } +} diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/impl/WxmpMsgSendRecordServiceImpl.java b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/impl/WxmpMsgSendRecordServiceImpl.java index 5bd33aa047..62fdc10b0d 100644 --- a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/impl/WxmpMsgSendRecordServiceImpl.java +++ b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/impl/WxmpMsgSendRecordServiceImpl.java @@ -101,4 +101,9 @@ public class WxmpMsgSendRecordServiceImpl extends BaseServiceImpl + + insert into wxmp_msg_send_record + (id, + customer_id, + client_type, + template_id, + user_id, + wx_open_id, + behavior_type, + title, + message_content, + message_time, + result, + reason, + del_flag, + revision, + created_by, + created_time, + updated_by, + updated_time) + values + (MD5(replace(UUID(),'-','')), + #{customerId}, + #{clientType}, + #{templateId}, + #{userId}, + #{wxOpenId}, + #{behaviorType}, + #{title}, + #{messageContent}, + #{messageTime}, + #{result}, + #{reason}, + 0, + 0, + 'sys', + now(), + 'sys', + now() + ) + - - \ No newline at end of file + 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 83ba28d09d..f47cbdf0a0 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 @@ -3,6 +3,38 @@ + + + update WxmpResiUserSubscribe + set count = if(count < 1, 0, count - ${num} ) + where + del_flag =0 + and customer_id = #{customerId} + and template_id = #{templateId} + and wx_open_id = #{openId} + and wx_subscribe_status = 'subscribe' + - \ No newline at end of file + + update WxmpResiUserSubscribe + set count = 0, + wx_subscribe_status = 'unsubscribe' + where + del_flag =0 + and customer_id = #{customerId} + and template_id = #{templateId} + and wx_open_id = #{openId} + + + 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 608e32e0c5..6f368f8a6e 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 @@ -3,6 +3,35 @@ + - - \ No newline at end of file + + update WxmpWorkUserSubscribe + set count = if(count < 1, 0, count - ${num} ) + where + del_flag =0 + and customer_id = #{customerId} + and template_id = #{templateId} + and wx_open_id = #{openId} + and wx_subscribe_status = 'subscribe' + + + update WxmpWorkUserSubscribe + set count = 0, + wx_subscribe_status = 'unsubscribe' + where + del_flag =0 + and customer_id = #{customerId} + and template_id = #{templateId} + and wx_open_id = #{openId} + +