內容有點多,請耐心!
最近公司的有這個業務需求,又很湊巧讓我來完成:
首先想要對接,先要一個公眾號,再就是開發文檔了:https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html
不過請注意這一點
ok,我們繼續:再來完成公眾號的基本配置:
服務器地址(URL):必須以http://或https://開頭,分別支持80端口和443端口。這個URL是很重要的,需要響應微信發送的token驗證
令牌(Token):必須為英文或數字,長度為3-32字符。上面說過做驗證的
消息加解密密鑰:可以直接隨機生成
消息加解密方式:明文、兼容、安全 看業務需求選擇:我覺得明文省事點(個人見解)
詳解微信開發文檔:https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html
如果一直用微信的接口調用,會有點麻煩所以這邊我就引用了 ---》WxJava《---
github:https://github.com/Wechat-Group/WxJava
gitee:https://gitee.com/binary/weixin-java-tools
先來看看官方文檔對於推送模板消息參數說明:
剛開始看這個開發文檔的時候我還是有些懵的
openId怎么拿? 不知道!那好吧,度娘一下、google一下,疑惑不就解決了!
ok,下一步template_id:模板Id;對於這個可以自己申請 or 選用已有的
我就省去沒必要的麻煩方正是個Demo,就選擇已有的:
選擇一個進去添加模板就行了:
ok,模板id也拿到了,現在就開始
請大家也詳細的看看 WxJava 的文檔
---先建立 SpringBoot 項目---
導入wxjava公眾號 對應的pom
<!-- WxJava公眾號 --> <dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-mp</artifactId> <version>3.6.0</version>
</dependency>
然后就需要配置公眾號相關的信息了,我個人比較喜歡在 yml 里面配置
# 微信公眾號配置
wx:
appid: 11111
secret: 22222
token: 33333
aeskey: 44444
配置了這個就需要對應這個配置的Component了(實體類)
/** * @ClassName WXProperties * @Author chenghao * @Date 2020/1/10 15:44 **/ @Data @Component @ConfigurationProperties(prefix = "wx") public class WxMpProperties { /** * 公眾號appId */
private String appId; /** * 公眾號appSecret */
private String secret; /** * 公眾號token */
private String token; /** * 公眾號aesKey */
private String aesKey; }
關於注解啥的我就不去詳解了 不明白的自己去看看官方文檔
SpringBoot:
Lombok:
https://projectlombok.org/features/all
-----------------------------好的,各位,我們現在開始-----------------------------
我先給大家一步一步分析
剛剛我們選擇的模板,這些key都一個一個參數,文檔上面說的很明白,賦值替換!!! 明白了這點就ok了。
好,再來回頭看 WxJava
恩,根據上面的示例代碼,我寫了個Demo
/**
* 微信消息推送
*
* @ClassName WxMsgPush
* @Author chenghao
* @Date 2020/1/10 16:20
**/
@Slf4j
@Component
public class WxMsgPush {
/**
* 微信公眾號API的Service
*/
private final WxMpService wxMpService;
/**
* 構造注入
*/
WxMsgPush(WxMpService wxMpService) {
this.wxMpService = wxMpService;
}
/**
* 發送微信模板信息
*
* @param openId 接受者openId
* @return 是否推送成功
*/
public Boolean SendWxMsg(String openId) {
// 發送模板消息接口
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
// 接收者openid
.toUser(openId)
// 模板id
.templateId("xxxxxxxxxxxxxxxxxxxxxxxxxxx")
// 模板跳轉鏈接
.url("http://www.baidu.com")
.build();
// 添加模板數據
templateMessage.addData(new WxMpTemplateData("first", "您好", "#FF00FF"))
.addData(new WxMpTemplateData("keyword1", "這是個測試", "#A9A9A9"))
.addData(new WxMpTemplateData("keyword2", "這又是個測試", "#FF00FF"))
.addData(new WxMpTemplateData("remark", "這還是個測試", "#000000"));
String msgId = null;
try {
// 發送模板消息
msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
} catch (WxErrorException e) {
e.printStackTrace();
}
log.warn("·==++--·推送微信模板信息:{}·--++==·", msgId != null ? "成功" : "失敗");
return msgId != null;
}
}
wdnmd!一上來就發現報錯!!!
Could not autowire. No beans of 'WxMpService' type found. -------> 無法自動接線。找不到“ WxMpService”類型的bean
一臉懵逼,然后看了下這個WxMpService接口 我靠,這些實現類,淦!
后來仔細研究了下,因為有多個實現類,我們需要自己寫個config,把這個實現類@Bean注入
/** * @ClassName WxConfig * @Author chenghao * @Date 2020/1/11 09:23 **/ @Configuration public class WxConfig { /** * 聲明實例 * * @return
*/ @Bean public WxMpService wxMpService() { WxMpService wxMpService = new WxMpServiceImpl(); return wxMpService; }
ok,開始分析代碼
首先這個
點進去瞅一眼有四個屬性:
/** * 接收者openid. */
private String toUser; /** * 模板ID. */
private String templateId; /** * 模板跳轉鏈接. * <pre> * url和miniprogram都是非必填字段,若都不傳則模板無跳轉;若都傳,會優先跳轉至小程序。 * 開發者可根據實際需要選擇其中一種跳轉方式即可。當用戶的微信客戶端版本不支持跳小程序時,將會跳轉至url。 * </pre> */
private String url; /** * 跳小程序所需數據,不需跳小程序可不用傳該數據. * * @see #url */
private MiniProgram miniProgram;
ok,我們繼續分析
留意一下這個
需要跟你自己申請的模板那些key對應!!!
這個就是賦值的內容了
文字對應的顏色
最后一步:推送
懵逼中·········
點進去,又來個接口
看到相應的方法了
好了,知道了對應方法的作用,終於可以推送了。但是但是,到現在,我才想起一件事情,我配置的公眾號信息,他能自己讀?很顯然我們少配置了信息。
真的是讓我一頓好找啊
他用是Lambda表達式+Stream 這里我就不用了,我不想太騷(主要是不會)
改了一下自己寫的config,大家要注意這個!!!
/**
* @ClassName WxConfig
* @Author chenghao
* @Date 2020/1/11 09:23
**/
@Configuration
public class WxConfig {
private final WxMpProperties wxMpProperties;
/**
* 構造注入
*
* @param wxMpProperties
*/
WxConfig(WxMpProperties wxMpProperties) {
this.wxMpProperties = wxMpProperties;
}
/**
* 微信客戶端配置存儲
*
* @return
*/
@Bean
public WxMpConfigStorage wxMpConfigStorage() {
WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
// 公眾號appId
configStorage.setAppId(wxMpProperties.getAppId());
// 公眾號appSecret
configStorage.setSecret(wxMpProperties.getSecret());
// 公眾號Token
configStorage.setToken(wxMpProperties.getToken());
// 公眾號EncodingAESKey
configStorage.setAesKey(wxMpProperties.getAesKey());
return configStorage;
}
/**
* 聲明實例
*
* @return
*/
@Bean
public WxMpService wxMpService() {
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService;
}
ok,主要 code 部分都完成了,開始測試吧。請自己建一個Controller
/**
* 微信消息推送
*/
private final WxMsgPush wxMsgPush;
/**
* 構造注入
*/
protected PushMsgApi(WxMsgPush wxMsgPush) {
this.wxMsgPush = wxMsgPush;
}
/** * 發送微信模板消息 */ @ApiOperation("發送微信模板消息") @ApiImplicitParams({ @ApiImplicitParam(name = "openId", value = "接受者openId", dataType = "String", paramType = "query") }) @PostMapping("/sendWxInfo") public void sendWxInfo(String openId) { // 執行發送
Boolean aBoolean = wxMsgPush.SendWxMsg(openId); System.out.println(aBoolean); }
ok!推送完成!!請大家自行去編寫!!!
(下篇出個微信登錄詳解)