| @@ -42,17 +42,13 @@ public class WxAppVisitSchedule { | |||
| @Autowired | |||
| private WxAppinfoService WxAppinfoService; | |||
| // @Scheduled(cron = "0 */1 * * * *?") | |||
| @Scheduled(cron = "0 0 9 * * ? ") | |||
| //@Scheduled(cron = "*/10 * * * * ?") // 测试10秒中一次 | |||
| @Scheduled(cron = "0 0 2,3,4 * * ? ") //2点,3点,4点各做一次 | |||
| public void start() { | |||
| try { | |||
| Calendar c =Calendar.getInstance(); | |||
| c.add(Calendar.DAY_OF_YEAR, -1); | |||
| Date time = c.getTime(); | |||
| String yesterday = new SimpleDateFormat("yyyyMMdd").format(time); | |||
| // "wx8eb8275b78db4ede", "76c43df01296998d8ce12383f213ac10"; | |||
| WxAppinfo appInfo = new WxAppinfo(); | |||
| appInfo.setType(EnumAppType.C.getCode()); | |||
| PageInfo<WxAppinfo> page = WxAppinfoService.listAsPage(appInfo, 1, 10000); | |||
| @@ -60,25 +56,38 @@ public class WxAppVisitSchedule { | |||
| if(StringUtils.isBlank(w.getAppId())||StringUtils.isBlank(w.getSecret())) { | |||
| continue; | |||
| } | |||
| getData(yesterday,w.getAppId(),w.getSecret(),w.getTenantId()); | |||
| Calendar c = Calendar.getInstance(); | |||
| String date; | |||
| for(int i = 0; i < 7; i++) { //查找最近7天有没有漏掉的数据 | |||
| c.add(Calendar.DAY_OF_YEAR, -1); | |||
| date = new SimpleDateFormat("yyyyMMdd").format(c.getTime()); | |||
| if (!checkData(date ,w.getAppId(), w.getTenantId())) | |||
| getData(date,w.getAppId(),w.getSecret(),w.getTenantId()); | |||
| } | |||
| } | |||
| // TODO hardcode appId | |||
| // String appId = "wx8eb8275b78db4ede"; | |||
| // String key ="76c43df01296998d8ce12383f213ac10"; | |||
| // String talentId ="456"; | |||
| // getData(yesterday,appId,key,talentId); | |||
| }catch(Exception e) { | |||
| logger.error("获取微信访问数据失败",e); | |||
| } | |||
| } | |||
| private boolean checkData(String date,String appId, String tenantId) { | |||
| WxUserVisit wxUserVisit = new WxUserVisit(); | |||
| wxUserVisit.setAppId(appId); | |||
| wxUserVisit.setTenantId(tenantId); | |||
| wxUserVisit.setRefDate(date); | |||
| PageInfo<WxUserVisit> page = wxUserVisitService.listAsPage(wxUserVisit, 1, 10000); | |||
| if (page.getList().size() > 0) | |||
| return true; | |||
| return false; | |||
| } | |||
| private void getData(String yesterday,String appId,String key,String talentId) throws Exception { | |||
| private void getData(String date,String appId,String key,String talentId) throws Exception { | |||
| String accessToken = getAccessToken(appId, key); | |||
| HttpHeaders headers = new HttpHeaders(); | |||
| headers.setContentType(MediaType.APPLICATION_JSON); | |||
| Map<String, String> map = new HashMap<String, String>(); | |||
| map.put("begin_date", yesterday); | |||
| map.put("end_date",yesterday); | |||
| map.put("begin_date", date); | |||
| map.put("end_date",date); | |||
| RestTemplate restTemplate = new RestTemplate(); | |||
| HttpEntity<Map<String,String>> entity = new HttpEntity<Map<String,String>>(map, headers); | |||
| String reqUrl =visit+accessToken; | |||
| @@ -89,21 +98,18 @@ public class WxAppVisitSchedule { | |||
| Map<String,Object> maps = (Map<String,Object>)JSON.parse(body); | |||
| JSONArray jSONArray = (JSONArray)maps.get("list"); | |||
| if(jSONArray==null || jSONArray.isEmpty()) { | |||
| logger.info("获取失败"); | |||
| logger.error("获取数据失败:"+body); | |||
| return; | |||
| } | |||
| JSONObject jsonObject = jSONArray.getJSONObject(0); | |||
| Map<String, Object> itemMap = JSONObject.toJavaObject(jsonObject, Map.class); | |||
| //{"visit_uv":6,"stay_time_uv":1178.3333,"stay_time_session":115.9016,"ref_date":"20180828", | |||
| // "visit_depth":3.1311,"session_cnt":61,"visit_pv":645,"visit_uv_new":3} | |||
| logger.info(JSON.toJSONString(itemMap)); | |||
| WxUserVisit v = new WxUserVisit(); | |||
| // TODO hardcode appId | |||
| // v.setAppId("wx8eb8275b78db4ede"); | |||
| v.setAppId(appId); | |||
| String time = itemMap.get("ref_date")+""; | |||
| Date date = new SimpleDateFormat("yyyyMMdd").parse(time); | |||
| v.setDayDate(date); | |||
| Date dayDate = new SimpleDateFormat("yyyyMMdd").parse(time); | |||
| v.setDayDate(dayDate); | |||
| v.setRefDate(time); | |||
| v.setSessionCnt(Integer.valueOf(itemMap.get("session_cnt")+"")); | |||
| v.setVisitPv(Integer.valueOf(itemMap.get("visit_pv")+"")); | |||
| @@ -119,7 +125,6 @@ public class WxAppVisitSchedule { | |||
| } | |||
| private String getAccessToken(String appId,String appSecret) { | |||
| // return "13_rBo3ajS3jjd8OXZ2MLd4HfLrmt78gvaCeRtu-Xme0iC0fhs_lNS47aLPEwI8kfZQIMKnWYshY5wpaf2IoSI7tgVBm7WwVrm_Bg96J31VPKi8pEp8yB6JiTpDcWkpwv5GngiH2vDkwz7VHOsPLBYcAGAZPM"; | |||
| String token="https://api.weixin.qq.com/cgi-bin/token?"+ | |||
| "grant_type=client_credential&appid=APPID&secret=APPSECRET"; | |||
| String url = token.replace("APPID", appId). | |||