Loading

公众号对接

背景

最近在完成自己的个人抽奖项目之后,想着为项目提供一个抽奖入口。分析后选择使用公众号来作为入口。

以下是公众号的具体接入流程。

具体接入流程

创建自己的公众号

具体步骤可以去网上搜,网上资源很多的照着来就好。这里就简单贴一个

如何建立一个微信公众号(个人)

接入自己的服务器

公众号是可以设置相关回复的,但前提是你自己没有对接自己的服务器,一旦对接了自己的服务器,相关的能力将会失效。

第一步:填写服务器配置

登录微信公众平台官网后,在公众平台官网的开发-基本设置页面,勾选协议成为开发者,点击“修改配置”按钮,填写服务器地址(URL)、Token和EncodingAESKey,其中URL是开发者用来接收微信消息和事件的接口URL。Token可由开发者可以任意填写,用作生成签名(该Token会和接口URL中包含的Token进行比对,从而验证安全性)。EncodingAESKey由开发者手动填写或随机生成,将用作消息体加解密密钥。

同时,开发者可选择消息加解密方式:明文模式、兼容模式和安全模式。模式的选择与服务器配置在提交后都会立即生效,请开发者谨慎填写及选择。加解密方式的默认状态为明文模式,选择兼容模式和安全模式需要提前配置好相关加解密代码,详情请参考消息体签名及加解密部分的文档

第二步:验证消息的确来自微信服务器

开发者提交信息后,微信服务器将发送GET请求到填写的服务器地址URL上 。该请求是微信为了验证该服务器的有效性

/**
 * 处理微信服务器发来的get请求,进行签名的验证
 * <p>
 * appid     微信端AppID
 * signature 微信端发来的签名
 * timestamp 微信端发来的时间戳
 * nonce     微信端发来的随机字符串
 * echostr   微信端发来的验证字符串
 */
@GetMapping(produces = "text/plain;charset=utf-8")
public String validate(@PathVariable String appid,
                       @RequestParam(value = "signature", required = false) String signature,
                       @RequestParam(value = "timestamp", required = false) String timestamp,
                       @RequestParam(value = "nonce", required = false) String nonce,
                       @RequestParam(value = "echostr", required = false) String echostr) {
    try {
        logger.info("微信公众号验签信息{}开始 [{}, {}, {}, {}]", appid, signature, timestamp, nonce, echostr);
        if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
            throw new IllegalArgumentException("请求参数非法,请核实!");
        }
        boolean check = wxValidateService.checkSign(signature, timestamp, nonce);
        logger.info("微信公众号验签信息{}完成 check:{}", appid, check);
        if (!check) {
            return null;
        }
        return echostr;
    } catch (Exception e) {
        logger.error("微信公众号验签信息{}失败 [{}, {}, {}, {}]", appid, signature, timestamp, nonce, echostr, e);
        return null;
    }
}

第三步:依据接口文档实现业务逻辑

验证成功后,就可以基于之前配置的地址与微信服务器进行对接。微信服务器将基于配置的地址发送post请求,携带公众号内的相关数据信息。 我们就可以基于这个接口向下游开发相关的业务。

/**
 * 此处是处理微信服务器的消息转发的
 */
@PostMapping(produces = "application/xml; charset=UTF-8")
public String post(@PathVariable String appid,
                   @RequestBody String requestBody,
                   @RequestParam("signature") String signature,
                   @RequestParam("timestamp") String timestamp,
                   @RequestParam("nonce") String nonce,
                   @RequestParam("openid") String openid,
                   @RequestParam(name = "encrypt_type", required = false) String encType,
                   @RequestParam(name = "msg_signature", required = false) String msgSignature) {
    try {
        logger.info("接收微信公众号信息请求{}开始 {}", openid, requestBody);
        MessageTextEntity message = XmlUtil.xmlToBean(requestBody, MessageTextEntity.class);
        BehaviorMatter behaviorMatter = new BehaviorMatter();
        behaviorMatter.setOpenId(openid);
        behaviorMatter.setFromUserName(message.getFromUserName());
        behaviorMatter.setMsgType(message.getMsgType());
        behaviorMatter.setContent(StringUtils.isBlank(message.getContent()) ? null : message.getContent().trim());
        behaviorMatter.setEvent(message.getEvent());
        behaviorMatter.setCreateTime(new Date(Long.parseLong(message.getCreateTime()) * 1000L));
        // 处理消息
        String result = wxReceiveService.doReceive(behaviorMatter);
        logger.info("接收微信公众号信息请求{}完成 {}", openid, result);
        return result;
    } catch (Exception e) {
        logger.error("接收微信公众号信息请求{}失败 {}", openid, requestBody,e);
        return "";
    }
}

以上就是公共号对接的具体流程,不得不说,公众号的文档的阅读性还是可以的,没有花费太多时间就对接好了。

posted @ 2023-09-01 17:18  花园SON  阅读(4)  评论(0编辑  收藏  举报