|
@@ -1,34 +1,40 @@
|
|
package com.book.push.handler;
|
|
package com.book.push.handler;
|
|
|
|
|
|
-import com.alibaba.fastjson.JSON;
|
|
|
|
-import com.book.dao.cps.pojo.AdminConfig;
|
|
|
|
-import com.book.dao.cps.pojo.User;
|
|
|
|
-import com.book.dao.cps.pojo.WechatAutoreply;
|
|
|
|
|
|
+import com.book.dao.cps.pojo.*;
|
|
|
|
+import com.book.dao.cpsshard.entity.Sign;
|
|
|
|
+import com.book.dao.cpsshard.mapper.SignMapper;
|
|
|
|
+import com.book.dao.cpsshard.pojo.example.SignExample;
|
|
|
|
+import com.book.dao.utils.DateUtils;
|
|
|
|
+import com.book.dao.utils.TimeUtil;
|
|
import com.book.push.builder.TextBuilder;
|
|
import com.book.push.builder.TextBuilder;
|
|
import com.book.push.builder.TuWenBuilder;
|
|
import com.book.push.builder.TuWenBuilder;
|
|
|
|
+import com.book.push.cons.SiteCons;
|
|
import com.book.push.cons.UrlCons;
|
|
import com.book.push.cons.UrlCons;
|
|
-import com.book.push.service.dao.AdminConfigService;
|
|
|
|
-import com.book.push.service.dao.OpHostService;
|
|
|
|
-import com.book.push.service.dao.UserService;
|
|
|
|
-import com.book.push.service.dao.WechatAutoreplayService;
|
|
|
|
|
|
+import com.book.push.service.dao.*;
|
|
|
|
+import com.book.push.service.push.PushService;
|
|
|
|
+import com.book.push.service.redis.RedisService;
|
|
|
|
+import com.book.push.service.wx.WxThirdPartService;
|
|
import com.book.push.utils.JsonUtils;
|
|
import com.book.push.utils.JsonUtils;
|
|
import com.book.push.vo.NewsMsgContent;
|
|
import com.book.push.vo.NewsMsgContent;
|
|
import com.book.push.vo.TextMsgContent;
|
|
import com.book.push.vo.TextMsgContent;
|
|
-import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
|
import me.chanjar.weixin.common.session.WxSessionManager;
|
|
import me.chanjar.weixin.common.session.WxSessionManager;
|
|
import me.chanjar.weixin.mp.api.WxMpService;
|
|
import me.chanjar.weixin.mp.api.WxMpService;
|
|
|
|
+import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
|
|
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
|
|
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
|
|
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
|
|
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
|
|
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
|
|
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
|
|
|
|
+import me.chanjar.weixin.mp.builder.kefu.NewsBuilder;
|
|
|
|
+import me.chanjar.weixin.open.api.WxOpenMpService;
|
|
|
|
+import me.chanjar.weixin.open.api.WxOpenService;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
-
|
|
|
|
-import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType;
|
|
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @author
|
|
* @author
|
|
@@ -43,7 +49,27 @@ public class MsgHandler extends AbstractHandler {
|
|
private UserService userService;
|
|
private UserService userService;
|
|
@Autowired
|
|
@Autowired
|
|
private OpHostService opHostService;
|
|
private OpHostService opHostService;
|
|
-
|
|
|
|
|
|
+ @Autowired
|
|
|
|
+ private SignMapper signMapper;
|
|
|
|
+ @Autowired
|
|
|
|
+ private SignRecommendService signRecommendService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private SignedRecommendService signedRecommendService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private ManageTitleService manageTitleService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private ManageCoverService manageCoverService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private PushService pushService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private ConfigService configService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private BookService bookService;
|
|
|
|
+ @Autowired
|
|
|
|
+ private RedisService redisService;
|
|
|
|
+ @Lazy
|
|
|
|
+ @Autowired
|
|
|
|
+ private WxThirdPartService wxThirdPartService;
|
|
@Override
|
|
@Override
|
|
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
|
|
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
|
|
Map<String, Object> context, WxMpService weixinService,
|
|
Map<String, Object> context, WxMpService weixinService,
|
|
@@ -53,15 +79,45 @@ public class MsgHandler extends AbstractHandler {
|
|
AdminConfig adminConfig = adminConfigService.selectByAppid(wxMessage.getToUser());
|
|
AdminConfig adminConfig = adminConfigService.selectByAppid(wxMessage.getToUser());
|
|
WechatAutoreply wechatAutoreply = wechatAutoreplayService.selectByAdminIdAndText(adminConfig.getAdminId(), wxMessage.getContent());
|
|
WechatAutoreply wechatAutoreply = wechatAutoreplayService.selectByAdminIdAndText(adminConfig.getAdminId(), wxMessage.getContent());
|
|
User user = userService.selectByOpenId(wxMessage.getFromUser());
|
|
User user = userService.selectByOpenId(wxMessage.getFromUser());
|
|
|
|
+ if ("签到".equals(wxMessage.getContent())){
|
|
|
|
+ SignExample example = SignExample.newAndCreateCriteria().andUidEqualTo(user.getId().intValue()).andCreatedateEqualTo(TimeUtil.getCurrentIntDate(TimeUtil.YYYYMMDD_N)).example();
|
|
|
|
+ Sign sign = signMapper.selectOneByExample(example);
|
|
|
|
+ if ("1".equals(adminConfig.getSignTip())) {
|
|
|
|
+
|
|
|
|
+ pushMessage(user, adminConfig, adminConfig.getAppid(), sign != null);
|
|
|
|
+ }
|
|
|
|
+ Config config = configService.selectByName(SiteCons.SIGN_KANDIAN);
|
|
|
|
+ int kandian = Integer.valueOf(config.getValue());
|
|
|
|
+
|
|
|
|
+ if (sign == null) {
|
|
|
|
+ sign = new Sign();
|
|
|
|
+ sign.setUid(user.getId().intValue());
|
|
|
|
+ sign.setCreatedate(TimeUtil.getCurrentIntDate(TimeUtil.YYYYMMDD_N));
|
|
|
|
+ sign.setCreatetime(DateUtils.getNow());
|
|
|
|
+ sign.setKandian(kandian);
|
|
|
|
+ signMapper.insert(sign);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ addKandian(user, kandian);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
String host = opHostService.selectById(adminConfig.getOphostId()).getHost();
|
|
String host = opHostService.selectById(adminConfig.getOphostId()).getHost();
|
|
if (wechatAutoreply != null) {
|
|
if (wechatAutoreply != null) {
|
|
String type = wechatAutoreply.getType();
|
|
String type = wechatAutoreply.getType();
|
|
- if ("text".equals(type)) {
|
|
|
|
|
|
+ if (MESSAGE_TYPE_TEXT.equals(type)) {
|
|
String textContent = wechatAutoreply.getTextContent();
|
|
String textContent = wechatAutoreply.getTextContent();
|
|
- List<TextMsgContent> textMsgContents = JsonUtils.getList(textContent, TextMsgContent.class);
|
|
|
|
|
|
+ TextMsgContent[] textMsgContents = JsonUtils.getArray(textContent, TextMsgContent.class);
|
|
StringBuilder sb = new StringBuilder();
|
|
StringBuilder sb = new StringBuilder();
|
|
for (TextMsgContent textMsgContent : textMsgContents) {
|
|
for (TextMsgContent textMsgContent : textMsgContents) {
|
|
- sb.append(String.format("<a href='%s'>%s</a>\n", textMsgContent.getUrl().replace("{$ophost}", host), textMsgContent.getTitle()));
|
|
|
|
|
|
+ String url = (textMsgContent.getUrl() !=null?textMsgContent.getUrl():textMsgContent.getReferral_url());
|
|
|
|
+ if (url==null){
|
|
|
|
+ url = String.format(UrlCons.FRONT_URL, adminConfig.getAppid(), host, UrlCons.MODEL_BOOK, adminConfig.getAdminId(), user.getId() + "")
|
|
|
|
+ .concat("&bookId=").concat(textMsgContent.getBook_id()).concat("&").concat(textMsgContent.getChapter_id());
|
|
|
|
+ }
|
|
|
|
+ sb.append("\n\r").append(String.format("<a href='%s'>%s</a>\n", url.replace("{$ophost}", host), textMsgContent.getTitle()));
|
|
}
|
|
}
|
|
String nickname = user.getNickname();
|
|
String nickname = user.getNickname();
|
|
String json = adminConfig.getJson().toString();
|
|
String json = adminConfig.getJson().toString();
|
|
@@ -70,9 +126,9 @@ public class MsgHandler extends AbstractHandler {
|
|
String msg = "@%s,欢迎关注 [%s],点击下方继续阅读 %s\n\r为了方便下次阅读,请置顶公众号";
|
|
String msg = "@%s,欢迎关注 [%s],点击下方继续阅读 %s\n\r为了方便下次阅读,请置顶公众号";
|
|
msg = String.format(msg, nickname, nick_name, sb.toString());
|
|
msg = String.format(msg, nickname, nick_name, sb.toString());
|
|
return new TextBuilder().build(msg, wxMessage, weixinService);
|
|
return new TextBuilder().build(msg, wxMessage, weixinService);
|
|
- } else if ("news".equals(type)) {
|
|
|
|
|
|
+ } else if (MESSAGE_TYPE_NEWS.equals(type)) {
|
|
|
|
|
|
- List<NewsMsgContent> newsMsgContents = JsonUtils.getList(wechatAutoreply.getNewsContent(), NewsMsgContent.class);
|
|
|
|
|
|
+ NewsMsgContent[] newsMsgContents = JsonUtils.getArray(wechatAutoreply.getNewsContent(), NewsMsgContent.class);
|
|
List<WxMpXmlOutNewsMessage.Item> items = new ArrayList<>();
|
|
List<WxMpXmlOutNewsMessage.Item> items = new ArrayList<>();
|
|
for (NewsMsgContent newsMsgContent : newsMsgContents) {
|
|
for (NewsMsgContent newsMsgContent : newsMsgContents) {
|
|
WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
|
|
WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
|
|
@@ -85,16 +141,75 @@ public class MsgHandler extends AbstractHandler {
|
|
.concat("&bookId=").concat(newsMsgContent.getBook_id()).concat("&").concat(newsMsgContent.getChapter_id());
|
|
.concat("&bookId=").concat(newsMsgContent.getBook_id()).concat("&").concat(newsMsgContent.getChapter_id());
|
|
}
|
|
}
|
|
item.setUrl(url);
|
|
item.setUrl(url);
|
|
|
|
+ items.add(item);
|
|
}
|
|
}
|
|
|
|
|
|
- new TuWenBuilder().build(items, wxMessage, weixinService);
|
|
|
|
|
|
+ return new TuWenBuilder().build(items, wxMessage, weixinService);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- return new TextBuilder().build("success", wxMessage, weixinService);
|
|
|
|
|
|
+ return null;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 添加看点
|
|
|
|
+ *
|
|
|
|
+ * @param user
|
|
|
|
+ * @param kandian
|
|
|
|
+ */
|
|
|
|
+ private void addKandian(User user, int kandian) {
|
|
|
|
+ //TODO
|
|
|
|
+ userService.insertKandian(user.getId(),"5",0,kandian);
|
|
|
|
+ redisService.del("freeCoin_"+user.getId());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 签到推送
|
|
|
|
+ *
|
|
|
|
+ * @param user
|
|
|
|
+ * @param b 是否已经签到
|
|
|
|
+ */
|
|
|
|
+ private void pushMessage(User user, AdminConfig adminConfig, String appid, boolean b) {
|
|
|
|
+ Long bookId;
|
|
|
|
+ if (b) {
|
|
|
|
+ SignedRecommand signRecommand = signedRecommendService.selectRandom(user.getSex().toString());
|
|
|
|
+ bookId = signRecommand.getBookId();
|
|
|
|
+ } else {
|
|
|
|
+ SignRecommand signRecommand = signRecommendService.selectRandom(user.getSex().toString());
|
|
|
|
+ bookId = signRecommand.getBookId();
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ ManageTitle manageTitle = manageTitleService.selectRandom(user.getSex().toString());
|
|
|
|
+ ManageCover manageCover = manageCoverService.selectRandom("1", user.getSex().toString());
|
|
|
|
+ WxMpKefuMessage.WxArticle wxArticle = new WxMpKefuMessage.WxArticle();
|
|
|
|
+ wxArticle.setTitle(manageTitle.getTitle());
|
|
|
|
+ wxArticle.setPicUrl(manageCover.getImage());
|
|
|
|
+ wxArticle.setUrl(createUrl(bookId, adminConfig, user));
|
|
|
|
+ WxMpKefuMessage build = new NewsBuilder().toUser(user.getOpenid()).addArticle(wxArticle).build();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ Integer platformId = adminConfig.getPlatformId();
|
|
|
|
+ WxOpenService wxOpenService = wxThirdPartService.getWxOpenServiceByPlatFormId(platformId);
|
|
|
|
+ WxOpenMpService wxMpService = wxOpenService.getWxOpenComponentService().getWxMpServiceByAppid(appid);
|
|
|
|
+ long second = 10 * 60 * 60;
|
|
|
|
+ pushService.addWxKefuMessagePushTask(wxMpService, build, second, TimeUnit.SECONDS);
|
|
|
|
+
|
|
|
|
+ System.out.println("发送失败");
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ private String createUrl(Long bookId, AdminConfig adminConfig, User user) {
|
|
|
|
+
|
|
|
|
+ return bookService.createBookUrl(bookId, adminConfig, user);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+ String s = "[{\"book_id\":3584,\"chapter_id\":\"\",\"title\":\"曾经是雇佣兵王的他,退役归来,横行都市,左拥右抱..\",\"description\":\"我噢噢噢噢噢噢噢噢\",\"image_type\":3,\"activity_url\":\"\",\"referral_url\":\"http:\\/\\/wxe1271417b2ff5b1c.{$ophost}\\/t\\/1175\",\"give_activity_url\":\"\",\"url\":\"http:\\/\\/wxe1271417b2ff5b1c.{$ophost}\\/t\\/1175\",\"image\":\"https:\\/\\/cpsn.zsjwaw.cn\\/uploads\\/20180601\\/e14a04e6ffe3ab0464cd965b6f34e6f2.jpg\"}]";
|
|
|
|
+ NewsMsgContent[] newsMsgContents = JsonUtils.getArray(s, NewsMsgContent.class);
|
|
|
|
+ System.out.println(newsMsgContents);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|