|
@@ -1,162 +1,160 @@
|
|
|
-package com.book.push.handler;
|
|
|
-
|
|
|
-import com.book.dao.cps.pojo.AdminConfig;
|
|
|
-import com.book.dao.cps.pojo.User;
|
|
|
-import com.book.dao.cps.pojo.WechatSubscribeConfig;
|
|
|
-import com.book.dao.cps.pojo.WechatSubscribeSwitch;
|
|
|
-import com.book.push.builder.TextBuilder;
|
|
|
-import com.book.push.builder.TuWenBuilder;
|
|
|
-import com.book.push.cons.UrlCons;
|
|
|
-import com.book.push.service.dao.*;
|
|
|
-import com.book.push.utils.JsonUtils;
|
|
|
-import com.book.push.vo.NewsMsgContent;
|
|
|
-import com.book.push.vo.TextMsgContent;
|
|
|
-import com.sun.xml.internal.fastinfoset.tools.FI_DOM_Or_XML_DOM_SAX_SAXEvent;
|
|
|
-import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
-import me.chanjar.weixin.common.session.WxSessionManager;
|
|
|
-import me.chanjar.weixin.mp.api.WxMpService;
|
|
|
-import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
|
|
|
-import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
|
|
|
-import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
|
|
|
-import me.chanjar.weixin.mp.bean.result.WxMpUser;
|
|
|
-import org.apache.commons.lang3.StringUtils;
|
|
|
-import org.checkerframework.checker.units.qual.A;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author
|
|
|
- */
|
|
|
-@Component
|
|
|
-public class SubscribeHandler extends AbstractHandler {
|
|
|
- @Autowired
|
|
|
- private UserService userService;
|
|
|
- @Autowired
|
|
|
- private AdminConfigService adminConfigService;
|
|
|
- @Autowired
|
|
|
- private WechatSubscribeSwitchService wechatSubscribeSwitchService;
|
|
|
- @Autowired
|
|
|
- private WechatSubscribeConfigService wechatSubscribeConfigService;
|
|
|
- @Autowired
|
|
|
- private OpHostService opHostService;
|
|
|
- @Override
|
|
|
- public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
|
|
|
- Map<String, Object> context, WxMpService weixinService,
|
|
|
- WxSessionManager sessionManager) throws WxErrorException {
|
|
|
-
|
|
|
- this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUser());
|
|
|
-
|
|
|
- // 获取微信用户基本信息
|
|
|
- WxMpUser userWxInfo ;
|
|
|
- String openId = StringUtils.isEmpty(wxMessage.getOpenId())?wxMessage.getOpenId():wxMessage.getFromUser();
|
|
|
- String appid = StringUtils.isEmpty(wxMessage.getAuthorizeAppId())?wxMessage.getToUser():wxMessage.getAuthorizeAppId();
|
|
|
- AdminConfig adminConfig = adminConfigService.selectByAppid(appid);
|
|
|
- User user = userService.selectByOpenId(openId);
|
|
|
- try {
|
|
|
- userWxInfo = weixinService.getUserService()
|
|
|
- .userInfo(wxMessage.getFromUser(), null);
|
|
|
- if (userWxInfo != null) {
|
|
|
- // TODO 可以添加关注用户到本地数据库
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- if (user==null){
|
|
|
-
|
|
|
- user = userService.createUserByWxUser(userWxInfo,adminConfig.getAdminId());
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- } catch (WxErrorException e) {
|
|
|
- if (e.getError().getErrorCode() == 48001) {
|
|
|
- this.logger.info("该公众号没有获取用户信息权限!");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- WxMpXmlOutMessage responseResult = null;
|
|
|
- try {
|
|
|
- responseResult = this.handleSpecial(wxMessage);
|
|
|
- } catch (Exception e) {
|
|
|
- this.logger.error(e.getMessage(), e);
|
|
|
- }
|
|
|
-
|
|
|
- if (responseResult != null) {
|
|
|
- return responseResult;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- try {
|
|
|
- WechatSubscribeSwitch wechatSubscribeSwitch = wechatSubscribeSwitchService.selectByAdminId(adminConfig.getAdminId());
|
|
|
- Byte status = wechatSubscribeSwitch.getStatus();
|
|
|
- if (status.intValue() == 0){
|
|
|
-
|
|
|
- return new TextBuilder().build(String.format("@%s,感谢关注啦",user.getNickname()), wxMessage, weixinService);
|
|
|
- }
|
|
|
- if (wechatSubscribeSwitch.getType()==1){
|
|
|
- return new TextBuilder().build(String.format("@%s,感谢关注啦",user.getNickname()), wxMessage, weixinService);
|
|
|
- }
|
|
|
- WechatSubscribeConfig wechatSubscribeConfig = wechatSubscribeConfigService.selectByAdminId(adminConfig.getAdminId());
|
|
|
- if (wechatSubscribeConfig!=null){
|
|
|
- String host = opHostService.selectById(adminConfig.getOphostId()).getHost();
|
|
|
- String type = wechatSubscribeConfig.getType().toString();
|
|
|
- if ("text".equals(type)){
|
|
|
- String textContent = wechatSubscribeConfig.getTextContent();
|
|
|
- List<TextMsgContent> textMsgContents = JsonUtils.getList(textContent, TextMsgContent.class);
|
|
|
- StringBuilder sb = new StringBuilder();
|
|
|
- for (TextMsgContent textMsgContent : textMsgContents) {
|
|
|
- sb.append(String.format("<a href='%s'>%s</a>\n", textMsgContent.getUrl().replace("{$ophost}", host), textMsgContent.getTitle()));
|
|
|
- }
|
|
|
- String nickname = user.getNickname();
|
|
|
- String json = adminConfig.getJson().toString();
|
|
|
- AdminConfig.Info info = JsonUtils.getObject(json, AdminConfig.Info.class);
|
|
|
- String nick_name = info.getAuthorizer_info().getNick_name();
|
|
|
- String msg = "@%s,欢迎关注 [%s],点击下方继续阅读 %s\n";
|
|
|
- msg = String.format(msg, nickname, nick_name, sb.toString());
|
|
|
- return new TextBuilder().build(msg, wxMessage, weixinService);
|
|
|
- }else if ("news".equals(type)){
|
|
|
- List<NewsMsgContent> newsMsgContents = JsonUtils.getList(wechatSubscribeConfig.getNewsContent(), NewsMsgContent.class);
|
|
|
- List<WxMpXmlOutNewsMessage.Item> items = new ArrayList<>();
|
|
|
- for (NewsMsgContent newsMsgContent : newsMsgContents) {
|
|
|
- WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
|
|
|
- item.setTitle(newsMsgContent.getTitle());
|
|
|
- item.setPicUrl(newsMsgContent.getImage());
|
|
|
- item.setDescription(newsMsgContent.getDescription());
|
|
|
- String url = newsMsgContent.getUrl().replace("{$ophost}", host);
|
|
|
- if (!StringUtils.isEmpty(newsMsgContent.getBook_id())) {
|
|
|
- url = String.format(UrlCons.FRONT_URL, adminConfig.getAppid(), host, UrlCons.MODEL_BOOK, adminConfig.getAdminId(), user.getId() + "")
|
|
|
- .concat("&bookId=").concat(newsMsgContent.getBook_id()).concat("&").concat(newsMsgContent.getChapter_id());
|
|
|
- }
|
|
|
- item.setUrl(url);
|
|
|
- }
|
|
|
-
|
|
|
- return new TuWenBuilder().build(items, wxMessage, weixinService);
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- this.logger.error(e.getMessage(), e);
|
|
|
- }
|
|
|
-
|
|
|
- return new TextBuilder().build(String.format("@%s,感谢关注啦",user.getNickname()), wxMessage, weixinService);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 处理特殊请求,比如如果是扫码进来的,可以做相应处理
|
|
|
- */
|
|
|
- private WxMpXmlOutMessage handleSpecial(WxMpXmlMessage wxMessage)
|
|
|
- throws Exception {
|
|
|
- //TODO
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
-}
|
|
|
+package com.book.push.handler;
|
|
|
+
|
|
|
+import com.book.dao.cps.pojo.AdminConfig;
|
|
|
+import com.book.dao.cps.pojo.User;
|
|
|
+import com.book.dao.cps.pojo.WechatSubscribeConfig;
|
|
|
+import com.book.dao.cps.pojo.WechatSubscribeSwitch;
|
|
|
+import com.book.push.builder.TextBuilder;
|
|
|
+import com.book.push.builder.TuWenBuilder;
|
|
|
+import com.book.push.cons.UrlCons;
|
|
|
+import com.book.push.service.dao.*;
|
|
|
+import com.book.push.utils.JsonUtils;
|
|
|
+import com.book.push.vo.NewsMsgContent;
|
|
|
+import com.book.push.vo.TextMsgContent;
|
|
|
+import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
+import me.chanjar.weixin.common.session.WxSessionManager;
|
|
|
+import me.chanjar.weixin.mp.api.WxMpService;
|
|
|
+import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
|
|
|
+import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
|
|
|
+import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
|
|
|
+import me.chanjar.weixin.mp.bean.result.WxMpUser;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class SubscribeHandler extends AbstractHandler {
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+ @Autowired
|
|
|
+ private AdminConfigService adminConfigService;
|
|
|
+ @Autowired
|
|
|
+ private WechatSubscribeSwitchService wechatSubscribeSwitchService;
|
|
|
+ @Autowired
|
|
|
+ private WechatSubscribeConfigService wechatSubscribeConfigService;
|
|
|
+ @Autowired
|
|
|
+ private OpHostService opHostService;
|
|
|
+ @Override
|
|
|
+ public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
|
|
|
+ Map<String, Object> context, WxMpService weixinService,
|
|
|
+ WxSessionManager sessionManager) throws WxErrorException {
|
|
|
+
|
|
|
+ this.logger.info("新关注用户 OPENID: " + wxMessage.getFromUser());
|
|
|
+
|
|
|
+ // 获取微信用户基本信息
|
|
|
+ WxMpUser userWxInfo ;
|
|
|
+ String openId = StringUtils.isEmpty(wxMessage.getOpenId())?wxMessage.getOpenId():wxMessage.getFromUser();
|
|
|
+ String appid = StringUtils.isEmpty(wxMessage.getAuthorizeAppId())?wxMessage.getToUser():wxMessage.getAuthorizeAppId();
|
|
|
+ AdminConfig adminConfig = adminConfigService.selectByAppid(appid);
|
|
|
+ User user = userService.selectByOpenId(openId);
|
|
|
+ try {
|
|
|
+ userWxInfo = weixinService.getUserService()
|
|
|
+ .userInfo(wxMessage.getFromUser(), null);
|
|
|
+ if (userWxInfo != null) {
|
|
|
+ // TODO 可以添加关注用户到本地数据库
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (user==null){
|
|
|
+
|
|
|
+ user = userService.createUserByWxUser(userWxInfo,adminConfig.getAdminId());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ } catch (WxErrorException e) {
|
|
|
+ if (e.getError().getErrorCode() == 48001) {
|
|
|
+ this.logger.info("该公众号没有获取用户信息权限!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ WxMpXmlOutMessage responseResult = null;
|
|
|
+ try {
|
|
|
+ responseResult = this.handleSpecial(wxMessage);
|
|
|
+ } catch (Exception e) {
|
|
|
+ this.logger.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (responseResult != null) {
|
|
|
+ return responseResult;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ try {
|
|
|
+ WechatSubscribeSwitch wechatSubscribeSwitch = wechatSubscribeSwitchService.selectByAdminId(adminConfig.getAdminId());
|
|
|
+ Byte status = wechatSubscribeSwitch.getStatus();
|
|
|
+ if (status.intValue() == 0){
|
|
|
+
|
|
|
+ return new TextBuilder().build(String.format("@%s,感谢关注啦",user.getNickname()), wxMessage, weixinService);
|
|
|
+ }
|
|
|
+ if (wechatSubscribeSwitch.getType()==1){
|
|
|
+ return new TextBuilder().build(String.format("@%s,感谢关注啦",user.getNickname()), wxMessage, weixinService);
|
|
|
+ }
|
|
|
+ WechatSubscribeConfig wechatSubscribeConfig = wechatSubscribeConfigService.selectByAdminId(adminConfig.getAdminId());
|
|
|
+ if (wechatSubscribeConfig!=null){
|
|
|
+ String host = opHostService.selectById(adminConfig.getOphostId()).getHost();
|
|
|
+ String type = wechatSubscribeConfig.getType().toString();
|
|
|
+ if ("text".equals(type)){
|
|
|
+ String textContent = wechatSubscribeConfig.getTextContent();
|
|
|
+ List<TextMsgContent> textMsgContents = JsonUtils.getList(textContent, TextMsgContent.class);
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (TextMsgContent textMsgContent : textMsgContents) {
|
|
|
+ sb.append(String.format("<a href='%s'>%s</a>\n", textMsgContent.getUrl().replace("{$ophost}", host), textMsgContent.getTitle()));
|
|
|
+ }
|
|
|
+ String nickname = user.getNickname();
|
|
|
+ String json = adminConfig.getJson().toString();
|
|
|
+ AdminConfig.Info info = JsonUtils.getObject(json, AdminConfig.Info.class);
|
|
|
+ String nick_name = info.getAuthorizer_info().getNick_name();
|
|
|
+ String msg = "@%s,欢迎关注 [%s],点击下方继续阅读 %s\n";
|
|
|
+ msg = String.format(msg, nickname, nick_name, sb.toString());
|
|
|
+ return new TextBuilder().build(msg, wxMessage, weixinService);
|
|
|
+ }else if ("news".equals(type)){
|
|
|
+ List<NewsMsgContent> newsMsgContents = JsonUtils.getList(wechatSubscribeConfig.getNewsContent(), NewsMsgContent.class);
|
|
|
+ List<WxMpXmlOutNewsMessage.Item> items = new ArrayList<>();
|
|
|
+ for (NewsMsgContent newsMsgContent : newsMsgContents) {
|
|
|
+ WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
|
|
|
+ item.setTitle(newsMsgContent.getTitle());
|
|
|
+ item.setPicUrl(newsMsgContent.getImage());
|
|
|
+ item.setDescription(newsMsgContent.getDescription());
|
|
|
+ String url = newsMsgContent.getUrl().replace("{$ophost}", host);
|
|
|
+ if (!StringUtils.isEmpty(newsMsgContent.getBook_id())) {
|
|
|
+ url = String.format(UrlCons.FRONT_URL, adminConfig.getAppid(), host, UrlCons.MODEL_BOOK, adminConfig.getAdminId(), user.getId() + "")
|
|
|
+ .concat("&bookId=").concat(newsMsgContent.getBook_id()).concat("&").concat(newsMsgContent.getChapter_id());
|
|
|
+ }
|
|
|
+ item.setUrl(url);
|
|
|
+ }
|
|
|
+
|
|
|
+ return new TuWenBuilder().build(items, wxMessage, weixinService);
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ this.logger.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+
|
|
|
+ return new TextBuilder().build(String.format("@%s,感谢关注啦",user.getNickname()), wxMessage, weixinService);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理特殊请求,比如如果是扫码进来的,可以做相应处理
|
|
|
+ */
|
|
|
+ private WxMpXmlOutMessage handleSpecial(WxMpXmlMessage wxMessage)
|
|
|
+ throws Exception {
|
|
|
+ //TODO
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|