微信公众号硬件功能开发

设备功能

在“设备功能”处添加产品,接入方案选择“平台基础接入方案”
添加产品
添加成功后,就有100个配额

服务器配置

设备功能配置
URL是后台回调接口地址,Token是为了验证回调信息的合法性的,EncodingAESKey随机生成即可

服务器配置回调校验开发

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

/**
 * 微信硬件平台配置校验
 *
 * @param appId
 * @param request
 * @param response
 */
@RequestMapping(value = "/hardware/{appId}", method = RequestMethod.GET)
@ApiOperation(value="微信硬件平台配置校验回调", notes="微信硬件平台配置校验回调")
public void receiveIotMsg(@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);
    String iotToken = "";
    if (officialAccountJsonBean.getCode() == ErrorCode.SUCCESS.getCode() && officialAccountJsonBean.getData() != null) {
        iotToken = officialAccountJsonBean.getData().getIotToken();
    }

    String curSign = SecurityUtils.encryptSHA1(iotToken, 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,Content-Type: application/json
消息、事件会以json格式的传输到后台

/**
* 微信硬件平台消息回调接口
*
* @param appId
* @return
*/
@RequestMapping(value = "/hardware/{appId}", method = {RequestMethod.POST}, headers = "Accept=application/json")
@ResponseBody
@ApiOperation(value="微信硬件平台消息回调", notes="微信硬件平台消息回调")
public WebJsonBean receiveIotMsg(@PathVariable("appId") String appId, @RequestBody DeviceBindMsg deviceBindMsg) {
   logger.info("收到微信硬件消息回调POST:{}", JSONObject.toJSONString(deviceBindMsg));

   HardwareMsgEvent hardwareMsgEvent = new HardwareMsgEvent(applicationContext, deviceBindMsg, appId);
   applicationContext.publishEvent(hardwareMsgEvent);

   return new WebJsonBean(ErrorCode.SUCCESS);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

来了就走下去

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值