在講微信公眾號開發之前,先來大概了解一下微信公眾號。微信公眾號大體上可以分為服務號和訂閱號,訂閱號和服務號的區別如下:
- 服務號可以申請微信支付功能。
- 服務號只能由企業申請,訂閱號可以有企業或個人申請。
- 訂閱號和服務號每月推送消息次數不同,訂閱號每天可以推送一次,服務號每月可以推送四次。
- 服務號推送的消息會出現在用戶的聊天列表中,而訂閱號推送的消息顯示在訂閱號文件夾中。
- 還有一些其他接口功能的區別和限制,總的來說服務號支持更高級的功能開發。
訂閱號更加偏向於向用戶傳遞咨詢,一般各種技術類公眾號都屬於訂閱號,訂閱號的消息推送並不會有太顯眼的提醒,如果你想讓某個公眾號的推送內容更加顯眼,可以選擇置為星標。置為星標后公眾號會顯示在所有訂閱號的最頂部,同時收到消息后會有黃色五角星星標提醒。
一 公眾號配置服務器
微信官方提供了非常完善的接入文檔,如果想了解文檔的具體內容,直接瀏覽器搜索微信開發文檔就可以了。但是為了方便開發,一般不會直接去根據微信開發文檔進行開發,gitee上有許多開源項目對微信開發文檔進行了封裝,這里我使用mica-weixin
開發包進行演示,mica-weixin
是jfinal-weixin
的boot版本。
配置服務器信息很簡單,具體流程就是微信服務發送請求一個請求給業務服務器,業務服務器驗證請求后給微信服務一個響應。
1.1 搭建業務服務
本地搭建一個spring-boot-weixin
的項目,使用內網穿透工具進行穿透,使其可以與外網進行通信。
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自定義配置支持多公眾號。
dream:
weixin:
wx-configs:
- appId: xxxxxx
appSecret: xxxxxx
token: javatrip
encodingAesKey: xxxxxx
appId
和appSecret
可在公眾號后台進行查看,具體位置在菜單開發—>基本配置中,其中appSecret
要妥善保管,現在公眾號已經不支持查看appSecret
了,如果你忘了appSecret
,只能進行重置。
1.1.3 開發消息校驗接口
mica-weixin
已經為我們提供好了消息校驗接口,只需要繼承DreamMsgControllerAdapter
就可以了。
@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
提供了多公眾號配置的功能,使用ThreadLocal
和appid
進行綁定。只需要簡單配置即可實現多公眾號配置。
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-weixin
將access_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(五)整合swagger生成接口文檔
spring-boot-route(六)整合JApiDocs生成接口文檔
spring-boot-route(七)整合jdbcTemplate操作數據庫
spring-boot-route(八)整合mybatis操作數據庫
spring-boot-route(九)整合JPA操作數據庫
spring-boot-route(十一)數據庫配置信息加密
spring-boot-route(十二)整合redis做為緩存
spring-boot-route(十三)整合RabbitMQ
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(二十四)分布式session的一致性處理
spring-boot-route(二十五)兩行代碼實現國際化
spring-boot-route(二十六)整合webSocket
這個系列的文章都是工作中頻繁用到的知識,學完這個系列,應付日常開發綽綽有余。如果還想了解其他內容,掃面下方二維碼告訴我,我會進一步完善這個系列的文章!