微信公众平台开发教程(四)_点击事件处理
在我们配置了微信公众号的基本配置之后
用户每次触发点击事件之后,腾讯都会回调到我们的这个URL,然后我们再处理对应逻辑,返回给腾讯。
下面以我暴露出来/wechat.do来进行举例:@RequestMapping(value="/wechat.do",method = RequestMethod.POST, produces = { "application/xml;charset=UTF-8" })
public @ResponseBody String processWxMessage( @RequestBody String requestBody, @RequestParam(value = "signature", required = false) String signature, @RequestParam(value = "timestamp", required = false) String timestamp, @RequestParam(value = "nonce", required = false) String nonce, @RequestParam(value = "encrypt_type", required = false) String encrypt_type, @RequestParam(value = "msg_signature", required = false) String msg_signature, HttpServletRequest request,HttpServletResponse response) throws Exception { String respMessage = null;
//接收微信消息实体类 ReceiveMessage message = new ReceiveMessage(); Map config=null; try{ if(StringUtils.isNotBlank(encrypt_type) && !"raw".equals(encrypt_type) && "aes".equals(encrypt_type)){ logger.info("消息经过加密处理!");
//获取本地存储的公众号配置信息 String sysytemAppid = (String) wxPropertiesBean.getProperties("system").get("system_appid"); config = this.wxMpService.queryWxAccountByCode(sysytemAppid); String encodingaeskey = (String) config.get("encoding_aes_key"); String token = (String) config.get("account_token"); String appid = (String) config.get("account_appid");
//解密消息 WXBizMsgCrypt msgCryPt = new WXBizMsgCrypt(token, encodingaeskey, appid); requestBody = msgCryPt.decryptMsg(msg_signature, timestamp, nonce, requestBody); }
message = MessageUtil.parseXml(requestBody);
//根据腾讯返回信息确认事件触发类型后进行处理,最后转换成xml格式的String返回给腾讯 respMessage = this.wxService.analyzeMessage(message, config); }catch (Exception e){ logger.error(e.getMessage()); return null; } return respMessage; }