微信公衆號自定義回調開發

配置服務器信息

公衆號自定義服務器配置
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);
    }
}


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