赞
踩
import com.clina.matron.component.bulid.ImageBuilder; import com.clina.matron.component.bulid.TextBuilder; import com.clina.matron.component.mp.WxMpConfiguration; import com.clina.matron.model.entity.GzhReplaySetting; import com.clina.matron.model.entity.WxApp; import com.clina.matron.model.entity.WxUser; import com.clina.matron.service.GzhReplaySettingService; import com.clina.matron.service.WxAppService; import com.clina.matron.service.WxUserService; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; 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.message.WxMpXmlOutTextMessage; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import java.util.Map; import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType; /** * Created by Clint * Date: 2020-08-17 21:43 * Description: */ @Slf4j @Component @AllArgsConstructor public class MsgHandler extends AbstractHandler { private final WxAppService wxAppService; private final WxUserService wxUserService; private final GzhReplaySettingService gzhReplaySettingService; @Override public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService, WxSessionManager sessionManager) { //组装回复消息 if (!wxMessage.getMsgType().equals(XmlMsgType.EVENT)) { WxMpXmlOutMessage rs; //TODO 可以选择将消息保存到本地 WxApp wxApp = wxAppService.findByWeixinSign(wxMessage.getToUser()); WxUser wxUser = wxUserService.getByOpenId(wxApp.getId(), wxMessage.getFromUser()); /** * 根据关键词回复消息(精确) */ if (StringUtils.isNotEmpty(wxMessage.getContent())) { GzhReplaySetting param = new GzhReplaySetting(); param.setGzhId(wxApp.getId()); param.setReplyType("message"); param.setKeyWord(wxMessage.getContent()); GzhReplaySetting replay = gzhReplaySettingService.getOneForQuery(param); if (replay != null && StringUtils.isNotEmpty(replay.getReplyContent())) { // 响应消息 return senfMessageByReplay(replay,wxMessage,wxMpService); } } } return null; } public WxMpXmlOutMessage senfMessageByReplay(GzhReplaySetting replay, WxMpXmlMessage wxMessage, WxMpService weixinService1){ if(replay.getMessageType().equals("text")){ log.info("回复文本消息..."); return new TextBuilder().build(replay.getReplyContent(), wxMessage,weixinService1); }else if(replay.getMessageType().equals("image")){ log.info("回复图片消息..."); return new ImageBuilder().build(replay.getReplyImage(), wxMessage,weixinService1); }else if(replay.getMessageType().equals("news")){ //发送文本消息 log.info("回复图文消息..."); WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item(); item.setDescription(replay.getReplyContent()); item.setPicUrl(replay.getReplyImage()); item.setTitle(replay.getReplyTitle()); item.setUrl(replay.getReplyUrl()); WxMpXmlOutNewsMessage m = WxMpXmlOutMessage.NEWS() .fromUser(wxMessage.getToUser()) .toUser(wxMessage.getFromUser()) .addArticle(item) .build(); return m; } return null; } }
package com.clina.matron.component.bulid;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class AbstractBuilder {
protected final Logger logger = LoggerFactory.getLogger(getClass());
public abstract WxMpXmlOutMessage build(String content,
WxMpXmlMessage wxMessage, WxMpService weixinService);
}
package com.clina.matron.component.handler;
import cn.hutool.json.JSONUtil;
import com.clina.matron.common.WeChatConstants;
import com.clina.matron.component.bulid.ImageBuilder;
import com.clina.matron.component.bulid.TextBuilder;
import com.clina.matron.component.mp.WxMpConfiguration;
import com.clina.matron.model.entity.GzhReplaySetting;
import com.clina.matron.model.entity.WxApp;
import com.clina.matron.model.entity.WxUser;
import com.clina.matron.service.GzhReplaySettingService;
import com.clina.matron.service.WxAppService;
import com.clina.matron.service.WxUserService;
import com.clina.matron.util.DateTimeUtils;
import lombok.AllArgs
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。