郑永安
2023-09-19 69185134fcfaf913ea45f1255677225a2cc311a4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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<VideoInfo> 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);
                }
            }
        }
    }
 
 
}