• 浅析微信公众号订阅消息开发流程


      官方文档:https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html

    1、登录公众号管理平台:广告与服务 - 模板消息

      在这里需要添加“消息模板”(这里是需要拿到模板 id 的),以及查看“模板消息接口文档”,根据文档去做后台开发即可。

      模板库里没有合适的模板时,需要自己新建,等待审核周期有点长,7-15天,所以需要提前申请模板

    2、前提条件:

    (1)用户需关注了你的微信公众号

    (2)你有用户的 openId

    3、如何确定发送给哪些人?

    (1)如果你有关联性的成员,比如问题,需要发送给该问题领域的技术专家去回答,那就直接取关联的该领域的技术专家的 users 去遍历发送订阅消息即可

    (2)如果没有关联性的成员,比如资讯或每日一题,怎么办呢?

      1. 可以在页面放置微信公众号的二维码,让用户去扫码

      2. 扫码进入公众号,关注后,自动回复快捷链接

      3. 点击订阅数据库每日一题,即可进入每日一题页面,给予按钮进行订阅(订阅后在表里存储订阅的哪些用户即可,在发送订阅消息时,从这些用户里去取即可)

    4、具体开发步骤:

    (1)Controller 层:声明接口

    @ApiOperation("发送每日一题微信提醒")
    @PreAuthorize("hasRole('sys')")
    @PostMapping("/dailys/{dailyId}/remind")
    public OperationInfo remindDaily(@PathVariable Integer dailyId) throws EmcsCustomException {
      dailyService.sendWxRemind(dailyId);
      return OperationInfo.success("发送订阅消息成功");
    }

    (2)异步遍历订阅用户发送消息提醒

    @Async
    public void sendWxRemind(Integer dailyId) throws EmcsCustomException {
      ......   //获取订阅用户   List<User> users = getSubscribedUsers();   if (users == null || users.isEmpty()) {     log.error("每日一题订阅用户为空");     return;   }   users.forEach(user-> {     wxService.sendLessonRemind(user, "每日一题更新啦", title, startTime, "点击查看或退订", null, pagePath);   }); }

    (3)如何发送,看文档即可,比如这样

    public void sendEventStartRemind(User user, String title, String name, String time, String comment, String url) {
      ......
      requestBody.put("template_id", "第一步的模板ID");
      requestBody.put("url", url);
      requestBody.put("data", data);
    
      String accessToken = getAccessToken(WxPublicConfig.getInstance()).getAccess_token();
      wechatApi.sendPublicTemplateMessage(accessToken, requestBody);
    }

      这里比较重要的是第一步的模板id,以及 accessToken 的获取,也有文档

        /**
         * 获取微信token, 公众号,开放号,小程序
         */
        private WxTokenVO getAccessToken(IWxConfig config) {
            WxTokenVO token = wechatApi.getAccessToken(config.getAppID(), config.getAppSecret());
            if (token.getErrcode() != null && token.getErrcode() != 0) {
                log.error("获取微信账户token异常: {}, code: {}, message: {}", config.getAppID(), token.getErrcode(), token.getErrmsg());
            }
            log.info("获取{}token: {}", config.getAppID(), JSON.toJSONString(token));
            return token;
        }

      下面就是按文档用 OpenFeign 请求微信提供的接口去做对应事情即可,如这样:

    @FeignClient(name = "wechatApi", url = "https://api.weixin.qq.com")
    public interface WechatApi {
        /**
         * 获取微信账号access token, 公众账号,开放账号,小程序账号
         * https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
         */
        @RequestLine("GET /cgi-bin/token?grant_type=client_credential&appid={appId}&secret={appSecret}")
        WxTokenVO getAccessToken(@Param("appId") String appId,
                                 @Param("appSecret") String appSecret);/**
         * 发送微信公众号模板消息
         * https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html
         */
        @RequestLine("POST /cgi-bin/message/template/send?access_token={accessToken}")
        @Body("body")
        void sendPublicTemplateMessage(@Param("accessToken") String accessToken, JSONObject body);
    }

    5、接收微信服务器返回

      如果需要接收微信服务器的返回,然后根据发送消息是否发送成功,然后做一些自己的相关业务的话,就需要填写这个回调地址。

      在微信公众管理平台:设置与开发 -  基本配置 - 服务器配置,这里去进行回调地址配置

      具体如何做,看官方文档:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Access_Overview.html

  • 相关阅读:
    Java通过Mybatis实现批量插入数据到Oracle中
    SpringMVC+Spring+Mybatis整合,使用druid连接池,声明式事务,maven配置
    图片懒加载原理
    js参考---sort函数
    css参考---纯css实现三角形
    css参考---实现元素水平垂直居中
    javascript疑难问题---13、函数防抖
    javascript疑难问题---12、函数节流
    闭包执行注意(函数节流为例)
    javascript疑难问题---11、回调函数
  • 原文地址:https://www.cnblogs.com/goloving/p/16159918.html
Copyright © 2020-2023  润新知