当前位置:   article > 正文

微信公众号java01_wxmpxmloutmessage 解析 聊内内容对象

wxmpxmloutmessage 解析 聊内内容对象

最近在研究微信公众号java开发,今天写一个简单的程序(我使用的是自己的微信测试号):

1、这里使用微信sdk

maven

  1.    <dependency>
  2. <groupId>com.github.binarywang</groupId>
  3. <artifactId>weixin-java-mp</artifactId>
  4. <version>2.9.0</version>
  5. </dependency>

springMVC的环境配置就不多说了。


2、wechatContorller,

  1. /**
  2. * 微信验证
  3. *
  4. */
  5. @RequestMapping("/index.do")
  6. public void index(HttpServletRequest request, HttpServletResponse response,
  7. HttpSession session) throws ServletException, IOException {
  8. wxMpServiceInstance.doResponse(request, response);
  9. }

3、WxMpServiceInstance

  1. public class WxMpServiceInstance{
  2. private static WxMpService wxMpService; // API 服务接口对象
  3. private WxMpConfigStorage wxMpConfigStorage; // 配置信息存储对象
  4. public static WxMpMessageRouter wxMpMessageRouter; // 微信消息路由器对象
  5. private WechatSendInformationService sendInformationService;
  6. @Resource
  7. private WechatManagementService wechatManagementService;
  8. @Resource
  9. private WechatRedreceiveRecordService wechatRedreceiveRecordService;
  10. private WxMpServiceInstance() {
  11. try {
  12. WxMpXMLInMemoryConfigStorage config = WxMpXMLInMemoryConfigStorage
  13. .fromXml();
  14. //微信公众号相关
  15. wxMpConfigStorage = config;
  16. wxMpService = new WxMpServiceImpl();
  17. wxMpService.setWxMpConfigStorage(config);
  18. wxMpMessageRouter = new WxMpMessageRouter(wxMpService);
  19. this.addEvtRouterRule();
  20. } catch (JAXBException e) {
  21. throw new RuntimeException(e);
  22. }
  23. }
  24. public WxMpService getWxMpService() {
  25. return wxMpService;
  26. }
  27. public void setWxMpService(WxMpService wxMpService) {
  28. this.wxMpService = wxMpService;
  29. }
  30. public WxMpConfigStorage getWxMpConfigStorage() {
  31. return wxMpConfigStorage;
  32. }
  33. public void setWxMpConfigStorage(WxMpConfigStorage wxMpConfigStorage) {
  34. this.wxMpConfigStorage = wxMpConfigStorage;
  35. }
  36. public WxMpMessageRouter getWxMpMessageRouter() {
  37. return wxMpMessageRouter;
  38. }
  39. public void setWxMpMessageRouter(WxMpMessageRouter wxMpMessageRouter) {
  40. this.wxMpMessageRouter = wxMpMessageRouter;
  41. }

  1. public void doResponse(HttpServletRequest request,
  2. HttpServletResponse response) throws ServletException, IOException {
  3. String signature = request.getParameter("signature");
  4. String nonce = request.getParameter("nonce");
  5. String timestamp = request.getParameter("timestamp");
  6. response.setContentType("text/html;charset=utf-8");
  7. response.setStatus(HttpServletResponse.SC_OK);
  8. if (!wxMpService.checkSignature(timestamp, nonce, signature)) {
  9. // 消息签名不正确,说明不是公众平台发过来的消息
  10. response.getWriter().println("非法请求");
  11. return; // 非法
  12. }
  13. // 合法
  14. String echostr = request.getParameter("echostr");
  15. if (StringUtils.isNotBlank(echostr)) {
  16. // 说明是一个仅仅用来验证的请求,回显echostr
  17. response.getWriter().println(echostr);
  18. return;
  19. }
  20. // 获取消息加密类型
  21. String encryptType = StringUtils.isBlank(request
  22. .getParameter("encrypt_type")) ? "raw" : request
  23. .getParameter("encrypt_type");
  24. WxMpXmlMessage inMessage = null; // 输入消息
  25. if ("raw".equals(encryptType)) {
  26. // 明文传输的消息
  27. try {
  28. inMessage = WxMpXmlMessage.fromXml(request.getInputStream());
  29. } catch (Exception e) {
  30. e.printStackTrace();
  31. }
  32. } else if ("aes".equals(encryptType)) {
  33. // 是aes加密的消息
  34. String msgSignature = request.getParameter("msg_signature");
  35. inMessage = WxMpXmlMessage.fromEncryptedXml(
  36. request.getInputStream(), wxMpConfigStorage, timestamp,
  37. nonce, msgSignature);
  38. } else {
  39. response.getWriter().println("不可识别的加密类型");
  40. return;
  41. }
  42. // 通过微信消息路由器处理输入消息inMessage,得到返回消息 outMessage
  43. WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage);
  44. if (outMessage != null) {
  45. if ("raw".equals(encryptType)) {
  46. response.getWriter().write(outMessage.toXml());
  47. } else if ("aes".equals(encryptType)) {
  48. response.getWriter().write(
  49. outMessage.toEncryptedXml(wxMpConfigStorage));
  50. }
  51. return;
  52. }
  53. }
  1. /**
  2. * 关注自动回复路由规则
  3. *
  4. */
  5. private void addEvtRouterRule(){
  6. WxMpMessageHandler handler = new WxMpMessageHandler() {
  7. @Override
  8. public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
  9. Map<String, Object> context, WxMpService wxMpService,
  10. WxSessionManager sessionManager) throws WxErrorException {
  11. String strOut = "欢迎你的关注";
  12. WxMpXmlOutTextMessage m = WxMpXmlOutMessage.TEXT()
  13. .content(strOut).fromUser(wxMessage.getToUser())
  14. .toUser(wxMessage.getFromUser()).build();
  15. return m;
  16. }
  17. };
  18. wxMpMessageRouter.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.SUBSCRIBE).handler(handler).end();
  19. }}



4、WxMpXMLInMemoryConfigStorage

  1. class WxMpXMLInMemoryConfigStorage extends WxMpInMemoryConfigStorage {
  2. // 将微信配置文件 /weixin.config.xml 解析为 WxMpXMLInMemoryConfigStorage对象返回
  3. public static WxMpXMLInMemoryConfigStorage fromXml(InputStream is) throws JAXBException {
  4. //微信配置参数
  5. WxMpXMLInMemoryConfigStorage cfg = new WxMpXMLInMemoryConfigStorage();
  6. cfg.setAppId("appid");
  7. cfg.setSecret("随机数");
  8. cfg.setToken("token");
  9. // cfg.setAesKey("密钥,测试号不用填写");
  10. return cfg;
  11. }
  12. }

5、运行项目,配置测试号中的url,token




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

闽ICP备14008679号