|
@@ -3,11 +3,16 @@ package com.ssj.service.weixin.push.service.impl;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.ssj.bean.weixin.push.domain.PushTemplate;
|
|
import com.ssj.bean.weixin.push.domain.PushTemplate;
|
|
import com.ssj.bean.weixin.push.domain.SendTemplateShort;
|
|
import com.ssj.bean.weixin.push.domain.SendTemplateShort;
|
|
|
|
+import com.ssj.bean.weixin.push.domain.TbWxTemplate;
|
|
import com.ssj.framework.core.common.service.BaseServiceImpl;
|
|
import com.ssj.framework.core.common.service.BaseServiceImpl;
|
|
import com.ssj.framework.core.rabbitmq.TopicRabbitConfig;
|
|
import com.ssj.framework.core.rabbitmq.TopicRabbitConfig;
|
|
import com.ssj.framework.core.rabbitmq.producer.BaseRabbitSend;
|
|
import com.ssj.framework.core.rabbitmq.producer.BaseRabbitSend;
|
|
|
|
+import com.ssj.framework.core.security.manager.TokenManager;
|
|
|
|
+import com.ssj.framework.core.util.SystemResourceLocator;
|
|
|
|
+import com.ssj.framework.weixin.util.NewsUtil;
|
|
import com.ssj.service.weixin.push.service.PushTemplateService;
|
|
import com.ssj.service.weixin.push.service.PushTemplateService;
|
|
import com.ssj.service.weixin.push.service.dao.PushTemplateDao;
|
|
import com.ssj.service.weixin.push.service.dao.PushTemplateDao;
|
|
|
|
+import com.ssj.service.weixin.push.service.util.WxTemplateCacheUtils;
|
|
import com.ssj.taskthread.Task;
|
|
import com.ssj.taskthread.Task;
|
|
import com.ssj.taskthread.ThreadPool;
|
|
import com.ssj.taskthread.ThreadPool;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
@@ -35,16 +40,20 @@ public class PushTemplateServiceImpl extends BaseServiceImpl<PushTemplate, Stri
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
private PushTemplateDao dao;
|
|
private PushTemplateDao dao;
|
|
-
|
|
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private TokenManager tokenManager;
|
|
|
|
+
|
|
|
|
+ @Override
|
|
public boolean savePushWxTemplate(SendTemplateShort sendTemplateShort) {
|
|
public boolean savePushWxTemplate(SendTemplateShort sendTemplateShort) {
|
|
boolean is=true;
|
|
boolean is=true;
|
|
- ThreadPool.getInstance().addTask(new SyncSaveAndSendPushDBTask(sendTemplateShort,dao));
|
|
|
|
|
|
+ ThreadPool.getInstance().addTask(new SyncSaveAndSendPushDBTask(sendTemplateShort,dao,tokenManager));
|
|
return is;
|
|
return is;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Override
|
|
public void saveAndSendPushWxTemplate(SendTemplateShort sendTemplateShort) {
|
|
public void saveAndSendPushWxTemplate(SendTemplateShort sendTemplateShort) {
|
|
- ThreadPool.getInstance().addTask(new SyncSaveAndSendPushDBTask(sendTemplateShort,dao));
|
|
|
|
|
|
+ ThreadPool.getInstance().addTask(new SyncSaveAndSendPushDBTask(sendTemplateShort,dao,tokenManager));
|
|
}
|
|
}
|
|
|
|
|
|
public boolean savePushWxTemplateByDb(SendTemplateShort sendTemplateShort) {
|
|
public boolean savePushWxTemplateByDb(SendTemplateShort sendTemplateShort) {
|
|
@@ -108,11 +117,14 @@ class SyncSaveAndSendPushDBTask extends Task{
|
|
private SendTemplateShort sendTemplateShort;
|
|
private SendTemplateShort sendTemplateShort;
|
|
|
|
|
|
private PushTemplateDao dao;
|
|
private PushTemplateDao dao;
|
|
|
|
+
|
|
|
|
+ private TokenManager tokenManager;
|
|
|
|
|
|
- public SyncSaveAndSendPushDBTask(SendTemplateShort sendTemplateShort,PushTemplateDao dao) {
|
|
|
|
|
|
+ public SyncSaveAndSendPushDBTask(SendTemplateShort sendTemplateShort,PushTemplateDao dao,TokenManager tokenManager) {
|
|
super();
|
|
super();
|
|
this.sendTemplateShort=sendTemplateShort;
|
|
this.sendTemplateShort=sendTemplateShort;
|
|
this.dao=dao;
|
|
this.dao=dao;
|
|
|
|
+ this.tokenManager = tokenManager;
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -122,15 +134,6 @@ class SyncSaveAndSendPushDBTask extends Task{
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
logger.error("推送信息保存异常...");
|
|
logger.error("推送信息保存异常...");
|
|
}
|
|
}
|
|
- try {
|
|
|
|
- String uuid=BaseRabbitSend.convertAndTopicSend(
|
|
|
|
- TopicRabbitConfig.exchange_task,
|
|
|
|
- TopicRabbitConfig.topic_push_messages_wx,
|
|
|
|
- sendTemplateShort);
|
|
|
|
- logger.error("已加入推送信息队列数据uuid:"+uuid+" data:"+JSONObject.toJSONString(sendTemplateShort));
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- logger.error("推送信息队列异常...");
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
public boolean savePushWxTemplateByDb(SendTemplateShort sendTemplateShort) {
|
|
public boolean savePushWxTemplateByDb(SendTemplateShort sendTemplateShort) {
|
|
@@ -153,16 +156,66 @@ class SyncSaveAndSendPushDBTask extends Task{
|
|
pushTemplate.setStat(1);
|
|
pushTemplate.setStat(1);
|
|
pushTemplate.setTryTime(0);
|
|
pushTemplate.setTryTime(0);
|
|
pushTemplate.setRemark("");
|
|
pushTemplate.setRemark("");
|
|
- pushTemplate=dao.save(pushTemplate);
|
|
|
|
- sendTemplateShort.setPushTemplateId(pushTemplate.getId());
|
|
|
|
|
|
+ run(pushTemplate,tokenManager);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
logger.error("保存推送信息异常...");
|
|
logger.error("保存推送信息异常...");
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ public void run(PushTemplate pushTemplate,TokenManager tokenManager) {
|
|
|
|
+ logger.error("立即微信推送任务开始---------------");
|
|
|
|
+ try {
|
|
|
|
+ TbWxTemplate tbWxTemplate= WxTemplateCacheUtils.getTbWxTemplate(pushTemplate.getWxTplSid(),null,null);
|
|
|
|
+ if(StringUtils.isEmpty(pushTemplate.getOpenid())){
|
|
|
|
+ pushTemplate.setRemark("OPENID为空,请注意检查来源代码逻辑...");
|
|
|
|
+ pushTemplate.setTryTime(pushTemplate.getTryTime()+1);
|
|
|
|
+ }else if(tbWxTemplate!=null && StringUtils.isNotEmpty(tbWxTemplate.getTemplateId())){
|
|
|
|
+ try {
|
|
|
|
+ String msg= NewsUtil.WX_TPL_BODY
|
|
|
|
+ .replace(NewsUtil.TPL_CODE_OPENID, pushTemplate.getOpenid())
|
|
|
|
+ .replace(NewsUtil.TPL_CODE_TPLID, tbWxTemplate.getTemplateId())
|
|
|
|
+ .replace(NewsUtil.TPL_CODE_URL, StringUtils.isNotEmpty(pushTemplate.getClickUrl())?pushTemplate.getClickUrl():"")
|
|
|
|
+ .replace(NewsUtil.TPL_CODE_DATA, pushTemplate.getContent())
|
|
|
|
+ .replace(NewsUtil.TPL_CODE_TOPCOLOR, StringUtils.isNotEmpty(pushTemplate.getTopColor())?pushTemplate.getTopColor():"");
|
|
|
|
+ if(StringUtils.isNotEmpty(pushTemplate.getMiniprogram())){
|
|
|
|
+ msg=msg.replace(NewsUtil.TPL_CODE_MINIPROQRAM_TEXT, NewsUtil.TPL_SUB_MINIPROQRAM_DATA);
|
|
|
|
+ msg=msg.replace(NewsUtil.MINIPROQRAM_DATA, pushTemplate.getMiniprogram());
|
|
|
|
+ }else{
|
|
|
|
+ msg=msg.replace(NewsUtil.TPL_CODE_MINIPROQRAM_TEXT, "");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //再去查询,防止重复推送,定时推送数据锁定一分钟
|
|
|
|
+
|
|
|
|
+ if(pushTemplate.getIsSuccess()==0){
|
|
|
|
+ JSONObject result =NewsUtil.sendTemplate(tokenManager.getSSJAccessToken(), msg);
|
|
|
|
+ logger.error("[PushTemplateServiceImpl] WxTplSid: " + pushTemplate.getWxTplSid() + ", result: " + result);
|
|
|
|
+ if(result != null && "0".equals(result.getString("errcode"))){
|
|
|
|
+ pushTemplate.setIsSuccess(1);
|
|
|
|
+ pushTemplate.setSuccessTime(new Date());
|
|
|
|
+ pushTemplate.setCurTotal(pushTemplate.getCurTotal()+1);
|
|
|
|
+ pushTemplate.setRemark("");
|
|
|
|
+ }else{
|
|
|
|
+ pushTemplate.setTryTime(pushTemplate.getTryTime()+1);
|
|
|
|
+ pushTemplate.setRemark("errcode:"+result.getString("errcode"));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ pushTemplate.setTryTime(pushTemplate.getTryTime()+1);
|
|
|
|
+ pushTemplate.setRemark("exception:"+e.getMessage());
|
|
|
|
+ }
|
|
|
|
+ }else{
|
|
|
|
+ pushTemplate.setRemark("模板池无法得到微信模板ID...");
|
|
|
|
+ pushTemplate.setTryTime(pushTemplate.getTryTime()+1);
|
|
|
|
+ }
|
|
|
|
+ dao.save(pushTemplate);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ logger.error("保存推送信息异常...");
|
|
|
|
+ }
|
|
|
|
+ logger.error("立即微信推送任务结束---------------");
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|