当前位置:   article > 正文

909422229_微信公众号接收消息进行回复_wxmpxmloutmessage 超链接

wxmpxmloutmessage 超链接

技术交流群:958923746,有学习视频,文档等。

一、基于上一篇文章已经说了如何配置服务器,配置完成后给公众号发送消息会返回接收的消息内容,一个json串。

二、打开上一篇提供的项目地址,打开WechatController

代码如下:这个代码是我经过接收的消息处理过的,使用的是加密后的消息,经过解密后得到一个XML消息串。

从这里看的出来,在验证tokent的时候的链接与接收消息的链接是一致的,

验证token是GET请求,会自动在GET方法中处理token,进行验证。@GetMapping

处理消息的时候是POST,会在POST方法中处理消息。@PostMapping

  1. package com.qh.wechat.controller;
  2. import me.chanjar.weixin.mp.api.WxMpMessageRouter;
  3. import me.chanjar.weixin.mp.api.WxMpService;
  4. import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
  5. import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.web.bind.annotation.*;
  11. import com.alibaba.fastjson.JSONObject;
  12. import com.qh.wechat.builder.TextBuilder;
  13. import com.qh.wechat.service.QhChatService;
  14. import com.qh.wechat.utils.JsonUtils;
  15. import com.qh.wechat.utils.StringHelpers;
  16. /**
  17. * @author Binary Wang(https://github.com/binarywang)
  18. */
  19. @RestController
  20. @RequestMapping("/wechat/portal")
  21. public class WechatController {
  22. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  23. @Autowired
  24. private WxMpService wxService;
  25. @Autowired
  26. private WxMpMessageRouter router;
  27. @Autowired
  28. private QhChatService qhChatService;
  29. @GetMapping(produces = "text/plain;charset=utf-8")
  30. public String authGet(@RequestParam(name = "signature", required = false) String signature,
  31. @RequestParam(name = "timestamp", required = false) String timestamp,
  32. @RequestParam(name = "nonce", required = false) String nonce,
  33. @RequestParam(name = "echostr", required = false) String echostr) {
  34. this.logger.info("\n接收到来自微信服务器的认证消息:[{}, {}, {}, {}]", signature, timestamp, nonce, echostr);
  35. if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
  36. throw new IllegalArgumentException("请求参数非法,请核实!");
  37. }
  38. if (this.wxService.checkSignature(timestamp, nonce, signature)) {
  39. return echostr;
  40. }
  41. return "非法请求";
  42. }
  43. @PostMapping(produces = "application/xml; charset=UTF-8")
  44. public String post(@RequestBody String requestBody, @RequestParam("signature") String signature,
  45. @RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce,
  46. @RequestParam(name = "encrypt_type", required = false) String encType,
  47. @RequestParam(name = "msg_signature", required = false) String msgSignature) {
  48. // this.logger.info(
  49. // "\n接收微信请求:[signature=[{}], encType=[{}], msgSignature=[{}],"
  50. // + " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
  51. // signature, encType, msgSignature, timestamp, nonce, requestBody);
  52. if (!this.wxService.checkSignature(timestamp, nonce, signature)) {
  53. throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
  54. }
  55. String out = null;
  56. if (encType == null) {
  57. // 明文传输的消息
  58. WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);
  59. WxMpXmlOutMessage outMessage = this.route(inMessage);
  60. if (outMessage == null) {
  61. return "";
  62. }
  63. out = outMessage.toXml();
  64. } else if ("aes".equals(encType)) {
  65. this.logger.info("--------------------aes加密的消息");
  66. // aes加密的消息
  67. WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody,
  68. this.wxService.getWxMpConfigStorage(), timestamp, nonce, msgSignature);
  69. // this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
  70. // WxMpXmlOutMessage outMessage = this.route(inMessage);
  71. // if (outMessage == null) {
  72. // return "";
  73. // }
  74. JSONObject jsStr = JSONObject.parseObject(JsonUtils.toJson(inMessage));
  75. try {
  76. out = qhChatService.qhChat(jsStr);
  77. } catch (Exception e) {
  78. e.printStackTrace();
  79. }
  80. // this.logger.info(out+"-----------调用接口返回数据");
  81. WxMpXmlOutMessage build = new TextBuilder().build(out, inMessage, this.wxService);
  82. out = build.toEncryptedXml(this.wxService.getWxMpConfigStorage());
  83. // 组装XML返回给微信
  84. // out = outMessage
  85. // .toEncryptedXml(this.wxService.getWxMpConfigStorage());
  86. }
  87. this.logger.debug("\n组装回复信息:{}", out);
  88. return out;
  89. }
  90. private WxMpXmlOutMessage route(WxMpXmlMessage message) {
  91. try {
  92. return this.router.route(message);
  93. } catch (Exception e) {
  94. this.logger.error(e.getMessage(), e);
  95. }
  96. return null;
  97. }
  98. }

 

返回的消息也是一个XML,需要微信的API转成XML。调用该方法:

WxMpXmlOutMessage build = new TextBuilder().build(out, inMessage, this.wxService);
            out = build.toEncryptedXml(this.wxService.getWxMpConfigStorage());

将需要回复的消息拼成一个微信可以识别的一个串,返回给微信。消息内容就是out。可以在上面处理out,然后将out重新转成XML。

最后效果:展示出来的是一个正常的消息,而不是一个json串。下面是我调用图灵接口进行消息的回复。

不懂可以留言联系。再会。

技术交流群:958923746,有学习视频,文档等。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/274135
推荐阅读
相关标签
  

闽ICP备14008679号