更新代码
This commit is contained in:
@@ -4,6 +4,7 @@ import org.rcy.framework.api.entity.BaseEntity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
import java.util.Map;
|
||||
|
||||
@Entity
|
||||
@Table(name = "MESSAGE")
|
||||
@@ -15,6 +16,9 @@ public class Message extends BaseEntity {
|
||||
private String messageReceiver;
|
||||
private String bizId;
|
||||
private Boolean hasRead = false;
|
||||
private SubcribeMsgMode subscribeMsgMode;
|
||||
private Map<String,String> templateValue;
|
||||
private String messageText;
|
||||
|
||||
public MessageType getMessageType() {
|
||||
return messageType;
|
||||
@@ -71,4 +75,28 @@ public class Message extends BaseEntity {
|
||||
public void setHasRead(Boolean hasRead) {
|
||||
this.hasRead = hasRead;
|
||||
}
|
||||
|
||||
public SubcribeMsgMode getSubscribeMsgMode() {
|
||||
return subscribeMsgMode;
|
||||
}
|
||||
|
||||
public void setSubscribeMsgMode(SubcribeMsgMode subscribeMsgMode) {
|
||||
this.subscribeMsgMode = subscribeMsgMode;
|
||||
}
|
||||
|
||||
public Map<String, String> getTemplateValue() {
|
||||
return templateValue;
|
||||
}
|
||||
|
||||
public void setTemplateValue(Map<String, String> templateValue) {
|
||||
this.templateValue = templateValue;
|
||||
}
|
||||
|
||||
public String getMessageText() {
|
||||
return messageText;
|
||||
}
|
||||
|
||||
public void setMessageText(String messageText) {
|
||||
this.messageText = messageText;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,13 +2,19 @@ package org.chenyon.message;
|
||||
|
||||
import org.chenyon.user.LoginCheck;
|
||||
import org.chenyon.user.UserContext;
|
||||
import org.chenyon.wx.AesException;
|
||||
import org.rcy.framework.api.entity.PageResult;
|
||||
import org.rcy.framework.api.entity.ResultMessage;
|
||||
import org.rcy.framework.utils.aes.AESUtils;
|
||||
import org.rcy.framework.utils.string.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.xml.parsers.ParserConfigurationException;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/message")
|
||||
public class MessageController {
|
||||
@@ -74,7 +80,16 @@ public class MessageController {
|
||||
|
||||
|
||||
@PostMapping("/sendMessage")
|
||||
public ResultMessage sendMessage(@RequestBody MessageVo vo){
|
||||
public ResultMessage sendMessage(@RequestBody MessageVo vo, HttpServletRequest request) throws Exception {
|
||||
String internalToken = request.getHeader("internalToken");
|
||||
if(StringUtils.isBlank(internalToken)) {
|
||||
return ResultMessage.error("无权限");
|
||||
}
|
||||
String decrypt = AESUtils.decrypt(internalToken, "rent*123");
|
||||
if(!"oa".equals(decrypt)){
|
||||
return ResultMessage.error("无权限");
|
||||
}
|
||||
messageService.send(vo);
|
||||
return ResultMessage.success();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,7 +25,9 @@
|
||||
<if test="messageReceiver != null">
|
||||
AND messageReceiver = #{messageReceiver}
|
||||
</if>
|
||||
|
||||
<if test="hasRead != null">
|
||||
AND hasRead = #{hasRead}
|
||||
</if>
|
||||
</where>
|
||||
</sql>
|
||||
</mapper>
|
||||
@@ -5,6 +5,8 @@ import org.rcy.framework.api.entity.PageQueryRequest;
|
||||
public class MessageQueryCondition extends PageQueryRequest {
|
||||
|
||||
private String messageReceiver;
|
||||
private Boolean hasRead;
|
||||
private String bizId;
|
||||
|
||||
public String getMessageReceiver() {
|
||||
return messageReceiver;
|
||||
@@ -13,4 +15,20 @@ public class MessageQueryCondition extends PageQueryRequest {
|
||||
public void setMessageReceiver(String messageReceiver) {
|
||||
this.messageReceiver = messageReceiver;
|
||||
}
|
||||
|
||||
public Boolean getHasRead() {
|
||||
return hasRead;
|
||||
}
|
||||
|
||||
public void setHasRead(Boolean hasRead) {
|
||||
this.hasRead = hasRead;
|
||||
}
|
||||
|
||||
public String getBizId() {
|
||||
return bizId;
|
||||
}
|
||||
|
||||
public void setBizId(String bizId) {
|
||||
this.bizId = bizId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.chenyon.message;
|
||||
|
||||
import org.chenyon.wx.AesException;
|
||||
import org.chenyon.wx.WXBizMsgCrypt;
|
||||
import org.rcy.framework.api.entity.PageResult;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -16,6 +15,8 @@ public class MessageService {
|
||||
|
||||
@Autowired
|
||||
private MessageDao messageDao;
|
||||
@Autowired
|
||||
private List<SubscribeMsgSender> subscribeMsgSenders;
|
||||
|
||||
|
||||
public MessageVo detail(Long id) {
|
||||
@@ -38,6 +39,7 @@ public class MessageService {
|
||||
public Integer countUnread(String cusNo){
|
||||
MessageQueryCondition messageQueryCondition = new MessageQueryCondition();
|
||||
messageQueryCondition.setMessageReceiver(cusNo);
|
||||
messageQueryCondition.setHasRead(false);
|
||||
Long count = messageDao.countCondition(messageQueryCondition);
|
||||
return count.intValue();
|
||||
}
|
||||
@@ -53,21 +55,23 @@ public class MessageService {
|
||||
|
||||
public void send(MessageVo messageVo) throws AesException, ParserConfigurationException {
|
||||
// 消息入库
|
||||
//
|
||||
String urlSend = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=";
|
||||
|
||||
|
||||
// 需要加密的明文
|
||||
String encodingAesKey = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG";
|
||||
String token = "pamtest";
|
||||
String timestamp = "1409304348";
|
||||
String nonce = "xxxxxx";
|
||||
String appId = "wxb11529c136998cb6";
|
||||
String replyMsg = " 中文<xml><ToUserName><![CDATA[oia2TjjewbmiOUlr6X-1crbLOvLw]]></ToUserName><FromUserName><![CDATA[gh_7f083739789a]]></FromUserName><CreateTime>1407743423</CreateTime><MsgType><![CDATA[video]]></MsgType><Video><MediaId><![CDATA[eYJ1MbwPRJtOvIEabaxHs7TX2D-HV71s79GUxqdUkjm6Gs2Ed1KF3ulAOA9H1xG0]]></MediaId><Title><![CDATA[testCallBackReplyVideo]]></Title><Description><![CDATA[testCallBackReplyVideo]]></Description></Video></xml>";
|
||||
|
||||
WXBizMsgCrypt pc = new WXBizMsgCrypt(token, encodingAesKey, appId);
|
||||
String mingwen = pc.encryptMsg(replyMsg, timestamp, nonce);
|
||||
System.out.println("加密后: " + mingwen);
|
||||
|
||||
Message message = new Message();
|
||||
message.setMessageType(MessageType.valueOf(messageVo.getMessageType()));
|
||||
message.setMessageContent(messageVo.getMessageContent());
|
||||
message.setMessageTime(messageVo.getMessageTime());
|
||||
message.setMessageReceiver(messageVo.getMessageReceiver());
|
||||
message.setBizId(messageVo.getBizId());
|
||||
message.setSubscribeMsgMode(SubcribeMsgMode.valueOf(messageVo.getSubscribeMsgMode()));
|
||||
message.setTitle(messageVo.getTitle());
|
||||
message.setHasRead(false);
|
||||
message.setTemplateValue(messageVo.getTemplateValue());
|
||||
message.setMessageText(messageVo.getMessageText());
|
||||
messageDao.save(message);
|
||||
//发送订阅消息
|
||||
for (SubscribeMsgSender subscribeMsgSender : subscribeMsgSenders) {
|
||||
if(subscribeMsgSender.support(messageVo) && Boolean.TRUE.equals(messageVo.getSendSubscribeMsg())){
|
||||
subscribeMsgSender.sendSubscribeMsg(messageVo);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,20 @@
|
||||
package org.chenyon.message;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class MessageVo {
|
||||
private String id;
|
||||
private String title;
|
||||
private String messageType;
|
||||
private String messageContent;
|
||||
private String messageText;
|
||||
private String messageTime;
|
||||
private String messageReceiver;
|
||||
private String bizId;
|
||||
private Boolean hasRead;
|
||||
private Boolean sendSubscribeMsg;
|
||||
private String subscribeMsgMode;
|
||||
private Map<String,String> templateValue;
|
||||
|
||||
public String getMessageType() {
|
||||
return messageType;
|
||||
@@ -73,4 +79,36 @@ public class MessageVo {
|
||||
public void setHasRead(Boolean hasRead) {
|
||||
this.hasRead = hasRead;
|
||||
}
|
||||
|
||||
public Boolean getSendSubscribeMsg() {
|
||||
return sendSubscribeMsg;
|
||||
}
|
||||
|
||||
public void setSendSubscribeMsg(Boolean sendSubscribeMsg) {
|
||||
this.sendSubscribeMsg = sendSubscribeMsg;
|
||||
}
|
||||
|
||||
public String getSubscribeMsgMode() {
|
||||
return subscribeMsgMode;
|
||||
}
|
||||
|
||||
public void setSubscribeMsgMode(String subscribeMsgMode) {
|
||||
this.subscribeMsgMode = subscribeMsgMode;
|
||||
}
|
||||
|
||||
public String getMessageText() {
|
||||
return messageText;
|
||||
}
|
||||
|
||||
public void setMessageText(String messageText) {
|
||||
this.messageText = messageText;
|
||||
}
|
||||
|
||||
public Map<String, String> getTemplateValue() {
|
||||
return templateValue;
|
||||
}
|
||||
|
||||
public void setTemplateValue(Map<String, String> templateValue) {
|
||||
this.templateValue = templateValue;
|
||||
}
|
||||
}
|
||||
7
src/main/java/org/chenyon/message/SubcribeMsgMode.java
Normal file
7
src/main/java/org/chenyon/message/SubcribeMsgMode.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package org.chenyon.message;
|
||||
|
||||
public enum SubcribeMsgMode {
|
||||
WEAPPMINIPROGRAM,
|
||||
SMS,
|
||||
EMAIL,;
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package org.chenyon.message;
|
||||
|
||||
public interface SubscribeMsgSender {
|
||||
void sendSubscribeMsg(MessageVo messageVo);
|
||||
Boolean support(MessageVo messageVo);
|
||||
}
|
||||
111
src/main/java/org/chenyon/message/WeAppMiniProgramMsgSender.java
Normal file
111
src/main/java/org/chenyon/message/WeAppMiniProgramMsgSender.java
Normal file
@@ -0,0 +1,111 @@
|
||||
package org.chenyon.message;
|
||||
|
||||
import org.chenyon.user.UserService;
|
||||
import org.chenyon.user.UserVo;
|
||||
import org.chenyon.wx.WXBizMsgCrypt;
|
||||
import org.chenyon.wx.WeChatAccessTokenService;
|
||||
import org.rcy.framework.utils.json.JsonUtils;
|
||||
import org.rcy.framework.utils.net.HttpRequestUtils;
|
||||
import org.rcy.framework.utils.net.HttpResponse;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class WeAppMiniProgramMsgSender implements SubscribeMsgSender{
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(WeAppMiniProgramMsgSender.class);
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
@Autowired
|
||||
private WeChatAccessTokenService weChatAccessTokenService;
|
||||
|
||||
@Override
|
||||
public void sendSubscribeMsg(MessageVo messageVo) {
|
||||
try {
|
||||
if(messageVo == null || messageVo.getMessageReceiver() == null){
|
||||
return;
|
||||
}
|
||||
UserVo userVo = userService.getByCusNo(messageVo.getMessageReceiver());
|
||||
//用户未开启推送则不发送
|
||||
if(userVo == null || !Boolean.TRUE.equals(userVo.getSubscribeMsg())){
|
||||
return;
|
||||
}
|
||||
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + weChatAccessTokenService.getAccessToken();
|
||||
Map<String,Object> params = new HashMap<>();
|
||||
params.put("touser",userVo.getOpenId());
|
||||
params.put("template_id",getTemplateId(messageVo.getMessageType()));
|
||||
params.put("page",getForwardPage(messageVo.getMessageType()));
|
||||
params.put("miniprogram_state","formal");
|
||||
params.put("lang","zh_CN");
|
||||
params.put("data",getPushData(messageVo));
|
||||
HttpResponse response = HttpRequestUtils.sendPost(url, JsonUtils.convertJson(params), null);
|
||||
Map respMap = JsonUtils.parseObject(response.getRespStr(), Map.class);
|
||||
if (respMap.get("errcode") != null) {
|
||||
log.error("推送消息到小程序失败");
|
||||
}
|
||||
}catch (Exception e) {
|
||||
log.error("推送消息到小程序失败: " + e.getMessage(),e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean support(MessageVo messageVo) {
|
||||
return SubcribeMsgMode.WEAPPMINIPROGRAM.name().equals(messageVo.getSubscribeMsgMode());
|
||||
}
|
||||
|
||||
private String getTemplateId(String msgType) {
|
||||
switch (msgType) {
|
||||
case "BILL": return "9QlNxNONJBICzw3Vcetqbf9yv4lI9q9cR_px8ujlOu8";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private Map<String,Object> getPushData(MessageVo messageVo) {
|
||||
switch (messageVo.getMessageType()) {
|
||||
case "BILL": return buildBillData(messageVo);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Map<String,Object> buildBillData(MessageVo messageVo) {
|
||||
Map<String,Object> data = new HashMap<>();
|
||||
//设置账单金额
|
||||
Map<String,Object> amount2 = new HashMap<>();
|
||||
amount2.put("value",messageVo.getTemplateValue().get("billAmount"));
|
||||
data.put("amount2",amount2);
|
||||
//设置账单日期
|
||||
Map<String,Object> time3 = new HashMap<>();
|
||||
time3.put("value",messageVo.getTemplateValue().get("billDate"));
|
||||
data.put("time3",time3);
|
||||
//设置备注
|
||||
Map<String,Object> thing4 = new HashMap<>();
|
||||
thing4.put("value",messageVo.getTemplateValue().get("remark"));
|
||||
data.put("thing4",thing4);
|
||||
//设置资产店名
|
||||
|
||||
Map<String,Object> thing8 = new HashMap<>();
|
||||
thing8.put("value",messageVo.getTemplateValue().get("assetsName") + "");
|
||||
data.put("thing8",thing8);
|
||||
|
||||
//设置房间号
|
||||
|
||||
Map<String,Object> thing1 = new HashMap<>();
|
||||
thing1.put("value",messageVo.getTemplateValue().get("roomNo") + "");
|
||||
data.put("thing1",thing1);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
private String getForwardPage(String msgType) {
|
||||
switch (msgType) {
|
||||
case "BILL": return "unpaid";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user