package com.gk.hotwork.Scheduls.updateToken; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.gk.hotwork.Domain.UserInfo; import com.gk.hotwork.Domain.Utils.Base64Encrypt; import com.gk.hotwork.Domain.Utils.HttpUtils; import com.gk.hotwork.Domain.VideoInfo; import com.gk.hotwork.Service.UserService; import com.gk.hotwork.Service.VideoService; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import java.io.UnsupportedEncodingException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.List; //@Configuration //@EnableScheduling //@ConditionalOnProperty(prefix = "scheduling",name = "enabled",havingValue = "true") public class updateTokenTask { private Logger logger = LogManager.getLogger(updateTokenTask.class); @Value("${nanowebUrl}") private String nanoweb; @Value("${nanowebEnable}") private Boolean nanowebEnable; @Autowired private VideoService videoService; // @Scheduled(cron = "0 0 0/1 * * ?") //每小时执行一次 // @Scheduled(cron = "0/10 * * * * ?") //每十秒执行一次 public void startRefreshToken() throws Exception{ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date now = new Date(); Calendar calendar = Calendar.getInstance(); calendar.setTime(now); calendar.add(Calendar.DATE, 1); Date endtime = calendar.getTime(); String endtimeStr = sdf.format(endtime); List videoInfoList = videoService.selectByTime(endtimeStr); if (videoInfoList.size() > 0){ for (VideoInfo videoInfo : videoInfoList) { String url="https://open.ys7.com/api/lapp/token/get?appKey="+videoInfo.getAppkey()+"&appSecret="+videoInfo.getSecret(); CloseableHttpClient client = HttpClients.createDefault(); HttpPost post = new HttpPost(url); post.setHeader("Content-Type", "application/x-www-form-urlencoded"); CloseableHttpResponse response = client.execute(post); String resData = EntityUtils.toString(response.getEntity(),"UTF-8"); client.close(); System.out.println(resData); JSONObject result = JSONObject.parseObject(resData); Integer code = result.getInteger("code"); if(code!=null && code==200){ JSONObject data = JSONObject.parseObject(result.getString("data")); String accessToken = data.getString("accessToken"); Long expireTime = data.getLong("expireTime"); videoInfo.setExpiretime(new Date(expireTime)); videoInfo.setToken(accessToken); videoInfo.setUpdatetime(new Date()); videoService.updateById(videoInfo); } } } } }