接上一篇内容https://blog.csdn.net/YISHENGYOUNI95/article/details/134383219
因为设置了公众号的服务器配置,导致之前设置的公众号自动回复失效了
需要通过之前配置的接口实现自动回复
参考一下官方文档回复文本消息 | 微信开放文档
根据用户发送的消息,返回固定的信息
代码如下
@PostMapping(value = "index")public String index(HttpServletRequest request,@RequestBody(required = false) WxServiceMsgDto wxServiceMsgDto) {Map<String, String[]> map = request.getParameterMap();log.info("事件参数: ");Set<Map.Entry<String, String[]>> entries = map.entrySet();for (Map.Entry<String, String[]> entry : entries) {System.out.println(entry.getKey() + ": " + Arrays.toString(entry.getValue()));}log.info("所有事件: {}", wxServiceMsgDto);//处理带参数请求if (null != wxServiceMsgDto &&!StrUtil.isEmpty(wxServiceMsgDto.getMsgType())) {//关注if (!StrUtil.isEmpty(wxServiceMsgDto.getEvent()) &&!StrUtil.isEmpty(wxServiceMsgDto.getEventKey()) &&!"null".equals(wxServiceMsgDto.getEventKey()) &&"subscribe".equals(wxServiceMsgDto.getEventKey()) &&"subscribe".equals(wxServiceMsgDto.getEvent())) {log.info("关注人OpenID: {}", wxServiceMsgDto.getFromUserName());String unionId = weChatService.getUserInfo(wxServiceMsgDto.getFromUserName());customerService.updateAccountOpenID(unionId, wxServiceMsgDto.getFromUserName());}// 文本消息if ("text".equalsIgnoreCase(wxServiceMsgDto.getMsgType())) {String content = wxServiceMsgDto.getContent();if (content.contains("停车") || content.contains("投诉") || content.contains("发票") || content.contains("电话")) {return wxMiniService.generateContent(wxServiceMsgDto.getFromUserName(), wxServiceMsgDto.getToUserName(), "XXXXXXXX");}}}return "";}
public String generateContent(String toUserNameData, String fromUserNameData, String contents) {// 创建document对象Document document = DocumentHelper.createDocument();// 创建根节点bookRootElement xml = document.addElement("xml");// 向根节点中添加第一个节点Element toUserName = xml.addElement("ToUserName");// 向子节点中添加属性toUserName.addCDATA(toUserNameData);Element fromUserName = xml.addElement("FromUserName");fromUserName.addCDATA(fromUserNameData);Element createTime = xml.addElement("CreateTime");createTime.addCDATA(String.valueOf(System.currentTimeMillis()));Element msgType = xml.addElement("MsgType");msgType.addCDATA("text");Element content = xml.addElement("Content");content.addCDATA(contents);String asXML = document.getRootElement().asXML();log.info("返回信息: {}", asXML);return asXML;}
@XmlRootElement(name = "xml")
@XmlAccessorType(XmlAccessType.FIELD)
@Data
public class WxServiceMsgDto {@XmlElement(name = "Event")private String event;@XmlElement(name = "Content")private String content;@XmlElement(name = "MsgType")private String msgType;@XmlElement(name = "ToUserName")private String toUserName;@XmlElement(name = "EventKey")private String eventKey;@XmlElement(name = "CreateTime")private String createTime;/*** fromUserName为关注人的openId**/@XmlElement(name = "FromUserName")private String fromUserName;}