WxPushReceptionController.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package com.cybertogether.wx.notice.web;
  2. import com.cybertogether.wx.notice.infrastructure.Downstream;
  3. import com.cybertogether.wx.notice.infrastructure.HttpClientUtils;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.web.bind.annotation.*;
  8. /**
  9. * 接收微信推送通知
  10. * 授权流程文档
  11. * https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1453779503&token=&lang=
  12. */
  13. @RestController
  14. @RequestMapping("/wx/pushReception")
  15. public class WxPushReceptionController {
  16. private static final Logger LOGGER = LoggerFactory.getLogger(WxPushReceptionController.class);
  17. @Autowired
  18. Downstream downstream;
  19. /**
  20. * 接收微信的授权事件通知
  21. * 开发配置-开发资料-授权事件接收配置
  22. * localhost:8080/pandora/wx/pushReception/authorization?timestamp=34353535&nonce=4342344&msg_signature=ee661ba5dbbf82d488aaf18883808411d9c73b31
  23. */
  24. @PostMapping("/authorization")
  25. public String receptionAuthorizationEvent(@RequestParam("timestamp") String timestamp,
  26. @RequestParam("nonce") String nonce,
  27. @RequestParam("msg_signature") String msgSignature,
  28. @RequestBody String encryptedXmlBodyText) {
  29. String urlParams = String.format("?timestamp=%s&nonce=%s&msg_signature=%s", timestamp, nonce, msgSignature);
  30. for (String url : downstream.getUrls()) {
  31. try {
  32. HttpClientUtils.postData(url + urlParams, encryptedXmlBodyText);
  33. }catch (Exception e) {
  34. // ignore
  35. }
  36. }
  37. return "success";
  38. }
  39. /**
  40. * 接收微信消息与事件通知
  41. * 开发配置-开发资料-消息与事件接收配置
  42. */
  43. @PostMapping("/msgEvent/{appId}")
  44. public String receptionMsgEvent(@RequestBody String requestBody, @PathVariable String appId) {
  45. LOGGER.info("收到微信[消息与事件]推送通知,appId:" + appId + " requestBody: " + requestBody);
  46. return "success";
  47. }
  48. }