需求: 簡(jiǎn)單的向關(guān)注公眾號(hào)的用戶發(fā)送消息(SpringBoot框架下使用)
1.首先獲得公眾號(hào)配置
注: 如果是測(cè)試,可申請(qǐng)測(cè)試公眾號(hào) https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
申請(qǐng)之后會(huì)得到
配置信息

模板信息和用戶列表

2.發(fā)送公眾號(hào)信息
1.配置文件
wxsendmsg:
#微信公眾號(hào)appid
appid: wx71eab5e698feb424sss
#微信公眾號(hào)appidsecret
appidsecret: 7169487cd50d69c70a1bcsd92fafa5ssasv
#微信公眾號(hào)tokenurl
gettokenurl: https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=
#微信公眾號(hào)發(fā)送消息
sendrequesturl: https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=
#微信公眾號(hào)獲得關(guān)注用戶列表url
getusersurl: https://api.weixin.qq.com/cgi-bin/user/get?access_token=
#微信公眾號(hào)消息模板id
templateid: QKBbSrPNqeCb1NE14uE1ASuQQPSvoDijGJINIvvjY2Y
注意項(xiàng): 對(duì)于獲取token、獲取用戶、發(fā)送請(qǐng)求對(duì)應(yīng)三個(gè)不同的路徑,這個(gè)要區(qū)分開(kāi),不然容易高混亂
@Value(value = "${wxsendmsg.appid}")
private String appid;
@Value(value = "${wxsendmsg.appidsecret}")
private String appidsecret;
@Value(value = "${wxsendmsg.sendrequesturl}")
private String sendrequesturl;
@Value(value = "${wxsendmsg.templateid}")
private String templateid;
@Value(value = "${wxsendmsg.gettokenurl}")
private String gettokenurl;
@Value(value = "${wxsendmsg.getusersurl}")
private String getusersurl;
2.獲得access_token
public String getAccessToken() {
//將access_token存入redis
Object o = redisUtil.get(ACCESSTOKEN + appid);
if (o != null) {
log.info("access_token : {}", o.toString());
return o.toString();
}else {
String requestUrl = gettokenurl + appid + "&secret=" + appidsecret;
String res = HttpUtil.get(requestUrl);
JSONObject jsonObject = JSONObject.fromObject(res);
String accessToken = jsonObject.getString("access_token");
redisUtil.set(ACCESSTOKEN + appid, accessToken);
redisUtil.expire(ACCESSTOKEN + appid, JwtUtil.EXPIRE_TIME);
log.info("access_token : {}", accessToken);
return accessToken;
}
}
3.獲得關(guān)注公眾號(hào)的用戶信息
//這里我將用戶的信息都存入到列表中
public List<String> getOpenids(String accessToken) {
RestTemplate restTemplate = new RestTemplate();
String requestUrl = getusersurl + accessToken;
ResponseEntity<String> response = restTemplate.postForEntity(requestUrl, null, String.class);
log.info("結(jié)果是: {}", response.getBody());
com.alibaba.fastjson.JSONObject result = com.alibaba.fastjson.JSONObject.parseObject(response.getBody());
com.alibaba.fastjson.JSONArray openIdJsonArray = result.getJSONObject("data").getJSONArray("openid");
Iterator iterator = openIdJsonArray.iterator();
List<String> openids = new ArrayList<>();
while (iterator.hasNext()) {
openids.add(iterator.next().toString());
}
return openids;
}
4.配置消息類(lèi)(樣式+內(nèi)容)
@Data
public class WeChatTemplateMsg {
/**
* 消息
*/
private String value;
/**
* 消息顏色
*/
private String color;
public WeChatTemplateMsg(String value) {
this.value = value;
this.color = "#173177";
}
public WeChatTemplateMsg(String value, String color) {
this.value = value;
this.color = color;
}
}
注: 此類(lèi)必須要有g(shù)et和set方法,不然會(huì)報(bào)轉(zhuǎn)換錯(cuò)誤
5.發(fā)送消息
// 模板參數(shù)
Map<String, WeChatTemplateMsg> sendMag = new HashMap<String, WeChatTemplateMsg>();
sendMag.put("person", new WeChatTemplateMsg(wxMsg));
private void sendToOpenid(Map<String, WeChatTemplateMsg> sendMag, String openids, String templateid, String requestUrl) {
RestTemplate restTemplate = new RestTemplate();
//拼接base參數(shù)
Map<String, Object> sendBody = new HashMap<>();
sendBody.put("touser", openids); // openId 用戶id
sendBody.put("url", "https://www.baidu.com"); //跳轉(zhuǎn)網(wǎng)頁(yè)url
sendBody.put("data", sendMag); // 模板參數(shù)
sendBody.put("template_id", templateid); // 模板Id
ResponseEntity<String> response = restTemplate.postForEntity(requestUrl, sendBody, String.class);
log.info("結(jié)果是: {}", response.getBody());
com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(response.getBody());
String messageCode = jsonObject.getString("errcode");
String msgId = jsonObject.getString("msgid");
log.info("messageCode : {},msgId : {}", messageCode, msgId);
}
浙公網(wǎng)安備 33010602011771號(hào)