# 本節知識點
- 1,springboot創建小程序推送后臺
- 2,微信小程序開發的學習
- 3,獲取推送需要的formid
- 4,實現微信小程序推送功能
# 課程中用到的網址和文件
- 1,小程序學習視頻:[https://edu.csdn.net/course/detail/9531](https://edu.csdn.net/course/detail/9531)
- 2,小程序云開發獲取用戶openid:
[https://edu.csdn.net/course/play/9604/204529](https://edu.csdn.net/course/play/9604/204529)
- 3,微信小程序官方推送文檔:
[https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/template-message.html](https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/template-message.html)
# 配套筆記
- 5行代碼實現微信小程序模版消息推送 (含推送后臺和小程序源碼)
[https://www.jianshu.com/p/35da86f309d4](https://www.jianshu.com/p/35da86f309d4)
# 視頻地址
在線視頻:[https://edu.csdn.net/course/detail/23750](https://edu.csdn.net/course/detail/23750)
網盤視頻:加老師微信索要視頻資源。
最好跟著老師的教程敲代碼,如果實在敲不出來,再加老師微信索要源碼。
# 推送驗證
http://localhost:8080/push?openid=o3DoL0WEdzcJ20AVJg1crP96gbjM&formid=00869c94008b43379e52658997d887e9
注意:這里一定要替換成你自己的openid,和formid。否則沒法推送成功的
# 核心代碼
- 1 三方類庫
```
<!--微信小程序模版推送-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<version>3.4.0</version>
</dependency>
```
- 2 推送的核心類
```
/**
* Created by qcl on 2019-05-20
* 微信:2501902696
* desc: 微信小程序模版推送實現
*/
@RestController
public class PushController {
@GetMapping("/push")
public String push(@RequestParam String openid, @RequestParam String formid) {
//1,配置小程序信息
WxMaInMemoryConfig wxConfig = new WxMaInMemoryConfig();
wxConfig.setAppid("wx7c54942dfc87f4d8");//小程序appid
wxConfig.setSecret("5873a729c365b65ab42bb5fc82d2ed49");//小程序AppSecret
WxMaService wxMaService = new WxMaServiceImpl();
wxMaService.setWxMaConfig(wxConfig);
//2,設置模版信息(keyword1:類型,keyword2:內容)
List<WxMaTemplateData> templateDataList = new ArrayList<>(2);
WxMaTemplateData data1 = new WxMaTemplateData("keyword1", "獲取老師微信");
WxMaTemplateData data2 = new WxMaTemplateData("keyword2", "2501902696");
templateDataList.add(data1);
templateDataList.add(data2);
//3,設置推送消息
WxMaTemplateMessage templateMessage = WxMaTemplateMessage.builder()
.toUser(openid)//要推送的用戶openid
.formId(formid)//收集到的formid
.templateId("eDZCu__qIz64Xx19dAoKg0Taf5AAoDmhUHprF6CAd4A")//推送的模版id(在小程序后臺設置)
.data(templateDataList)//模版信息
.page("pages/index/index")//要跳轉到小程序那個頁面
.build();
//4,發起推送
try { wxMaService.getMsgService().sendTemplateMsg(templateMessage);
} catch (WxErrorException e) {
System.out.println("推送失敗:" + e.getMessage());
return e.getMessage();
}
return "推送成功";
}
}
```
- 3,小程序獲取formid的核心布局
```
<form class="form_item" bindsubmit='gorRunnerLobby' report-submit='true' data-type="1">
<button class="button" form-type='submit'>
獲取formid
</button>
</form>
```
- 4,小程序獲取formid的核心js代碼
```
//獲取用戶的formid,用于模版消息推送
gorRunnerLobby(event) {
console.log("formid: " + event.detail.formId);
this.setData({
formid: event.detail.formId
})
},
```
# 老師微信
2501902696(備注小程序)
# 學習群
學習群請加老師微信2501902696(備注java)老師拉你進學習群。