微信公眾號(hào)開(kāi)發(fā)-模板信息推送
我們是微信小程序用戶(hù)在系統(tǒng)中進(jìn)行用戶(hù)綁定摸柄,但是需要推送信息到微信公眾中
發(fā)送流程
- 1.獲取access_token
- 2.獲取所有的用戶(hù)openid
- 3.獲取所有的用戶(hù)的用戶(hù)信息
- 4.獲取用戶(hù)的unionid
- 5.綁定用戶(hù)微信公眾號(hào)openid,unionid,微信小程序openid
- 6.通過(guò)用戶(hù)微信小程序的openid找到用戶(hù)微信公眾號(hào)的openid
- 7.拿著微信公眾號(hào)openid進(jìn)行模板推送
以下只演示部分代碼
/**
* 獲取公眾號(hào)token
* @param appId
* @param secret
* @return
*/
public String getAccessToken(String appId,String secret){
String url = "https://api.weixin.qq.com/cgi-bin/token";
Map<String, String> dataMap = new HashMap<>();
dataMap.put("grant_type", "client_credential");
dataMap.put("appid", appId);
dataMap.put("secret", secret);
WxGetTokenVo wxGetTokenVo = JSONObject.parseObject(HttpClientUtils.get(url, dataMap), WxGetTokenVo.class);
return wxGetTokenVo.getAccess_token();
}
/**
* 獲取全部的openID
* @param acessToken
* @param nextOpenId
* @param wxGetOpenIdVos
* @return
*/
public void getAllOpenId(String acessToken,String nextOpenId,List<WxGetOpenIdVo> wxGetOpenIdVos){
String url = "https://api.weixin.qq.com/cgi-bin/user/get";
Map<String, String> dataMap = new HashMap<>(2);
dataMap.put("access_token", acessToken);
if(StringUtils.isNotBlank(nextOpenId)){
dataMap.put("next_openid", nextOpenId);
}
String openidUsersStr = HttpClientUtils.get(url,dataMap);
WxGetOpenIdVo wxGetOpenIdVo = JSONObject.parseObject(openidUsersStr, WxGetOpenIdVo.class);
if(wxGetOpenIdVo != null){
if(wxGetOpenIdVo.getData() != null) {
wxGetOpenIdVos.add(wxGetOpenIdVo);
}
if(StringUtils.isNotBlank(wxGetOpenIdVo.getNext_openid())){
getAllOpenId(acessToken, wxGetOpenIdVo.getNext_openid(), wxGetOpenIdVos);
}
}
}
- 獲取用戶(hù)的用戶(hù)數(shù)據(jù)
/**
* 獲取用戶(hù)詳情
* @param acessToken
* @param openId
* @param wxGetUserInfoVos
*/
public void getUserInfo(String acessToken,String openId,List<WxGetUserInfoVo> wxGetUserInfoVos){
String url = "https://api.weixin.qq.com/cgi-bin/user/info";
Map<String, String> dataMap = new HashMap<>();
dataMap.put("access_token", acessToken);
dataMap.put("openid", openId);
dataMap.put("lang", "zh_CN");
WxGetUserInfoVo wxGetUserInfoVo = JSONObject.parseObject(HttpClientUtils.get(url, dataMap), WxGetUserInfoVo.class);
wxGetUserInfoVos.add(wxGetUserInfoVo);
}
public static void send(String token){
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;
//TemplateMessage 需要封裝 測(cè)試代碼沒(méi)有進(jìn)行封裝
TemplateMessage templateMessage=new TemplateMessage();
AppletVo appletVo = new AppletVo();//跳轉(zhuǎn)小程序使用
appletVo.setAppid("****");
appletVo.setPagepath("/pages/index/index");
templateMessage.setMiniprogram(appletVo);
// templateMessage.setUrl("www.baidu.com");
templateMessage.setTouser("********");
templateMessage.setTemplate_id("3IrFxsPYYlutAYqru1ZPtGHCMceMfojGURmVkaWLOZg");
//設(shè)置模板標(biāo)題
Content first=new Content();
first.setValue("感謝您進(jìn)行實(shí)名認(rèn)證嗦玖!");
first.setColor("");
//設(shè)置模板內(nèi)容
Content keyword1=new Content();
keyword1.setValue("認(rèn)證失敗");
keyword1.setColor("#FF0000");
//設(shè)置模板位置
Content keyword2=new Content();
keyword2.setValue("2019-08-26");
keyword2.setColor("");
//設(shè)置設(shè)備
//設(shè)置跳轉(zhuǎn)內(nèi)容
Content remark=new Content();
remark.setValue("請(qǐng)?zhí)峤徽鎸?shí)的信息器瘪,重新提交,謝謝!");
remark.setColor("#FF0000");
//創(chuàng)建模板信息數(shù)據(jù)對(duì)象
Data data=new Data();
data.setFirst(first);
data.setKeyword1(keyword1);
data.setKeyword2(keyword2);
data.setRemark(remark);
templateMessage.setData(data);
//將封裝的數(shù)據(jù)轉(zhuǎn)成JSON
String jsonString = JSON.toJSONString(templateMessage);
System.out.println("templateMessage = "+templateMessage);
JSONObject post = doPost(url, JSONObject.parseObject(jsonString));
System.out.println(post);
}
public static void main(String[] args) {
WeChatUtil weChatUtil = new WeChatUtil();
String accessToken = weChatUtil.getAccessToken(WXAPPID, KEY);
//send(accessToken);
List<WxGetOpenIdVo> wxGetOpenIdVos = new ArrayList<>();
weChatUtil.getAllOpenId(accessToken, null, wxGetOpenIdVos);
List<WxGetUserInfoVo> dataUserInfo = new ArrayList<>();
wxGetOpenIdVos.forEach(wxGetOpenIdVo ->{
wxGetOpenIdVo.getData().getOpenid().forEach(openid ->
weChatUtil.getUserInfo(accessToken, openid, dataUserInfo)
);
}
);
dataUserInfo.forEach(dataUser->
System.out.println(dataUser.getOpenid() + "---" +dataUser.getUnionid())
);
}
如果需要獲取到微信用的unionid需要在微信開(kāi)放平臺(tái)綁定公眾號(hào)墨辛,否則不返回該字段寥闪。部分封裝代碼沒(méi)有貼在代碼中凿渊,自行封裝彩掐。
示例代碼 沒(méi)有整理
[https://www.lanzous.com/i5t9bcb](https://www.lanzous.com/i5t9bcb)