微信公众号自定义回调开发

配置服务器信息

公众号自定义服务器配置
URL是后台回调接口地址,Token是为了验证回调信息的合法性的,EncodingAESKey随机生成即可

开发公众号配置校验回调开发

配置校验回调是以GET方式回调的,同消息事件回调是一个地址,只是请求方式不同。
如果验证成功,则响应回调请求的echostr值,否则啥也不返回。
代码示例:

/**
 * 微信公众号配置校验
 *
 * @param appId
 * @param request
 * @param response
 */
@RequestMapping(value = "/webchat/{appId}", method = {RequestMethod.GET})
@ApiOperation(value="微信公众号配置校验回调", notes="微信公众号配置校验回调")
public void receiveOfficialMsgGet(@PathVariable("appId") String appId, HttpServletRequest request, HttpServletResponse response) {
    logger.info("收到微信回调GET:{}", appId);

    // 微信加密签名
    String signature = request.getParameter("signature");
    // 时间戳
    String timestamp = request.getParameter("timestamp");
    // 随机数
    String nonce = request.getParameter("nonce");
    // 随机字符串
    String echostr = request.getParameter("echostr");

    Map<String, String[]> map = request.getParameterMap();
    for (Map.Entry<String, String[]> entry : map.entrySet()) {
        logger.info("{}={}", entry.getKey(), entry.getValue() != null ? entry.getValue()[0] : "");
    }

    // 获取公众号信息
    SingleParamRequest<String> singleParamRequest = new SingleParamRequest<>();
    singleParamRequest.setData(appId);
    ServiceJsonBean<OfficialAccount> officialAccountJsonBean = weiXinOfficialAccountService.getOfficialAccountByAppId(singleParamRequest);
    logger.info("获取到微信公众号信息:{}", JSONObject.toJSONString(officialAccountJsonBean));
    String officialToken = "";
    if (officialAccountJsonBean.getCode() == ErrorCode.SUCCESS.getCode() && officialAccountJsonBean.getData() != null) {
        officialToken = officialAccountJsonBean.getData().getOfficialToken();
    }

    String curSign = SecurityUtils.encryptSHA1(officialToken, timestamp, nonce);
    if (StringUtils.isEquals(signature, curSign) && StringUtils.isNotEmpty(echostr)) {
        logger.info("验证成功!");
        try {
            response.getWriter().write(echostr);
        } catch (IOException e) {
            logger.error("response输出异常", e);
        }
    }
}

消息、事件回调

消息、事件回调跟校验回调是同一个接口地址,但是请求方式为POST
消息、事件会以xml格式的传输到后台,后台解析xml进行处理

/**
 * 接收微信公众号消息或事件
 *
 * @param appId
 * @param request
 * @return
 */
@RequestMapping(value = "/webchat/{appId}", method = {RequestMethod.POST})
@ResponseBody
@ApiOperation(value="微信公众号消息或事件回调", notes="微信公众号消息或事件回调")
public void receiveOfficialMsgPost(@PathVariable("appId") String appId, HttpServletRequest request) {
    logger.info("收到微信回调POST:{}", appId);

    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
        String line = "";
        StringBuffer buffer = new StringBuffer();
        while ((line = br.readLine()) != null) {
            buffer.append(line);
        }
        String notifyXml = buffer.toString();
        logger.info("收到微信回调:{}", notifyXml);

        Map<String, String> xmlMap = XmlUtils.xmlToMap(notifyXml);

        // 发布事件
        NormalMsgEvent normalMsgEvent = new NormalMsgEvent(applicationContext, xmlMap, appId);
        applicationContext.publishEvent(normalMsgEvent);
    } catch (Exception e) {
        logger.error("微信消息回调异常", e);
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章