1.在微信开发文档找到对应的方法
微信开发文档
可以看到请求地址中需要access_token,所以第一步先去获取token
2.获取微信小程序的access_token
java实现代码
@Service
public class WeiXinServiceImpl implements WeiXinService {
private final String MINI_ACCESS_TOKEN = "mall:common:mini_access_token";
private final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
private RestTemplate restTemplate;
@Value("${wx.miniapp.appid}")//微信小程序的APPID
private String miniAppId;
@Value("${wx.miniapp.appsecret}")
private String miniAppSsecret;
@Override
public String getMiniAppAccessToken() {
ResponseEntity<JSONObject> responseEntity = restTemplate.getForEntity(String.format(ACCESS_TOKEN_URL, miniAppId, miniAppSsecret), JSONObject.class);
String accessToken = responseEntity.getBody().getString("access_token");
return accessToken;
}
}
3.调用方法获取访问趋势数据
private final String GETDAILYRETAIN = "https://api.weixin.qq.com/datacube/getweanalysisappiddailyretaininfo?access_token=%s";
private final String GETDAILYVISITTREND = "https://api.weixin.qq.com/datacube/getweanalysisappiddailyvisittrend?access_token=%s";
@Override
public JSONObject getDailyRetain(String beginDate, String endDate) {
//获取微信小程序Access_token
String accessToken = getMiniAppAccessToken();
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
JSONObject param = new JSONObject();
param.put("begin_date", beginDate);
param.put("end_date", endDate);
HttpEntity formEntity = new HttpEntity(param, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(String.format(GETDAILYRETAIN, accessToken), formEntity, String.class);
JSONObject jsonObject = JSONObject.parseObject(responseEntity.getBody());
return jsonObject;
}
@Override
public JSONObject getDailyVisitTrend(String beginDate, String endDate) {
String accessToken = getMiniAppAccessToken();
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
JSONObject param = new JSONObject();
param.put("begin_date", beginDate);
param.put("end_date", endDate);
HttpEntity formEntity = new HttpEntity(param, headers);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(String.format(GETDAILYVISITTREND, accessToken), formEntity, String.class);
JSONObject jsonObject = JSONObject.parseObject(responseEntity.getBody());
return jsonObject;
}
4.获取微信小程序访问数据
根据开发文档中返回值的属性值,获取对应的数据
java实现代码
public WxxcxAnalysisDailyRetain getDailyRetain(String date) {
WxxcxAnalysisDailyRetain retain = new WxxcxAnalysisDailyRetain();
//
JSONObject dailyRetain = weiXinService.getDailyRetain(date, date);
Object visit_uv = dailyRetain.getJSONArray("visit_uv").getJSONObject(0).get("value");
retain.setVisitUv((Integer) visit_uv);
//
JSONObject dailyVisitTrend = weiXinService.getDailyVisitTrend(date, date);
Object session_cnt = dailyVisitTrend.getJSONArray("list").getJSONObject(0).get("session_cnt");
Object visit_pv = dailyVisitTrend.getJSONArray("list").getJSONObject(0).get("visit_pv");
Object visit_uv_new = dailyVisitTrend.getJSONArray("list").getJSONObject(0).get("visit_uv_new");
Object stay_time_uv = dailyVisitTrend.getJSONArray("list").getJSONObject(0).get("stay_time_uv");
Object stay_time_session = dailyVisitTrend.getJSONArray("list").getJSONObject(0).get("stay_time_session");
Object visit_depth = dailyVisitTrend.getJSONArray("list").getJSONObject(0).get("visit_depth");
//
retain.setSessionCnt((Integer) session_cnt);
retain.setVisitPv((Integer) visit_pv);
retain.setVisitUvNew((Integer) visit_uv_new);
retain.setStayTimeUv((BigDecimal) stay_time_uv);
retain.setStayTimeSession((BigDecimal) stay_time_session);
retain.setVisitDepth((BigDecimal) visit_depth);
retain.setCreateTime(new Date());
return retain;
}
版权声明:本文为qq_39339701原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。