diff --git a/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/form/TemplateListFormDTO.java b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/form/TemplateListFormDTO.java new file mode 100644 index 0000000000..87b3ff0cad --- /dev/null +++ b/epmet-module/epmet-message/epmet-message-client/src/main/java/com/epmet/dto/form/TemplateListFormDTO.java @@ -0,0 +1,31 @@ +package com.epmet.dto.form; + +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import java.io.Serializable; + +/** + * @Description 获取客户小程序模板列表-接口入参 + * @Author sun + */ +@Data +public class TemplateListFormDTO implements Serializable { + + /** + * 客户Id + */ + @NotBlank(message="客户Id不能为空", groups = {TemplateListFormDTO.AddUserInternalGroup.class}) + private String customerId; + /** + * 小程序Id + */ + @NotBlank(message="小程序appId不能为空", groups = {TemplateListFormDTO.AddUserInternalGroup.class}) + private String appId; + /** + * 模板类型(站内信提醒) + */ + private String templateType; + public interface AddUserInternalGroup {} +} + 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 index 9a55b17386..1dc30e2f8c 100644 --- 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 @@ -48,15 +48,4 @@ public class WxSubscribeMessageFormDTO implements Serializable { @NotBlank(message = "消息内容不能为空") private String messageContent; - /** - * 消息时间 - */ - @NotBlank(message = "消息时间不能为空") - private String messageTime; - - /** - * 所需下发的订阅模板id - */ - @NotBlank(message = "模板id不能为空") - private String templateId; } diff --git a/epmet-module/epmet-message/epmet-message-server/pom.xml b/epmet-module/epmet-message/epmet-message-server/pom.xml index e692dd2a18..394ad79511 100644 --- a/epmet-module/epmet-message/epmet-message-server/pom.xml +++ b/epmet-module/epmet-message/epmet-message-server/pom.xml @@ -101,6 +101,12 @@ epmet-user-client 2.0.0 + + com.epmet + epmet-third-client + 2.0.0 + compile + 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 index 992146e6ab..667a8b3bfc 100644 --- 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 @@ -34,8 +34,12 @@ public interface WxmpMessageConstant { String TITLE = "title"; + int TITLE_LIMIT = 20; + String MESSAGE_CONTENT = "message_content"; + int MESSAGE_CONTENT_LIMIT = 20; + String MESSAGE_TIME = "message_time"; String DATA = "data"; 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 23e854f9de..7e06c80b0a 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 @@ -20,11 +20,16 @@ package com.epmet.controller; 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.GetTemplateListFormDTO; import com.epmet.dto.form.WxMsgAuthInfoFormDTO; import com.epmet.dto.form.WxSubscribeMessageFormDTO; +import com.epmet.dto.result.GetTemplateListResultDTO; import com.epmet.service.WxmpMessageService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.*; +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 java.util.List; @@ -72,4 +77,16 @@ public class WxmpMessageController { wxmpMessageService.sendWxSubscribeMessage(msgList); return new Result(); } + + /** + * @return + * @Description 居民端、工作端-获取客户小程序模板列表 + * @author sun + */ + @PostMapping("templatelist") + public Result> templateList(@RequestBody GetTemplateListFormDTO formDTO) { + ValidatorUtils.validateEntity(formDTO, GetTemplateListFormDTO.AddUserInternalGroup.class); + return new Result>().ok(wxmpMessageService.templateList(formDTO)); + } + } 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 d2ec0c8443..c1a6e83d2f 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 @@ -64,4 +64,14 @@ public interface WxmpResiUserSubscribeDao extends BaseDao msgList); + + /** + * @return + * @Description 居民端、工作端-获取客户小程序模板列表 + * @author sun + */ + List templateList(GetTemplateListFormDTO formDTO); } diff --git a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/impl/WxmpMessageServiceImpl.java b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/impl/WxmpMessageServiceImpl.java index 1a17894174..bf9d8ed39c 100644 --- a/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/impl/WxmpMessageServiceImpl.java +++ b/epmet-module/epmet-message/epmet-message-server/src/main/java/com/epmet/service/impl/WxmpMessageServiceImpl.java @@ -21,16 +21,21 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.epmet.commons.tools.constant.NumConstant; import com.epmet.commons.tools.enums.EnvEnum; +import com.epmet.commons.tools.exception.RenException; import com.epmet.commons.tools.exception.ValidateException; +import com.epmet.commons.tools.utils.ConvertUtils; import com.epmet.commons.tools.utils.HttpClientManager; +import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.validator.ValidatorUtils; import com.epmet.constant.WxmpMessageConstant; import com.epmet.dao.WxmpResiUserSubscribeDao; import com.epmet.dao.WxmpTemplateMsgSubscribeStatusDao; import com.epmet.dao.WxmpWorkUserSubscribeDao; +import com.epmet.dto.form.GetTemplateListFormDTO; import com.epmet.dto.form.StaffBasicInfoFormDTO; import com.epmet.dto.form.UserBasicInfoFormDTO; import com.epmet.dto.form.WxSubscribeMessageFormDTO; +import com.epmet.dto.result.GetTemplateListResultDTO; import com.epmet.entity.WxmpMsgSendRecordEntity; import com.epmet.entity.WxmpTemplateMsgSubscribeStatusEntity; import com.epmet.exception.WxSubscribeException; @@ -60,225 +65,258 @@ import java.util.Map; */ @Service public class WxmpMessageServiceImpl implements WxmpMessageService { - private Logger logger = LoggerFactory.getLogger(getClass()); + private Logger logger = LoggerFactory.getLogger(getClass()); - @Autowired - private WxmpTemplateMsgSubscribeStatusDao msgSubscribeStatusDao; + @Autowired + private WxmpTemplateMsgSubscribeStatusDao msgSubscribeStatusDao; - @Autowired - private EpmetUserOpenFeignClient epmetUserOpenFeignClient; + @Autowired + private EpmetUserOpenFeignClient epmetUserOpenFeignClient; - @Autowired - private WxmpMessageRedis wxmpMessageRedis; + @Autowired + private WxmpMessageRedis wxmpMessageRedis; - @Resource - private WxmpResiUserSubscribeDao wxmpResiUserSubscribeDao; + @Resource + private WxmpResiUserSubscribeDao wxmpResiUserSubscribeDao; - @Resource - private WxmpWorkUserSubscribeDao wxmpWorkUserSubscribeDao; + @Resource + private WxmpWorkUserSubscribeDao wxmpWorkUserSubscribeDao; - @Autowired - private WxmpMsgSendRecordService wxmpMsgSendRecordService; + @Autowired + private WxmpMsgSendRecordService wxmpMsgSendRecordService; - /** - * @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) { - WxmpTemplateMsgSubscribeStatusEntity userSubscribeStatusEntity = msgSubscribeStatusDao.getUserSubscribeStatusEntity(userId, customerId, clientType); - if (userSubscribeStatusEntity != null) { - userSubscribeStatusEntity.setAlwaysVisit(alwaysVisit); - userSubscribeStatusEntity.setSubscribeStatus(subscribeStatus); - msgSubscribeStatusDao.updateById(userSubscribeStatusEntity); - return; - } + /** + * @return void + * @Description 保存系统授权信息 + * @author wxz + * @date 2020.10.21 17:29 + */ + @Override + public void saveSysAuthorizeInfo(String customerId, String clientType, String alwaysVisit, String subscribeStatus, String userId) { + WxmpTemplateMsgSubscribeStatusEntity userSubscribeStatusEntity = msgSubscribeStatusDao.getUserSubscribeStatusEntity(userId, customerId, clientType); + if (userSubscribeStatusEntity != null) { + userSubscribeStatusEntity.setAlwaysVisit(alwaysVisit); + userSubscribeStatusEntity.setSubscribeStatus(subscribeStatus); + msgSubscribeStatusDao.updateById(userSubscribeStatusEntity); + return; + } - userSubscribeStatusEntity = new WxmpTemplateMsgSubscribeStatusEntity(); - userSubscribeStatusEntity.setCustomerId(customerId); - userSubscribeStatusEntity.setClientType(clientType); - userSubscribeStatusEntity.setAlwaysVisit(alwaysVisit); - userSubscribeStatusEntity.setSubscribeStatus(subscribeStatus); - userSubscribeStatusEntity.setUserId(userId); - msgSubscribeStatusDao.insert(userSubscribeStatusEntity); - } + userSubscribeStatusEntity = new WxmpTemplateMsgSubscribeStatusEntity(); + userSubscribeStatusEntity.setCustomerId(customerId); + userSubscribeStatusEntity.setClientType(clientType); + 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 = 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服务失败",""); - } + /** + * @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获取失败",""); + if (StringUtils.isBlank(openId)) { + throw new WxSubscribeException("openId获取失败", "", ""); } - //获取accessToken - StringBuilder key = new StringBuilder(msg.getCustomerId()).append(":").append(msg.getClientType()); - Map authorizerRefreshToken = new HashMap<>(); + //获取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); - } + 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, templateId, customerId); - } else if (WxmpMessageConstant.WORK.equals(clientType)) { - count = wxmpWorkUserSubscribeDao.getWorkSubscribeInfo(openId, templateId, customerId); - } - if (count == null) { - //用户未订阅 - throw new WxSubscribeException("用户未订阅",openId); - } - if (count == 0) { - throw new WxSubscribeException("用户可用额度不足",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); + } - //发送消息 - 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); - 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); - } + //判断用户是否有次数 + 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); + } - 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)) { - //发送成功 + //发送消息 + 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); + } - //订阅条数-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,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)); - } - } + 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)) { + //发送成功 - //抛出错误 - throw new WxSubscribeException(String.valueOf(resultMap.get(WxmpMessageConstant.ERR_MSG)),openId); - } + //订阅条数-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)); + } + } - succecssCount++; - } catch (Exception e) { - String errMsg = e.getMessage(); - //ValidateException错误信息为getMsg + //抛出错误 + 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).getOpenId(), WxmpMessageConstant.ERROR); - wxmpMsgSendRecordEntity.setReason(errMsg); - int saveRes = wxmpMsgSendRecordService.saveRecord(wxmpMsgSendRecordEntity); - if (saveRes == 0) { - logger.error("消息{}发送失败且存入记录表失败", JSON.toJSONString(msg)); - } - } + 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.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 + * @Description 居民端、工作端-获取客户小程序模板列表 + * @author sun + */ + @Override + public List templateList(GetTemplateListFormDTO formDTO) { + GetTemplateListFormDTO dto = ConvertUtils.sourceToTarget(formDTO, GetTemplateListFormDTO.class); + String url = "https://epmet-cloud.elinkservice.cn/api/third/personaltemplate/templatelist"; + //String url = "http://localhost:8080/api/third/personaltemplate/templatelist"; + String data = HttpClientManager.getInstance().sendPostByJSON(url, JSON.toJSONString(dto)).getData(); + logger.info("ThirdLoginServiceImpl.getUserWeChat:httpclient->url:"+url+",结果->"+data); + JSONObject toResult = JSON.parseObject(data); + Result mapToResult = ConvertUtils.mapToEntity(toResult, Result.class); + if (null != toResult.get("code")) { + mapToResult.setCode(((Integer) toResult.get("code")).intValue()); } - 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()); - try { - wxmpMsgSendRecordEntity.setMessageTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(msg.getMessageTime())); - } catch (ParseException e) { - wxmpMsgSendRecordEntity.setMessageTime(new Date()); + if (!mapToResult.success()) { + logger.error("调用epmet_third服务获取小程序消息订阅模板数据失败"); + throw new RenException(mapToResult.getCode()); } - wxmpMsgSendRecordEntity.setResult(status); - return wxmpMsgSendRecordEntity; + List resultList = (List) mapToResult.getData(); + return resultList; } + } 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 2b97a22eec..2a1df8a057 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 @@ -37,4 +37,17 @@ 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 4673179dee..efa31174f9 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 @@ -34,4 +34,16 @@ and template_id = #{templateId} and wx_open_id = #{openId} + diff --git a/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/GetTemplateListFormDTO.java b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/GetTemplateListFormDTO.java new file mode 100644 index 0000000000..09fb259376 --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/GetTemplateListFormDTO.java @@ -0,0 +1,31 @@ +package com.epmet.dto.form; + +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import java.io.Serializable; + +/** + * @Description 获取客户小程序模板列表-接口入参 + * @Author sun + */ +@Data +public class GetTemplateListFormDTO implements Serializable { + + /** + * 客户Id + */ + @NotBlank(message="客户Id不能为空", groups = {AddUserInternalGroup.class}) + private String customerId; + /** + * 小程序Id + */ + @NotBlank(message="小程序appId不能为空", groups = {AddUserInternalGroup.class}) + private String appId; + /** + * 模板类型(站内信提醒) + */ + private String templateType; + public interface AddUserInternalGroup {} +} + diff --git a/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/result/GetTemplateListResultDTO.java b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/result/GetTemplateListResultDTO.java new file mode 100644 index 0000000000..d95e482c6f --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/result/GetTemplateListResultDTO.java @@ -0,0 +1,25 @@ +package com.epmet.dto.result; + +import lombok.Data; + +import java.io.Serializable; + +/** + * @Description 获取客户小程序模板列表-接口返参 + * @Author sun + */ +@Data +public class GetTemplateListResultDTO implements Serializable { + private static final long serialVersionUID = 6856602932571839314L; + + /** + * 模板Id + */ + private String templateId; + + /** + * 模板类型(站内信提醒) + */ + private String templateType; + +} diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/controller/PersonalTemplateController.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/controller/PersonalTemplateController.java new file mode 100644 index 0000000000..bec7a2d3a8 --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/controller/PersonalTemplateController.java @@ -0,0 +1,37 @@ +package com.epmet.controller; + +import com.epmet.commons.tools.utils.Result; +import com.epmet.dto.form.GetTemplateListFormDTO; +import com.epmet.dto.result.GetTemplateListResultDTO; +import com.epmet.service.PersonalTemplateService; +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 java.util.List; + +/** + * @author generator generator@elink-cn.com + * @since v1.0.0 2020-09-09 + */ +@RestController +@RequestMapping("personaltemplate") +public class PersonalTemplateController { + + @Autowired + private PersonalTemplateService personalTemplateService; + + /** + * @return + * @Description 居民端、工作端-获取客户小程序模板列表 + * @author sun + */ + @PostMapping("templatelist") + public Result> templateList(@RequestBody GetTemplateListFormDTO formDTO) { + return new Result>().ok(personalTemplateService.templateList(formDTO)); + } + + +} diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/PersonalTemplateDao.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/PersonalTemplateDao.java index bd77f6ffea..a92981e0d4 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/PersonalTemplateDao.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/PersonalTemplateDao.java @@ -19,7 +19,9 @@ package com.epmet.dao; import com.epmet.commons.mybatis.dao.BaseDao; import com.epmet.dto.PersonalTemplateDTO; +import com.epmet.dto.form.GetTemplateListFormDTO; import com.epmet.dto.result.CustomerTempResultDTO; +import com.epmet.dto.result.GetTemplateListResultDTO; import com.epmet.dto.result.TemplateDTO; import com.epmet.entity.PersonalTemplateEntity; import org.apache.ibatis.annotations.Mapper; @@ -76,4 +78,11 @@ public interface PersonalTemplateDao extends BaseDao { */ List selectListByCustomerId(@Param("appId") String appId, @Param("customerId") String customerId, @Param("clientType") String clientType); + + /** + * @return + * @Description 居民端、工作端-获取客户小程序模板列表 + * @author sun + */ + List selectTemplateList(GetTemplateListFormDTO formDTO); } \ No newline at end of file diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/PersonalTemplateService.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/PersonalTemplateService.java index 447e6dd388..9f79ece63c 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/PersonalTemplateService.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/PersonalTemplateService.java @@ -20,6 +20,8 @@ package com.epmet.service; import com.epmet.commons.mybatis.service.BaseService; import com.epmet.commons.tools.page.PageData; import com.epmet.dto.PersonalTemplateDTO; +import com.epmet.dto.form.GetTemplateListFormDTO; +import com.epmet.dto.result.GetTemplateListResultDTO; import com.epmet.dto.result.TemplateDTO; import com.epmet.entity.PersonalTemplateEntity; @@ -123,4 +125,11 @@ public interface PersonalTemplateService extends BaseService getListByCustomer(String appId, String customerId, String clientType); + + /** + * @return + * @Description 居民端、工作端-获取客户小程序模板列表 + * @author sun + */ + List templateList(GetTemplateListFormDTO formDTO); } \ No newline at end of file diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/PersonalTemplateServiceImpl.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/PersonalTemplateServiceImpl.java index f7b5589f43..8a5764427a 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/PersonalTemplateServiceImpl.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/PersonalTemplateServiceImpl.java @@ -25,6 +25,8 @@ import com.epmet.commons.tools.page.PageData; import com.epmet.commons.tools.utils.ConvertUtils; import com.epmet.dao.PersonalTemplateDao; import com.epmet.dto.PersonalTemplateDTO; +import com.epmet.dto.form.GetTemplateListFormDTO; +import com.epmet.dto.result.GetTemplateListResultDTO; import com.epmet.dto.result.TemplateDTO; import com.epmet.entity.PersonalTemplateEntity; import com.epmet.service.PersonalTemplateService; @@ -116,4 +118,15 @@ public class PersonalTemplateServiceImpl extends BaseServiceImpl templateList(GetTemplateListFormDTO formDTO) { + //根据客户Id、appId、模板类型查询小程序订阅消息模板列表 + return baseDao.selectTemplateList(formDTO); + } + } \ No newline at end of file diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/PersonalTemplateDao.xml b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/PersonalTemplateDao.xml index f3b46b3c42..0b3f7ace51 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/PersonalTemplateDao.xml +++ b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/PersonalTemplateDao.xml @@ -75,5 +75,18 @@ ON t1.PRI_TMPL_ID = t2.PID + \ No newline at end of file diff --git a/epmet-module/resi-group/resi-group-server/src/main/java/com/epmet/modules/constant/UserMessageConstant.java b/epmet-module/resi-group/resi-group-server/src/main/java/com/epmet/modules/constant/UserMessageConstant.java index 71bef9e2d4..f9b97bfb9f 100644 --- a/epmet-module/resi-group/resi-group-server/src/main/java/com/epmet/modules/constant/UserMessageConstant.java +++ b/epmet-module/resi-group/resi-group-server/src/main/java/com/epmet/modules/constant/UserMessageConstant.java @@ -59,4 +59,9 @@ public interface UserMessageConstant { */ String CREATION_OF_GROUP_MESSAGE_TEMPLATE = "%s%s申请创建小组【%s】,请审核。"; + /** + * 组长审核入组申请时的微信订阅behavior + */ + String WX_APPLY_GROUP_BEHAVIOR = "小组消息"; + } diff --git a/epmet-module/resi-group/resi-group-server/src/main/java/com/epmet/modules/member/service/impl/ResiGroupMemberServiceImpl.java b/epmet-module/resi-group/resi-group-server/src/main/java/com/epmet/modules/member/service/impl/ResiGroupMemberServiceImpl.java index 803ea51d94..db6422eee4 100644 --- a/epmet-module/resi-group/resi-group-server/src/main/java/com/epmet/modules/member/service/impl/ResiGroupMemberServiceImpl.java +++ b/epmet-module/resi-group/resi-group-server/src/main/java/com/epmet/modules/member/service/impl/ResiGroupMemberServiceImpl.java @@ -34,6 +34,7 @@ import com.epmet.constant.ReadFlagConstant; import com.epmet.dto.form.UserMessageFormDTO; import com.epmet.dto.form.UserResiInfoFormDTO; import com.epmet.dto.form.UserResiInfoListFormDTO; +import com.epmet.dto.form.WxSubscribeMessageFormDTO; import com.epmet.dto.result.UserResiInfoResultDTO; import com.epmet.feign.EpmetMessageOpenFeignClient; import com.epmet.modules.constant.GroupMemberConstant; @@ -435,7 +436,7 @@ public class ResiGroupMemberServiceImpl extends BaseServiceImpl msgList = new ArrayList<>(); + msgList.add(wxSubscribeMessageFormDTO); + epmetMessageOpenFeignClient.sendWxSubscribeMessage(msgList); } /** @@ -500,7 +511,7 @@ public class ResiGroupMemberServiceImpl extends BaseServiceImpl msgList = new ArrayList<>(); + msgList.add(wxSubscribeMessageFormDTO); + epmetMessageOpenFeignClient.sendWxSubscribeMessage(msgList); } /** diff --git a/epmet-module/resi-partymember/resi-partymember-server/src/main/java/com/epmet/modules/partymember/service/impl/PartyMemberConfirmServiceImpl.java b/epmet-module/resi-partymember/resi-partymember-server/src/main/java/com/epmet/modules/partymember/service/impl/PartyMemberConfirmServiceImpl.java index 7713b97928..72dd1e18ed 100644 --- a/epmet-module/resi-partymember/resi-partymember-server/src/main/java/com/epmet/modules/partymember/service/impl/PartyMemberConfirmServiceImpl.java +++ b/epmet-module/resi-partymember/resi-partymember-server/src/main/java/com/epmet/modules/partymember/service/impl/PartyMemberConfirmServiceImpl.java @@ -23,6 +23,7 @@ import com.epmet.modules.feign.ResiGroupFeignClient; import com.epmet.modules.partymember.entity.*; import com.epmet.modules.partymember.redis.PartymemberInfoRedis; import com.epmet.modules.partymember.service.*; +import com.epmet.modules.warmhearted.constant.ResiWarmUserMessageConstant; import com.epmet.modules.warmhearted.constant.ResiWarmheartedConstant; import com.epmet.modules.warmhearted.constant.ResiWarmheartedVisitConstant; import com.epmet.redis.ResiPartyMemberRedis; @@ -644,6 +645,17 @@ public class PartyMemberConfirmServiceImpl implements PartyMemberConfirmService String messageContent = String.format(userMsg, gridName); userMessageFormDTO.setMessageContent(messageContent); userMessageFormDTO.setReadFlag(ReadFlagConstant.UN_READ); + + //发送微信订阅消息 + WxSubscribeMessageFormDTO wxSubscribeMessageFormDTO = new WxSubscribeMessageFormDTO(); + wxSubscribeMessageFormDTO.setCustomerId(formDTO.getCustomerId()); + wxSubscribeMessageFormDTO.setUserId(formDTO.getUserId()); + wxSubscribeMessageFormDTO.setClientType(AppClientConstant.APP_RESI); + wxSubscribeMessageFormDTO.setBehaviorType(ResiWarmUserMessageConstant.WX_WARMHEARTED_BEHAVIOR); + wxSubscribeMessageFormDTO.setMessageContent(messageContent); + List msgList = new ArrayList<>(); + msgList.add(wxSubscribeMessageFormDTO); + epmetMessageOpenFeignClient.sendWxSubscribeMessage(msgList); //保存消息 return epmetMessageOpenFeignClient.saveUserMessage(userMessageFormDTO); } diff --git a/epmet-module/resi-partymember/resi-partymember-server/src/main/java/com/epmet/modules/warmhearted/constant/ResiWarmUserMessageConstant.java b/epmet-module/resi-partymember/resi-partymember-server/src/main/java/com/epmet/modules/warmhearted/constant/ResiWarmUserMessageConstant.java index 15cb42bd63..13e1231a2a 100644 --- a/epmet-module/resi-partymember/resi-partymember-server/src/main/java/com/epmet/modules/warmhearted/constant/ResiWarmUserMessageConstant.java +++ b/epmet-module/resi-partymember/resi-partymember-server/src/main/java/com/epmet/modules/warmhearted/constant/ResiWarmUserMessageConstant.java @@ -26,4 +26,9 @@ public interface ResiWarmUserMessageConstant { */ String AUDIT_REJECT_MSG = "您好,您申请的%s热心居民,已被驳回,原因:%s"; + /** + * 热心居民申请-微信订阅behavior + */ + String WX_WARMHEARTED_BEHAVIOR = "热心居民申请消息"; + } diff --git a/epmet-module/resi-partymember/resi-partymember-server/src/main/java/com/epmet/modules/warmhearted/service/impl/ResiWarmheartedApplyServiceImpl.java b/epmet-module/resi-partymember/resi-partymember-server/src/main/java/com/epmet/modules/warmhearted/service/impl/ResiWarmheartedApplyServiceImpl.java index c00077dd1f..26beee052c 100644 --- a/epmet-module/resi-partymember/resi-partymember-server/src/main/java/com/epmet/modules/warmhearted/service/impl/ResiWarmheartedApplyServiceImpl.java +++ b/epmet-module/resi-partymember/resi-partymember-server/src/main/java/com/epmet/modules/warmhearted/service/impl/ResiWarmheartedApplyServiceImpl.java @@ -39,6 +39,7 @@ import com.epmet.dto.form.*; import com.epmet.dto.result.AgencyAndStaffsResultDTO; import com.epmet.dto.result.GovStaffRoleResultDTO; import com.epmet.dto.result.UserResiInfoResultDTO; +import com.epmet.feign.EpmetMessageOpenFeignClient; import com.epmet.modules.feign.EpmetMessageFeignClient; import com.epmet.modules.feign.EpmetUserFeignClient; import com.epmet.modules.feign.GovOrgFeignClient; @@ -90,6 +91,8 @@ public class ResiWarmheartedApplyServiceImpl extends BaseServiceImpl msgList = new ArrayList<>(); + msgList.add(wxSubscribeMessageFormDTO); + epmetMessageOpenFeignClient.sendWxSubscribeMessage(msgList); + //保存消息 return epmetMessageFeignClient.saveUserMessage(userMessageFormDTO); }