diff --git a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/constant/WxMsgTemplateSubscribeStatus.java b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/constant/WxMsgTemplateSubscribeStatus.java
new file mode 100644
index 0000000000..9ea7e585ae
--- /dev/null
+++ b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/constant/WxMsgTemplateSubscribeStatus.java
@@ -0,0 +1,7 @@
+package com.epmet.constant;
+
+public interface WxMsgTemplateSubscribeStatus {
+ //wx订阅状态 订阅状态(订阅:subscribe 取消订阅:unsubscribe)
+ String SUBSCRIBE = "subscribe";
+ String UNSUBSCRIBE = "unsubscribe";
+}
diff --git a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/WxmpTemplateMsgSubscribeStatusDTO.java b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/WxmpTemplateMsgSubscribeStatusDTO.java
index 00dc07a199..59ad254400 100644
--- a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/WxmpTemplateMsgSubscribeStatusDTO.java
+++ b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/WxmpTemplateMsgSubscribeStatusDTO.java
@@ -103,4 +103,6 @@ public class WxmpTemplateMsgSubscribeStatusDTO implements Serializable {
*/
private Date updatedTime;
+ private Date templateId;
+
}
\ No newline at end of file
diff --git a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/form/WxMsgAuthInfoFormDTO.java b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/form/WxMsgAuthInfoFormDTO.java
index 11d70e506d..c8dde831d2 100644
--- a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/form/WxMsgAuthInfoFormDTO.java
+++ b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/form/WxMsgAuthInfoFormDTO.java
@@ -8,17 +8,35 @@ import javax.validation.constraints.NotBlank;
public class WxMsgAuthInfoFormDTO {
public interface SaveSysAuthInfoGroup {}
+ public interface GetAuthInfoGroup {}
+ public interface SaveWxSubscribeInfoGroup {}
- @NotBlank(message = "客户id不能为空", groups = { SaveSysAuthInfoGroup.class })
+ @NotBlank(message = "客户id不能为空", groups = { SaveSysAuthInfoGroup.class, GetAuthInfoGroup.class, SaveWxSubscribeInfoGroup.class })
private String customerId;
- @NotBlank(message = "客户端类型不能为空", groups = { SaveSysAuthInfoGroup.class })
+ @NotBlank(message = "客户端类型不能为空", groups = { SaveSysAuthInfoGroup.class, GetAuthInfoGroup.class, SaveWxSubscribeInfoGroup.class })
private String clientType;
- @NotBlank(message = "是否总是字段不能为空", groups = { SaveSysAuthInfoGroup.class })
+ @NotBlank(message = "'是否总是允许'选项不能为空", groups = { SaveSysAuthInfoGroup.class })
private String alwaysVisit;
@NotBlank(message = "是否去订阅字段不能为空", groups = { SaveSysAuthInfoGroup.class })
private String subscribeStatus;
+ @NotBlank(message = "模板ID不能为空", groups = { SaveWxSubscribeInfoGroup.class, GetAuthInfoGroup.class, SaveSysAuthInfoGroup.class })
+ private String templateId;
+
+ @NotBlank(message = "微信'是否总是允许'选项不能为空", groups = { SaveWxSubscribeInfoGroup.class })
+ private String wxAlwaysVisit;
+
+ @NotBlank(message = "微信'是否订阅字段不能为空'", groups = { SaveWxSubscribeInfoGroup.class })
+ private String wxSubscribeStatus;
+
+ @NotBlank(message = "动作类型不能为空", groups = { SaveWxSubscribeInfoGroup.class })
+ private String behaviorType;
+
+
+
+
+
}
diff --git a/epmet-module/epmet-message/epmet-message-server/pom.xml b/epmet-module/epmet-message/epmet-message-server/pom.xml
index 28f44f4962..6a8d05d9de 100644
--- a/epmet-module/epmet-message/epmet-message-server/pom.xml
+++ b/epmet-module/epmet-message/epmet-message-server/pom.xml
@@ -25,6 +25,11 @@
epmet-message-client
2.0.0
+
+ com.epmet
+ epmet-user-client
+ 2.0.0
+
com.epmet
epmet-admin-client
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 7e06c80b0a..15e8e81d0e 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
@@ -24,12 +24,10 @@ import com.epmet.dto.form.GetTemplateListFormDTO;
import com.epmet.dto.form.WxMsgAuthInfoFormDTO;
import com.epmet.dto.form.WxSubscribeMessageFormDTO;
import com.epmet.dto.result.GetTemplateListResultDTO;
+import com.epmet.dto.result.WxMsgAuthInfoResultDTO;
import com.epmet.service.WxmpMessageService;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
import java.util.List;
@@ -64,11 +62,13 @@ public class WxmpMessageController {
String clientType = form.getClientType();
String customerId = form.getCustomerId();
String subscribeStatus = form.getSubscribeStatus();
+ String templateId = form.getTemplateId();
- wxmpMessageService.saveSysAuthorizeInfo(customerId, clientType, alwaysVisit, subscribeStatus, loginUserUtil.getLoginUserId());
+ wxmpMessageService.saveSysAuthorizeInfo(customerId, clientType, alwaysVisit, subscribeStatus, loginUserUtil.getLoginUserId(), templateId);
return new Result();
}
+
@PostMapping("sendwxsubscribemessage")
public Result sendWxSubscribeMessage(@RequestBody List msgList){
for (WxSubscribeMessageFormDTO wxSubscribeMessageFormDTO : msgList) {
@@ -89,4 +89,39 @@ public class WxmpMessageController {
return new Result>().ok(wxmpMessageService.templateList(formDTO));
}
-}
+ /**
+ * @Description 查询用户授权信息
+ * @return
+ * @author wxz
+ * @date 2020.10.21 18:15
+ */
+ @PostMapping("get-authorizeinfo")
+ public Result getAuthorizationInfo(@RequestBody WxMsgAuthInfoFormDTO form) {
+ ValidatorUtils.validateEntity(form, WxMsgAuthInfoFormDTO.GetAuthInfoGroup.class);
+ WxMsgAuthInfoResultDTO authorizationInfo = wxmpMessageService.getAuthorizationInfo(
+ form.getCustomerId(), form.getClientType(),
+ loginUserUtil.getLoginUserId(), form.getTemplateId());
+ return new Result().ok(authorizationInfo);
+ }
+
+ /**
+ * 保存微信订阅的授权信息
+ * @param form
+ * @return
+ */
+ @PostMapping("save-wxsubscribe")
+ public Result saveWxSubscribe(@RequestBody WxMsgAuthInfoFormDTO form) {
+ ValidatorUtils.validateEntity(form, WxMsgAuthInfoFormDTO.SaveWxSubscribeInfoGroup.class);
+ String customerId = form.getCustomerId();
+ String clientType = form.getClientType();
+ String templateId = form.getTemplateId();
+ String wxAlwaysVisit = form.getWxAlwaysVisit();
+ String wxSubscribeStatus = form.getWxSubscribeStatus();
+ String behaviorType = form.getBehaviorType();
+
+ wxmpMessageService.saveWxSubscribe(customerId, clientType, templateId, wxAlwaysVisit, wxSubscribeStatus, behaviorType, loginUserUtil.getLoginUserId());
+
+ return new Result();
+ }
+
+}
\ No newline at end of file
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 c1a6e83d2f..28080d66e7 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
@@ -74,4 +74,15 @@ public interface WxmpResiUserSubscribeDao extends BaseDao {
WxMsgAuthInfoResultDTO getUserSubscribeStatusDTO(@Param("userId") String userId,
- @Param("customerId") String customerId,
- @Param("clientType") String clientType);
+ @Param("customerId") String customerId,
+ @Param("clientType") String clientType,
+ @Param("templateId") String templateId);
WxmpTemplateMsgSubscribeStatusEntity getUserSubscribeStatusEntity(@Param("userId") String userId,
- @Param("customerId") String customerId,
- @Param("clientType") String clientType);
+ @Param("customerId") String customerId,
+ @Param("clientType") String clientType,
+ @Param("templateId") String templateId);
}
\ No newline at end of file
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 0638e2bfc4..eb33773c8f 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
@@ -23,7 +23,7 @@ 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
@@ -75,4 +75,16 @@ public interface WxmpWorkUserSubscribeDao extends BaseDao msgList) {
- logger.info("待发送订阅消息数量:{}", msgList.size());
- int succecssCount = 0;
- for (WxSubscribeMessageFormDTO msg : msgList) {
- try {
- String userId = msg.getUserId();
- String clientType = msg.getClientType();
- String customerId = msg.getCustomerId();
- String templateId = null;
- 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);
- }
-
- //获取模板id
- if (WxmpMessageConstant.RESI.equals(clientType)) {
- templateId = wxmpResiUserSubscribeDao.getResiSubscribeTemplateId(openId, templateId, customerId);
- } else if (WxmpMessageConstant.WORK.equals(clientType)) {
- templateId = wxmpWorkUserSubscribeDao.getWorkSubscribeTemplateId(openId, templateId, customerId);
- }
- if (StringUtils.isBlank(templateId)) {
- throw new WxSubscribeException("获取模板id失败", "", openId);
- }
-
- //判断用户是否有次数
- Integer count = null;
- if (WxmpMessageConstant.RESI.equals(clientType)) {
- count = wxmpResiUserSubscribeDao.getResiSubscribeInfo(openId, templateId, customerId);
- } else if (WxmpMessageConstant.WORK.equals(clientType)) {
- count = wxmpWorkUserSubscribeDao.getWorkSubscribeInfo(openId, templateId, customerId);
- }
- if (count == null) {
- //用户未订阅
- throw new WxSubscribeException("用户未订阅", templateId, openId);
- }
- if (count == 0) {
- throw new WxSubscribeException("用户可用额度不足", templateId, 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()).substring(0, WxmpMessageConstant.TITLE_LIMIT)));
- data.put(WxmpMessageConstant.MESSAGE_CONTENT, new JSONObject().put("value", msg.getMessageContent().substring(0, WxmpMessageConstant.MESSAGE_CONTENT_LIMIT)));
- data.put(WxmpMessageConstant.MESSAGE_TIME, new JSONObject().put("value", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date())));
- jsonObject.put(WxmpMessageConstant.DATA, data);
- EnvEnum envEnum = EnvEnum.getCurrentEnv();
- //选填项
- if (WxmpMessageConstant.RESI.equals(clientType)) {
- jsonObject.put(WxmpMessageConstant.PAGE, WxmpMessageConstant.PAGE_RESI);
- } else if (WxmpMessageConstant.WORK.equals(clientType)) {
- jsonObject.put(WxmpMessageConstant.PAGE, WxmpMessageConstant.PAGE_WORK);
- }
- //开发环境
- if ("dev".equals(envEnum.getCode())) {
- jsonObject.put(WxmpMessageConstant.MINIPROGRAM_STATE, WxmpMessageConstant.STATE_DEV);
- }
- //测试环境
- if ("test".equals(envEnum.getCode())) {
- jsonObject.put(WxmpMessageConstant.MINIPROGRAM_STATE, WxmpMessageConstant.STATE_TEST);
- }
-
- 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, templateId, customerId, 1);
- } else if (WxmpMessageConstant.WORK.equals(clientType)) {
- decrease = wxmpWorkUserSubscribeDao.decreaseWorkSubscribeCount(openId, templateId, customerId, 1);
- }
- if (decrease == 0) {
- logger.error("消息{}发送成功但订阅条数-1失败", JSON.toJSONString(msg));
- }
- //存表
- int saveRes = wxmpMsgSendRecordService.saveRecord(initRecord(msg, templateId, 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, templateId, customerId);
- } else if (WxmpMessageConstant.WORK.equals(clientType)) {
- clear = wxmpWorkUserSubscribeDao.clearWorkSubscribeCount(openId, templateId, customerId);
- }
- if (clear == 0) {
- logger.error("消息{}发送失败且清空订阅条数失败", JSON.toJSONString(msg));
- }
- }
-
- //抛出错误
- throw new WxSubscribeException(String.valueOf(resultMap.get(WxmpMessageConstant.ERR_MSG)),templateId, openId);
- }
-
- succecssCount++;
- } catch (Exception e) {
- String errMsg = e.getMessage();
- //ValidateException错误信息为getMsg
+ private Logger logger = LoggerFactory.getLogger(getClass());
+
+ @Autowired
+ private WxmpTemplateMsgSubscribeStatusDao msgSubscribeStatusDao;
+
+ @Autowired
+ private EpmetUserOpenFeignClient epmetUserOpenFeignClient;
+
+ @Autowired
+ private WxmpMessageRedis wxmpMessageRedis;
+
+ @Resource
+ private WxmpResiUserSubscribeDao wxmpResiUserSubscribeDao;
+
+ @Resource
+ private WxmpWorkUserSubscribeDao wxmpWorkUserSubscribeDao;
+
+ @Autowired
+ private WxmpMsgSendRecordService wxmpMsgSendRecordService;
+
+ @Autowired
+ private WxmpUserSubscribeRecordDao subscribeRecordDao;
+
+ @Autowired
+ private WxmpResiUserSubscribeDao resiUserSubscribeDao;
+
+ @Autowired
+ private EpmetUserOpenFeignClient userOpenFeignClient;
+
+ /**
+ * @Description 保存系统授权信息
+ * @return void
+ * @author wxz
+ * @date 2020.10.21 17:29
+ */
+ @Override
+ public void saveSysAuthorizeInfo(String customerId, String clientType, String alwaysVisit, String subscribeStatus, String userId, String templateId) {
+ WxmpTemplateMsgSubscribeStatusEntity userSubscribeStatusEntity = msgSubscribeStatusDao.getUserSubscribeStatusEntity(userId, customerId, clientType, templateId);
+ if (userSubscribeStatusEntity != null) {
+ userSubscribeStatusEntity.setAlwaysVisit(alwaysVisit);
+ userSubscribeStatusEntity.setSubscribeStatus(subscribeStatus);
+ msgSubscribeStatusDao.updateById(userSubscribeStatusEntity);
+ return;
+ }
+
+ userSubscribeStatusEntity = new WxmpTemplateMsgSubscribeStatusEntity();
+ userSubscribeStatusEntity.setCustomerId(customerId);
+ userSubscribeStatusEntity.setClientType(clientType);
+ userSubscribeStatusEntity.setTemplateId(templateId);
+ userSubscribeStatusEntity.setAlwaysVisit(alwaysVisit);
+ userSubscribeStatusEntity.setSubscribeStatus(subscribeStatus);
+ userSubscribeStatusEntity.setUserId(userId);
+ msgSubscribeStatusDao.insert(userSubscribeStatusEntity);
+ }
+
+ /**
+ * @param msgList
+ * @return void
+ * @Description 发送订阅消息
+ * @Author liushaowen
+ * @Date 2020/10/21 15:34
+ */
+ @Override
+ public void sendWxSubscribeMessage(List msgList) {
+ logger.info("待发送订阅消息数量:{}", msgList.size());
+ int succecssCount = 0;
+ for (WxSubscribeMessageFormDTO msg : msgList) {
+ try {
+ String userId = msg.getUserId();
+ String clientType = msg.getClientType();
+ String customerId = msg.getCustomerId();
+ String templateId = null;
+ 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);
+ }
+
+ //获取模板id
+ if (WxmpMessageConstant.RESI.equals(clientType)) {
+ templateId = wxmpResiUserSubscribeDao.getResiSubscribeTemplateId(openId, templateId, customerId);
+ } else if (WxmpMessageConstant.WORK.equals(clientType)) {
+ templateId = wxmpWorkUserSubscribeDao.getWorkSubscribeTemplateId(openId, templateId, customerId);
+ }
+ if (StringUtils.isBlank(templateId)) {
+ throw new WxSubscribeException("获取模板id失败", "", openId);
+ }
+
+ //判断用户是否有次数
+ Integer count = null;
+ if (WxmpMessageConstant.RESI.equals(clientType)) {
+ count = wxmpResiUserSubscribeDao.getResiSubscribeInfo(openId, templateId, customerId);
+ } else if (WxmpMessageConstant.WORK.equals(clientType)) {
+ count = wxmpWorkUserSubscribeDao.getWorkSubscribeInfo(openId, templateId, customerId);
+ }
+ if (count == null) {
+ //用户未订阅
+ throw new WxSubscribeException("用户未订阅", templateId, openId);
+ }
+ if (count == 0) {
+ throw new WxSubscribeException("用户可用额度不足", templateId, 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()).substring(0, WxmpMessageConstant.TITLE_LIMIT)));
+ data.put(WxmpMessageConstant.MESSAGE_CONTENT, new JSONObject().put("value", msg.getMessageContent().substring(0, WxmpMessageConstant.MESSAGE_CONTENT_LIMIT)));
+ data.put(WxmpMessageConstant.MESSAGE_TIME, new JSONObject().put("value", new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date())));
+ jsonObject.put(WxmpMessageConstant.DATA, data);
+ EnvEnum envEnum = EnvEnum.getCurrentEnv();
+ //选填项
+ if (WxmpMessageConstant.RESI.equals(clientType)) {
+ jsonObject.put(WxmpMessageConstant.PAGE, WxmpMessageConstant.PAGE_RESI);
+ } else if (WxmpMessageConstant.WORK.equals(clientType)) {
+ jsonObject.put(WxmpMessageConstant.PAGE, WxmpMessageConstant.PAGE_WORK);
+ }
+ //开发环境
+ if ("dev".equals(envEnum.getCode())) {
+ jsonObject.put(WxmpMessageConstant.MINIPROGRAM_STATE, WxmpMessageConstant.STATE_DEV);
+ }
+ //测试环境
+ if ("test".equals(envEnum.getCode())) {
+ jsonObject.put(WxmpMessageConstant.MINIPROGRAM_STATE, WxmpMessageConstant.STATE_TEST);
+ }
+
+ 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, templateId, customerId, 1);
+ } else if (WxmpMessageConstant.WORK.equals(clientType)) {
+ decrease = wxmpWorkUserSubscribeDao.decreaseWorkSubscribeCount(openId, templateId, customerId, 1);
+ }
+ if (decrease == 0) {
+ logger.error("消息{}发送成功但订阅条数-1失败", JSON.toJSONString(msg));
+ }
+ //存表
+ int saveRes = wxmpMsgSendRecordService.saveRecord(initRecord(msg, templateId, 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, templateId, customerId);
+ } else if (WxmpMessageConstant.WORK.equals(clientType)) {
+ clear = wxmpWorkUserSubscribeDao.clearWorkSubscribeCount(openId, templateId, customerId);
+ }
+ if (clear == 0) {
+ logger.error("消息{}发送失败且清空订阅条数失败", JSON.toJSONString(msg));
+ }
+ }
+
+ //抛出错误
+ throw new WxSubscribeException(String.valueOf(resultMap.get(WxmpMessageConstant.ERR_MSG)),templateId, 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).getTemplateId(), ((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 templateId, String openId, String status) {
- WxmpMsgSendRecordEntity wxmpMsgSendRecordEntity = new WxmpMsgSendRecordEntity();
- wxmpMsgSendRecordEntity.setCustomerId(msg.getCustomerId());
- wxmpMsgSendRecordEntity.setClientType(msg.getClientType());
- wxmpMsgSendRecordEntity.setTemplateId(templateId);
- wxmpMsgSendRecordEntity.setUserId(msg.getUserId());
- wxmpMsgSendRecordEntity.setWxOpenId(openId);
- wxmpMsgSendRecordEntity.setBehaviorType(msg.getBehaviorType());
- wxmpMsgSendRecordEntity.setTitle("您有一条" + msg.getBehaviorType());
- wxmpMsgSendRecordEntity.setMessageContent(msg.getMessageContent());
- wxmpMsgSendRecordEntity.setMessageTime(new Date());
- wxmpMsgSendRecordEntity.setResult(status);
- return wxmpMsgSendRecordEntity;
- }
+ if (e instanceof WxSubscribeException) {
+ //存表
+ WxmpMsgSendRecordEntity wxmpMsgSendRecordEntity = initRecord(msg, ((WxSubscribeException) e).getTemplateId(), ((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 templateId, String openId, String status) {
+ WxmpMsgSendRecordEntity wxmpMsgSendRecordEntity = new WxmpMsgSendRecordEntity();
+ wxmpMsgSendRecordEntity.setCustomerId(msg.getCustomerId());
+ wxmpMsgSendRecordEntity.setClientType(msg.getClientType());
+ wxmpMsgSendRecordEntity.setTemplateId(templateId);
+ wxmpMsgSendRecordEntity.setUserId(msg.getUserId());
+ wxmpMsgSendRecordEntity.setWxOpenId(openId);
+ wxmpMsgSendRecordEntity.setBehaviorType(msg.getBehaviorType());
+ wxmpMsgSendRecordEntity.setTitle("您有一条" + msg.getBehaviorType());
+ wxmpMsgSendRecordEntity.setMessageContent(msg.getMessageContent());
+ wxmpMsgSendRecordEntity.setMessageTime(new Date());
+ wxmpMsgSendRecordEntity.setResult(status);
+ return wxmpMsgSendRecordEntity;
+ }
/**
* @return
@@ -319,4 +332,150 @@ public class WxmpMessageServiceImpl implements WxmpMessageService {
return resultList;
}
-}
+ @Override
+ public WxMsgAuthInfoResultDTO getAuthorizationInfo(String customerId, String clientType, String loginUserId, String templateId) {
+ return msgSubscribeStatusDao.getUserSubscribeStatusDTO(loginUserId, customerId, clientType, templateId);
+ }
+
+ @Override
+ public void saveWxSubscribe(String customerId, String clientType, String templateId, String wxAlwaysVisit,
+ String wxSubscribeStatus, String behaviorType, String userId) {
+
+ String openId;
+ // 1. 用户订阅信息表新增或者更新
+ if (AppClientConstant.APP_GOV.equals(clientType)) {
+ // 工作端
+ openId = getWorkOpenId(userId);
+ saveWxWorkSubscribe(customerId, templateId, wxSubscribeStatus, userId, openId);
+ } else {
+ //居民端
+ openId = getResiOpenId(userId);
+ saveWxResiSubscribe(customerId, templateId, wxSubscribeStatus, userId, openId);
+ }
+
+ // 2.保存行为记录
+ WxmpUserSubscribeRecordEntity record = new WxmpUserSubscribeRecordEntity();
+ record.setBehaviorType(behaviorType);
+ record.setCustomerId(customerId);
+ record.setTemplateId(templateId);
+ record.setUserId(userId);
+ record.setWxAlwaysVisit(wxAlwaysVisit);
+ record.setWxOpenId(openId);
+ record.setWxSubscribeStatus(wxSubscribeStatus);
+ subscribeRecordDao.insert(record);
+
+ // 3.更新用户模板订阅状态
+ WxmpTemplateMsgSubscribeStatusEntity subscribeStatusEntity = msgSubscribeStatusDao.getUserSubscribeStatusEntity(userId, customerId, clientType, templateId);
+ if (subscribeStatusEntity == null) {
+ subscribeStatusEntity = new WxmpTemplateMsgSubscribeStatusEntity();
+ subscribeStatusEntity.setUserId(userId);
+ subscribeStatusEntity.setWxSubscribeStatus(wxSubscribeStatus);
+ subscribeStatusEntity.setTemplateId(templateId);
+ subscribeStatusEntity.setWxAlwaysVisit(wxAlwaysVisit);
+ subscribeStatusEntity.setCustomerId(customerId);
+ subscribeStatusEntity.setClientType(clientType);
+ msgSubscribeStatusDao.insert(subscribeStatusEntity);
+ return;
+ }
+
+ subscribeStatusEntity.setWxAlwaysVisit(wxAlwaysVisit);
+ subscribeStatusEntity.setWxSubscribeStatus(wxSubscribeStatus);
+
+ msgSubscribeStatusDao.updateById(subscribeStatusEntity);
+ }
+
+ /**
+ * @Description 保存居民端微信订阅信息
+ * @return void
+ * @author wxz
+ * @date 2020.10.22 09:56
+ */
+ private void saveWxResiSubscribe(String customerId, String templateId, String wxSubscribeStatus, String userId, String openId) {
+
+ WxmpResiUserSubscribeEntity wxResiSubscribe = resiUserSubscribeDao.getWxResiSubscribe(userId, customerId, templateId);
+ if (wxResiSubscribe == null) {
+ WxmpResiUserSubscribeEntity insert = new WxmpResiUserSubscribeEntity();
+ insert.setCount(WxMsgTemplateSubscribeStatus.SUBSCRIBE.equals(wxSubscribeStatus) ? 1 : 0);
+ insert.setCustomerId(customerId);
+ insert.setTemplateId(templateId);
+ insert.setUserId(userId);
+ insert.setWxOpenId(openId);
+ insert.setWxSubscribeStatus(wxSubscribeStatus);
+ return;
+ }
+
+ if (WxMsgTemplateSubscribeStatus.SUBSCRIBE.equals(wxSubscribeStatus)) {
+ resiUserSubscribeDao.incrSubscribeCount(1, userId, customerId, templateId, openId);
+ } else {
+ resiUserSubscribeDao.unSubscribe(userId, customerId, templateId, openId);
+ }
+ }
+
+ /**
+ * @Description 保存工作端订阅信息
+ * @return void
+ * @author wxz
+ * @date 2020.10.22 14:21
+ */
+ private void saveWxWorkSubscribe(String customerId, String templateId, String wxSubscribeStatus, String userId, String openId) {
+ WxmpWorkUserSubscribeEntity wxWorkSubscribe = wxmpWorkUserSubscribeDao.getWxWorkSubscribe(userId, customerId, templateId);
+ if (wxWorkSubscribe == null) {
+ WxmpWorkUserSubscribeEntity insert = new WxmpWorkUserSubscribeEntity();
+ insert.setCount(WxMsgTemplateSubscribeStatus.SUBSCRIBE.equals(wxSubscribeStatus) ? 1 : 0);
+ insert.setCustomerId(customerId);
+ insert.setTemplateId(templateId);
+ insert.setUserId(userId);
+ insert.setWxOpenId(openId);
+ insert.setWxSubscribeStatus(wxSubscribeStatus);
+ wxmpWorkUserSubscribeDao.insert(insert);
+ return ;
+ }
+
+ if (WxMsgTemplateSubscribeStatus.SUBSCRIBE.equals(wxSubscribeStatus)) {
+ wxmpWorkUserSubscribeDao.incrSubscribeCount(1, userId, customerId, templateId, openId);
+ } else {
+ wxmpWorkUserSubscribeDao.unSubscribe(userId, customerId, templateId, openId);
+ }
+
+ }
+
+ /**
+ * @Description 根据UserId查询居民OpenId
+ * @return string
+ * @author wxz
+ * @date 2020.10.22 13:35
+ */
+ public String getResiOpenId(String userId) {
+ UserBasicInfoFormDTO bif = new UserBasicInfoFormDTO();
+ bif.setUserId(userId);
+ Result result = userOpenFeignClient.getUserBasicInfo(bif);
+ if (!result.success()) {
+ throw new RenException(String.format("查询居民OpenId失败,userId:%s,错误信息:%s",userId , result.getInternalMsg()));
+ }
+ UserBasicInfo userBasicInfo = result.getData();
+ if (userBasicInfo == null) {
+ throw new RenException(String.format("根据userId:%s,没有查询到居民OpenId",userId));
+ }
+ return userBasicInfo.getOpenId();
+ }
+
+ /**
+ * @Description 查询工作人员openId
+ * @return java.lang.String
+ * @author wxz
+ * @date 2020.10.22 14:46
+ */
+ public String getWorkOpenId(String userId) {
+ StaffBasicInfoFormDTO form = new StaffBasicInfoFormDTO();
+ form.setStaffId(userId);
+ Result result = userOpenFeignClient.getStaffBasicInfo(form);
+ if (!result.success()) {
+ throw new RenException(String.format("查询工作人员OpenId失败,userId:%s,错误信息:%s",userId , result.getInternalMsg()));
+ }
+ StaffBasicInfo staffBasicInfo = result.getData();
+ if (staffBasicInfo == null) {
+ throw new RenException(String.format("根据userId:%s,没有查询到工作人员OpenId",userId));
+ }
+ return staffBasicInfo.getOpenId();
+ }
+}
\ 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 2a1df8a057..099108d016 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
@@ -50,4 +50,46 @@
limit 1
+
+
+ update wxmp_resi_user_subscribe
+ set COUNT=COUNT + #{incr},
+ WX_SUBSCRIBE_STATUS = 'subscribe'
+ where USER_ID = #{userId}
+ and CUSTOMER_ID = #{customerId}
+ and TEMPLATE_ID = #{templateId}
+ and WX_OPEN_ID = #{resiOpenId}
+
+
+
+
+ update wxmp_resi_user_subscribe
+ set WX_SUBSCRIBE_STATUS = 'unsubscribe',
+ COUNT = 0
+ where USER_ID = #{userId}
+ and CUSTOMER_ID = #{customerId}
+ and TEMPLATE_ID = #{templateId}
+ and WX_OPEN_ID = #{resiOpenId}
+
+
+
+
diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpTemplateMsgSubscribeStatusDao.xml b/epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpTemplateMsgSubscribeStatusDao.xml
index d1fde2360a..fd51895084 100644
--- a/epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpTemplateMsgSubscribeStatusDao.xml
+++ b/epmet-module/epmet-message/epmet-message-server/src/main/resources/mapper/WxmpTemplateMsgSubscribeStatusDao.xml
@@ -22,6 +22,7 @@
where USER_ID = #{userId}
and CUSTOMER_ID = #{customerId}
and CLIENT_TYPE = #{clientType}
+ and TEMPLATE_ID = #{templateId}
\ No newline at end of file
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 efa31174f9..7c9e547118 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
@@ -2,6 +2,15 @@
+
+ update wxmp_work_user_subscribe
+ set COUNT=COUNT + #{incr},
+ WX_SUBSCRIBE_STATUS = 'subscribe'
+ where USER_ID = #{userId}
+ and CUSTOMER_ID = #{customerId}
+ and TEMPLATE_ID = #{templateId}
+ and WX_OPEN_ID = #{openId}
+
+
+
+ update wxmp_work_user_subscribe
+ set WX_SUBSCRIBE_STATUS = 'unsubscribe',
+ COUNT = 0
+ where USER_ID = #{userId}
+ and CUSTOMER_ID = #{customerId}
+ and TEMPLATE_ID = #{templateId}
+ and WX_OPEN_ID = #{resiOpenId}
+
+
+