dev --- 3.8.0.A版本, openProject引用 ; formao-live --- 3.7.0.B 版本, formallProject引用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 5.2 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 消息机制未实现,下面为通知回调中设置的代码部分
  2. 以下代码可通过腾讯全网发布测试用例
  3. ```
  4. @RestController
  5. @RequestMapping("notify")
  6. public class NotifyController extends WechatThridBaseController {
  7. @Autowired
  8. protected WxOpenServiceDemo wxOpenService;
  9. @RequestMapping("receive_ticket")
  10. public Object receiveTicket(@RequestBody(required = false) String requestBody, @RequestParam("timestamp") String timestamp,
  11. @RequestParam("nonce") String nonce, @RequestParam("signature") String signature,
  12. @RequestParam(name = "encrypt_type", required = false) String encType,
  13. @RequestParam(name = "msg_signature", required = false) String msgSignature) {
  14. this.logger.info(
  15. "\n接收微信请求:[signature=[{}], encType=[{}], msgSignature=[{}],"
  16. + " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
  17. signature, encType, msgSignature, timestamp, nonce, requestBody);
  18. if (!StringUtils.equalsIgnoreCase("aes", encType) || !wxOpenService.getWxOpenComponentService().checkSignature(timestamp, nonce, signature)) {
  19. throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
  20. }
  21. // aes加密的消息
  22. WxOpenXmlMessage inMessage = WxOpenXmlMessage.fromEncryptedXml(requestBody, wxOpenService.getWxOpenConfigStorage(), timestamp, nonce, msgSignature);
  23. this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
  24. String out = null;
  25. try {
  26. out = wxOpenService.getWxOpenComponentService().route(inMessage);
  27. } catch (WxErrorException e) {
  28. throw new ResponseException(ErrorCodeEnum.ERROR, e);
  29. }
  30. this.logger.debug("\n组装回复信息:{}", out);
  31. return out;
  32. }
  33. @RequestMapping("{appId}/callback")
  34. public Object callback(@RequestBody(required = false)String requestBody,
  35. @PathVariable ("appId") String appId,
  36. @RequestParam("signature") String signature,
  37. @RequestParam("timestamp") String timestamp,
  38. @RequestParam("nonce") String nonce,
  39. @RequestParam("openid") String openid,
  40. @RequestParam("encrypt_type") String encType,
  41. @RequestParam("msg_signature") String msgSignature) {
  42. this.logger.info(
  43. "\n接收微信请求:[appId=[{}], openid=[{}], signature=[{}], encType=[{}], msgSignature=[{}],"
  44. + " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
  45. appId, openid, signature, encType, msgSignature, timestamp, nonce, requestBody);
  46. logger.info("query:"+getHttpServletRequest().getQueryString()+"\nbody:"+requestBody);
  47. if (!StringUtils.equalsIgnoreCase("aes", encType) || !wxOpenService.getWxOpenComponentService().checkSignature(timestamp, nonce, signature)) {
  48. throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
  49. }
  50. String out = "";
  51. // aes加密的消息
  52. WxMpXmlMessage inMessage = WxOpenXmlMessage.fromEncryptedMpXml(requestBody, wxOpenService.getWxOpenConfigStorage(), timestamp, nonce, msgSignature);
  53. this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
  54. // 全网发布测试用例
  55. if (StringUtils.equalsAnyIgnoreCase(appId, "wxd101a85aa106f53e", "wx570bc396a51b8ff8")) {
  56. try {
  57. if (StringUtils.equals(inMessage.getMsgType(), "text")) {
  58. if (StringUtils.equals(inMessage.getContent(), "TESTCOMPONENT_MSG_TYPE_TEXT")) {
  59. out = new WxOpenCryptUtil(wxOpenService.getWxOpenConfigStorage()).encrypt(
  60. WxMpXmlOutMessage.TEXT().content("TESTCOMPONENT_MSG_TYPE_TEXT_callback")
  61. .fromUser(inMessage.getToUser())
  62. .toUser(inMessage.getFromUser())
  63. .build()
  64. .toXml()
  65. );
  66. } else if (StringUtils.startsWith(inMessage.getContent(), "QUERY_AUTH_CODE:")) {
  67. String msg = inMessage.getContent().replace("QUERY_AUTH_CODE:", "") + "_from_api";
  68. WxMpKefuMessage kefuMessage = WxMpKefuMessage.TEXT().content(msg).toUser(inMessage.getFromUser()).build();
  69. wxOpenService.getWxOpenComponentService().getWxMpServiceByAppid(appId).getKefuService().sendKefuMessage(kefuMessage);
  70. }
  71. } else if (StringUtils.equals(inMessage.getMsgType(), "event")) {
  72. WxMpKefuMessage kefuMessage = WxMpKefuMessage.TEXT().content(inMessage.getEvent() + "from_callback").toUser(inMessage.getFromUser()).build();
  73. wxOpenService.getWxOpenComponentService().getWxMpServiceByAppid(appId).getKefuService().sendKefuMessage(kefuMessage);
  74. }
  75. } catch (WxErrorException e) {
  76. logger.error("callback", e);
  77. }
  78. }
  79. return out;
  80. }
  81. }
  82. ```