微信公众平台官网:https://mp.weixin.qq.com
数据统计模块儿,图文分析数据接口(以getarticletotal接口为例)
https://api.weixin.qq.com/datacube/getarticletotal?access_token=ACCESS_TOKEN
接口介绍:
该接口只有三个参数,access_token 是请求接口的凭证,是必须要的,begin_date和end_date,最大跨度时间为1天,也就是两个参数必须是同一天,end_date允许设置的最大值为昨日,但最多统计发表日后7天数据。
了解了规则,接下来撸代码:
实例化微信工具类(自写)
WeixinUtil wu = new WeixinUtil(gi.getOriginalId() + "", gi.getAppId(), gi.getSecret(), "", "");
然后调用接口
Map<String, Object> map = wu.getArticleTotal(day, day);
getArticleTotal接口内
// 图文素材接口
public Map<String, Object> getArticleTotal(String begin_date, String end_date) throws Exception {
String url = "https://api.weixin.qq.com/datacube/getarticletotal?access_token=" + getAccessToken();
String json = HttpRequestHelper.paramsPostJson(url,"{\"begin_date\": \"" + begin_date + "\",\"end_date\": \"" + end_date + "\"}");
validateAccessToken(json);
Map<String, Object> map1 = JacksonHelper.fromJSON(json, HashMap.class);
return map1;
}
接下来,是上一段代码中getAccessToken方法
public String getAccessToken() throws Exception {
//先看redis里是否有,为空就接口获取
if (RedisUtil.getStringValue(RANDOM_REDIS_ACCESS_TOKEN + appid) == null
|| "".equals(RedisUtil.getStringValue(RANDOM_REDIS_ACCESS_TOKEN + appid))) {
String sendurl = (new StringBuilder("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="))
.append(appid).append("&secret=").append(secret).toString();
HttpRequester req = new HttpRequester();
HttpResponser res = req.sendGet(sendurl);
String myjson = res.getContent();
//json转map
HashMap jo = JacksonHelper.fromJSON(myjson, HashMap.class);
String accessToken = (String) jo.get("access_token");
if (accessToken != null && !"".equals(accessToken)) {
RedisUtil.setExpireString(RANDOM_REDIS_ACCESS_TOKEN + appid, EXPIRES_IN, accessToken);//存redis
}
//如果accessToken为空,
if (accessToken == null || "".equals(accessToken)) {
return myjson;
}
return accessToken;
} else {
return RedisUtil.getStringValue(RANDOM_REDIS_ACCESS_TOKEN + appid);
//取AccessToken
}
}
把返回的数据拿出来
List<Map<String, Object>> list = (ArrayList<Map<String, Object>>) map.get("list");
遍历这个list,加入实体类
for (Map<String, Object> aSum : list) {}
GzhDayArticle gda = new GzhDayArticle(gi.getGzhId(), title,
aSum.get("msgid").toString(), article.get("stat_date").toString(), day,
Integer.parseInt(article.get("target_user") + ""),
Integer.parseInt(article.get("share_user") + ""),
Integer.parseInt(article.get("share_count") + ""),
Integer.parseInt(article.get("add_to_fav_user") + ""),
Integer.parseInt(article.get("add_to_fav_count") + ""),
Integer.parseInt(article.get("int_page_from_session_read_user") + ""),
Integer.parseInt(article.get("int_page_from_session_read_count") + ""),
Integer.parseInt(article.get("int_page_from_feed_read_user") + ""),
Integer.parseInt(article.get("int_page_from_feed_read_count") + ""),
Integer.parseInt(article.get("int_page_from_friends_read_user") + ""),
Integer.parseInt(article.get("int_page_from_friends_read_count") + ""),
departmentNo, userId, Integer.parseInt(article.get("int_page_read_user") + ""),
Integer.parseInt(article.get("int_page_read_count") + ""));
gzhService.saveGzhDayArticle(gda);
到此位置,获取微信公众号群发图文消息算是整个流程结束了,谨以做记录,方便日后复习。