| @@ -45,8 +45,8 @@ public class WxAppVisitSchedule { | |||
| //@Scheduled(cron = "*/10 * * * * ?") // 测试10秒中一次 | |||
| @Scheduled(cron = "0 0 2,3,4 * * ? ") //2点,3点,4点各做一次 | |||
| public void start() { | |||
| @Scheduled(cron = "0 0 2,3,4 * * ? ") //2,3,4点做一次 | |||
| public void daliyVisitRetrive() { | |||
| try { | |||
| WxAppinfo appInfo = new WxAppinfo(); | |||
| @@ -61,8 +61,8 @@ public class WxAppVisitSchedule { | |||
| 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()); | |||
| if (!checkData(date,w)) | |||
| getData(date,w); | |||
| } | |||
| } | |||
| }catch(Exception e) { | |||
| @@ -70,10 +70,10 @@ public class WxAppVisitSchedule { | |||
| } | |||
| } | |||
| private boolean checkData(String date,String appId, String tenantId) { | |||
| private boolean checkData(String date,WxAppinfo appInfo) { | |||
| WxUserVisit wxUserVisit = new WxUserVisit(); | |||
| wxUserVisit.setAppId(appId); | |||
| wxUserVisit.setTenantId(tenantId); | |||
| wxUserVisit.setAppId(appInfo.getAppId()); | |||
| wxUserVisit.setTenantId(appInfo.getTenantId()); | |||
| wxUserVisit.setRefDate(date); | |||
| PageInfo<WxUserVisit> page = wxUserVisitService.listAsPage(wxUserVisit, 1, 10000); | |||
| if (page.getList().size() > 0) | |||
| @@ -81,8 +81,8 @@ public class WxAppVisitSchedule { | |||
| return false; | |||
| } | |||
| private void getData(String date,String appId,String key,String talentId) throws Exception { | |||
| String accessToken = getAccessToken(appId, key); | |||
| private void getData(String date,WxAppinfo appInfo) throws Exception { | |||
| String accessToken = appInfo.getAccessToken(); | |||
| HttpHeaders headers = new HttpHeaders(); | |||
| headers.setContentType(MediaType.APPLICATION_JSON); | |||
| Map<String, String> map = new HashMap<String, String>(); | |||
| @@ -106,7 +106,7 @@ public class WxAppVisitSchedule { | |||
| logger.info(JSON.toJSONString(itemMap)); | |||
| WxUserVisit v = new WxUserVisit(); | |||
| // TODO hardcode appId | |||
| v.setAppId(appId); | |||
| v.setAppId(appInfo.getAppId()); | |||
| String time = itemMap.get("ref_date")+""; | |||
| Date dayDate = new SimpleDateFormat("yyyyMMdd").parse(time); | |||
| v.setDayDate(dayDate); | |||
| @@ -118,20 +118,8 @@ public class WxAppVisitSchedule { | |||
| v.setStayTimeSession(itemMap.get("stay_time_session")+""); | |||
| v.setVisitDepth(itemMap.get("visit_depth")+""); | |||
| v.setStayTimeUv(itemMap.get("stay_time_uv")+""); | |||
| v.setTenantId(talentId); | |||
| v.setTenantId(appInfo.getTenantId()); | |||
| wxUserVisitService.saveOrUpdate(v); | |||
| } | |||
| } | |||
| private String getAccessToken(String appId,String appSecret) { | |||
| String token="https://api.weixin.qq.com/cgi-bin/token?"+ | |||
| "grant_type=client_credential&appid=APPID&secret=APPSECRET"; | |||
| String url = token.replace("APPID", appId). | |||
| replace("APPSECRET", appSecret); | |||
| Map<String,Object> map = restTemplate.getForObject(url,Map.class); | |||
| logger.info("获取access_token返回:"+JSON.toJSONString(map)); | |||
| return (String)map.get("access_token"); | |||
| } | |||
| } | |||