|
| 1 | +消息机制未实现,下面为通知回调中设置的代码部分 |
| 2 | +``` |
| 3 | +@RestController |
| 4 | +@RequestMapping("notify") |
| 5 | +public class NotifyController extends WechatThridBaseController { |
| 6 | + @Autowired |
| 7 | + protected WxOpenServiceDemo wxOpenService; |
| 8 | + @RequestMapping("receive_ticket") |
| 9 | + public Object receiveTicket(@RequestBody(required = false) String requestBody, @RequestParam("timestamp") String timestamp, |
| 10 | + @RequestParam("nonce") String nonce, @RequestParam("signature") String signature, |
| 11 | + @RequestParam(name = "encrypt_type", required = false) String encType, |
| 12 | + @RequestParam(name = "msg_signature", required = false) String msgSignature) { |
| 13 | + this.logger.info( |
| 14 | + "\n接收微信请求:[signature=[{}], encType=[{}], msgSignature=[{}]," |
| 15 | + + " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ", |
| 16 | + signature, encType, msgSignature, timestamp, nonce, requestBody); |
| 17 | +
|
| 18 | + if (!StringUtils.equalsIgnoreCase("aes", encType) || !wxOpenService.getWxOpenComponentService().checkSignature(timestamp, nonce, signature)) { |
| 19 | + throw new IllegalArgumentException("非法请求,可能属于伪造的请求!"); |
| 20 | + } |
| 21 | +
|
| 22 | + // aes加密的消息 |
| 23 | + WxOpenXmlMessage inMessage = WxOpenXmlMessage.fromEncryptedXml(requestBody, wxOpenService.getWxOpenConfigStorage(), timestamp, nonce, msgSignature); |
| 24 | + this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString()); |
| 25 | + String out = null; |
| 26 | + try { |
| 27 | + out = wxOpenService.getWxOpenComponentService().route(inMessage); |
| 28 | + } catch (WxErrorException e) { |
| 29 | + throw new ResponseException(ErrorCodeEnum.ERROR, e); |
| 30 | + } |
| 31 | +
|
| 32 | + this.logger.debug("\n组装回复信息:{}", out); |
| 33 | +
|
| 34 | + return out; |
| 35 | + } |
| 36 | + @RequestMapping("{appId}/callback") |
| 37 | + public Object callback(@RequestBody(required = false)String requestBody, |
| 38 | + @PathVariable ("appId") String appId, |
| 39 | + @RequestParam("signature") String signature, |
| 40 | + @RequestParam("timestamp") String timestamp, |
| 41 | + @RequestParam("nonce") String nonce, |
| 42 | + @RequestParam("openid") String openid, |
| 43 | + @RequestParam("encrypt_type") String encType, |
| 44 | + @RequestParam("msg_signature") String msgSignature) { |
| 45 | + this.logger.info( |
| 46 | + "\n接收微信请求:[appId=[{}], openid=[{}], signature=[{}], encType=[{}], msgSignature=[{}]," |
| 47 | + + " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ", |
| 48 | + appId, openid, signature, encType, msgSignature, timestamp, nonce, requestBody); |
| 49 | + logger.info("query:"+getHttpServletRequest().getQueryString()+"\nbody:"+requestBody); |
| 50 | + if (!StringUtils.equalsIgnoreCase("aes", encType) || !wxOpenService.getWxOpenComponentService().checkSignature(timestamp, nonce, signature)) { |
| 51 | + throw new IllegalArgumentException("非法请求,可能属于伪造的请求!"); |
| 52 | + } |
| 53 | +
|
| 54 | + String out = ""; |
| 55 | + // aes加密的消息 |
| 56 | + WxMpXmlMessage inMessage = WxOpenXmlMessage.fromEncryptedMpXml(requestBody, wxOpenService.getWxOpenConfigStorage(), timestamp, nonce, msgSignature); |
| 57 | + this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString()); |
| 58 | + //wxOpenService.getWxOpenComponentService().getWxMpServiceByAppid(appId); |
| 59 | + return out; |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
0 commit comments