spring-boot-route(二十三)开发微信公众号

在讲微信公众号开发以前,先来大概了解一下微信公众号。微信公众号大致上能够分为服务号和订阅号,订阅号和服务号的区别以下:html

  1. 服务号能够申请微信支付功能。
  2. 服务号只能由企业申请,订阅号能够有企业或我的申请。
  3. 订阅号和服务号每个月推送消息次数不一样,订阅号天天能够推送一次,服务号每个月能够推送四次。
  4. 服务号推送的消息会出如今用户的聊天列表中,而订阅号推送的消息显示在订阅号文件夹中。
  5. 还有一些其余接口功能的区别和限制,总的来讲服务号支持更高级的功能开发。

订阅号更加偏向于向用户传递咨询,通常各类技术类公众号都属于订阅号,订阅号的消息推送并不会有太显眼的提醒,若是你想让某个公众号的推送内容更加显眼,能够选择置为星标。置为星标后公众号会显示在全部订阅号的最顶部,同时收到消息后会有黄色五角星星标提醒。java

一 公众号配置服务器

微信官方提供了很是完善的接入文档,若是想了解文档的具体内容,直接浏览器搜索微信开发文档就能够了。可是为了方便开发,通常不会直接去根据微信开发文档进行开发,gitee上有许多开源项目对微信开发文档进行了封装,这里我使用mica-weixin开发包进行演示,mica-weixinjfinal-weixin的boot版本。git

配置服务器信息很简单,具体流程就是微信服务发送请求一个请求给业务服务器,业务服务器验证请求后给微信服务一个响应github

1.1 搭建业务服务

本地搭建一个spring-boot-weixin的项目,使用内网穿透工具进行穿透,使其能够与外网进行通讯。redis

1.1.1 引入mica-weixin依赖

<dependency>
    <groupId>net.dreamlu</groupId>
    <artifactId>mica-weixin</artifactId>
    <version>2.0.1</version>
</dependency>

1.1.2 配置公众号信息

mica-weixin经过配置文件进行公众号信息的配置,若是你想经过数据库配置公众号信息,能够参考我之前写过的一篇文章jfinal-weixin自定义配置支持多公众号spring

dream:
  weixin:
    wx-configs:
    - appId: xxxxxx
      appSecret: xxxxxx
      token: javatrip
      encodingAesKey: xxxxxx

appIdappSecret可在公众号后台进行查看,具体位置在菜单开发—>基本配置中,其中appSecret要妥善保管,如今公众号已经不支持查看appSecret了,若是你忘了appSecret,只能进行重置。数据库

1.1.3 开发消息校验接口

mica-weixin已经为咱们提供好了消息校验接口,只须要继承DreamMsgControllerAdapter就能够了。api

@WxMsgController("/weixin/wx")
public class WeiXinMsgController extends DreamMsgControllerAdapter {
    @Override
    protected void processInFollowEvent(InFollowEvent inFollowEvent) {
    }

    @Override
    protected void processInTextMsg(InTextMsg inTextMsg) {
    }

    @Override
    protected void processInMenuEvent(InMenuEvent inMenuEvent) {
    }
}

同时,须要开启缓存,因为mica-weixin的将access_token等信息放在了缓存中。在启动类上加@EnableCaching就开启了。浏览器

@SpringBootApplication
@EnableCaching
public class WeixinApplication {
    public static void main(String[] args) {
        SpringApplication.run(WeixinApplication.class, args);
    }
}

1.1.4 公众号后台配置服务器信息

使用内网穿透工具穿透内网地址,而后在公众号后台菜单开发—>基本配置中填写服务器配置信息。缓存

填写完成后点击启用,这样就完成了微信服务器和业务服务器的关系配置。开启开发者配置后,自动回复、自定义菜单等功能都不能正常使用了。这时候就须要去调用对应的接口实现这些功能。

二 实现各类消息接口

2.1 关注消息

在一步中,自定义类WeiXinMsgController中须要重写三个父类中的方法,其中processInFollowEvent()就是关注和取消关注的方法,取消关注后用户虽然不能收到消息,可是后台能够接收到用户取消关注的事件。

@Override
protected void processInFollowEvent(InFollowEvent inFollowEvent) {

    OutTextMsg defaultMsg = new OutTextMsg(inFollowEvent);
    // 关注
    if(InFollowEvent.EVENT_INFOLLOW_SUBSCRIBE.equals(inFollowEvent.getEvent())){
        // 可将关注用户录入db,此处能够获取到用户openid
        String openId = inFollowEvent.getFromUserName();
        // 查询db,根据响应消息类型封装消息体
        if("文本消息"){
            OutTextMsg otm = new OutTextMsg(inFollowEvent);
            otm.setContent("消息内容");
            render(otm);
            return;
        }else if("图片消息"){
            OutImageMsg oim = new OutImageMsg(inFollowEvent);
            // 这里须要调用微信提供的素材接口,将图片上传至素材库。
            oim.setMediaId("图片素材id");
            render(oim);
            return;
        }else if("图文消息"){
            OutNewsMsg onm = new OutNewsMsg(inFollowEvent);
            onm.addNews("标题","简介","图片地址","图文连接");
            render(onm);
            return;
        }else if("视频消息"){
            OutVideoMsg ovm = new OutVideoMsg(inFollowEvent);
            ovm.setTitle("标题");
            ovm.setDescription("简介");
            ovm.setMediaId("视频素材id");
            render(ovm);
            return;
        }else{
            defaultMsg.setContent("感谢关注");
        }
    }
    // 取消关注
    if(InFollowEvent.EVENT_INFOLLOW_UNSUBSCRIBE.equals(inFollowEvent.getEvent())){
        log.info("用户取消关注了");
        // 此处能够将取消关注的用户更新db
    }
}

2.2 关键词消息

响应内容跟关注消息同样,查询db去匹配关键词,然会根据消息内容封装对应的消息体进行返回,若是没匹配到关键词则回复统一的消息内容。processInTextMsg()方法就是用来回复关键词消息的。

@Override
protected void processInTextMsg(InTextMsg inTextMsg) {

    String content = inTextMsg.getContent();
    // 根据用户发送的content去查询db中的响应内容
    if("文本消息"){
        OutTextMsg otm = new OutTextMsg(inTextMsg);
        otm.setContent("消息内容");
        render(otm);
        return;
    }else if("图片消息"){
        OutImageMsg oim = new OutImageMsg(inTextMsg);
        // 这里须要调用微信提供的素材接口,将图片上传至素材库。
        oim.setMediaId("图片素材id");
        render(oim);
        return;
    }else if("图文消息"){
        OutNewsMsg onm = new OutNewsMsg(inTextMsg);
        onm.addNews("标题","简介","图片地址","图文连接");
        render(onm);
        return;
    }else if("视频消息"){
        OutVideoMsg ovm = new OutVideoMsg(inTextMsg);
        ovm.setTitle("标题");
        ovm.setDescription("简介");
        ovm.setMediaId("视频素材id");
        render(ovm);
        return;
    }else{
        OutTextMsg otm = new OutTextMsg(inTextMsg);
        otm.setContent("暂未查到关键词...");
    }
}

2.3 菜单消息

点击菜单后也是同样,经过processInMenuEvent()方法进行响应内容的回复。

@Override
protected void processInMenuEvent(InMenuEvent inMenuEvent) {
    String eventKey = inMenuEvent.getEventKey();
    // 根据用户发送的content去查询db中的响应内容
    if("文本消息"){
        OutTextMsg otm = new OutTextMsg(inMenuEvent);
        otm.setContent("消息内容");
        render(otm);
        return;
    }else if("图片消息"){
        OutImageMsg oim = new OutImageMsg(inMenuEvent);
        // 这里须要调用微信提供的素材接口,将图片上传至素材库。
        oim.setMediaId("图片素材id");
        render(oim);
        return;
    }else if("图文消息"){
        OutNewsMsg onm = new OutNewsMsg(inMenuEvent);
        onm.addNews("标题","简介","图片地址","图文连接");
        render(onm);
        return;
    }else if("视频消息"){
        OutVideoMsg ovm = new OutVideoMsg(inMenuEvent);
        ovm.setTitle("标题");
        ovm.setDescription("简介");
        ovm.setMediaId("视频素材id");
        render(ovm);
        return;
    }else{
        OutTextMsg otm = new OutTextMsg(inMenuEvent);
        otm.setContent("无效连接,请重试...");
    }
}

三 接口API调用

目前,微信提供的接口对订阅号的限制比较大,未认证的订阅号基本上只有接收消息的几个功能接口。

调用接口的时候须要传递token,获取token须要在微信后台中配置业务服务器的白名单。以下:

若是须要配置多个白名单ip,使用回车键将多个ip分隔开。

mica-weixin提供了全部的接口封装,具体可参考它的官方文档,若是要获取微信菜单,能够这样写:

@WxApi("weixin/api")
public class WeiXinApiController {
    @GetMapping("menu")
    @ResponseBody
    public String getMenu(){
        ApiResult menu = MenuApi.getMenu();
        return menu.getJson();
    }
}

@WxApi这个是它的自定义注解,其实就是包含了@RequestMapping@Controller

四 其余事项

4.1 多公众号配置

mica-weixin提供了多公众号配置的功能,使用ThreadLocalappid进行绑定。只须要简单配置便可实现多公众号配置。

dream:
  weixin:
    wx-configs:
      - appId: xxxxxx
        appSecret: xxxxxx
        token: javatrip
        encodingAesKey: xxxxxx
      - appId: xxxxxx
        appSecret: xxxxxx
        token: javatrip
        encodingAesKey: xxxxxx

4.2 redis配置

access_token的有效期是2小时,而且该接口有调用次数限制,mica-weixinaccess_token存储在redis中,避免每次调用接口都去获取access-token,所以项目须要配置redis。

spring:
  redis:
    host: localhost
    port: 6379

4.3 手动选择ThreadLocal

若是想要开发微信公众号的后台管理功能,多公众号的时候就须要手动去指定当前线程使用哪一个公众号信息。以下:

ApiConfigKit.setThreadLocalAppId(appid);

至此,SpringBoot开发微信公众号就算完成了,因为订阅号开放的接口太少了,好多功能不能正常演示。还有mica-weixin也许不是最好的选择,若是想试着开发微信公众号,能够在gitee上找一下开发包。至于我为何会使用mica-weixin,是由于我曾用过一段时间的jfinal框架,与之配套的微信开发包就是jfinal-weixin,也就是jfinal版的mica-weixin


本文示例代码已上传至github,点个star支持一下!

Spring Boot系列教程目录

spring-boot-route(一)Controller接收参数的几种方式

spring-boot-route(二)读取配置文件的几种方式

spring-boot-route(三)实现多文件上传

spring-boot-route(四)全局异常处理

spring-boot-route(五)整合Swagger生成接口文档

spring-boot-route(六)整合JApiDocs生成接口文档

spring-boot-route(七)整合jdbcTemplate操做数据库

spring-boot-route(八)整合mybatis操做数据库

spring-boot-route(九)整合JPA操做数据库

spring-boot-route(十)多数据源切换

spring-boot-route(十一)数据库配置信息加密

spring-boot-route(十二)整合redis作为缓存

spring-boot-route(十三)整合RabbitMQ

spring-boot-route(十四)整合Kafka

spring-boot-route(十五)整合RocketMQ

spring-boot-route(十六)使用logback生产日志文件

spring-boot-route(十七)使用aop记录操做日志

spring-boot-route(十八)spring-boot-adtuator监控应用

spring-boot-route(十九)spring-boot-admin监控服务

spring-boot-route(二十)Spring Task实现简单定时任务

spring-boot-route(二十一)quartz实现动态定时任务

spring-boot-route(二十二)实现邮件发送功能

spring-boot-route(二十三)开发微信公众号

这个系列的文章都是工做中频繁用到的知识,学完这个系列,应付平常开发绰绰有余。若是还想了解其余内容,扫面下方二维码告诉我,我会进一步完善这个系列的文章!

相关文章
相关标签/搜索