zhanghua
2023-12-26 04186253c1f67753e5ef2200f2ef42bfaaab82ea
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package com.ycl.timer;
 
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.xxpt.gateway.shared.client.http.ExecutableClient;
import com.alibaba.xxpt.gateway.shared.client.http.GetClient;
import com.alibaba.xxpt.gateway.shared.client.http.PostClient;
import com.ycl.config.DingConfig;
import com.ycl.util.DingUtil;
import com.ycl.util.VideoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
 
import static com.ycl.common.constant.DingConst.GET_TIKER;
import static com.ycl.common.constant.DingConst.GET_TOKEN;
 
/**
 * 获取钉钉 token
 */
@Slf4j
@Component
public class GetDingToken implements  ApplicationListener<ContextRefreshedEvent> {
    private static GetClient getTokenClient;
    @Autowired
    private StringRedisTemplate redisTemplate;
 
    @Resource
    private ExecutableClient executableClient;
 
    @Resource
    private DingConfig dingConfig;
    @Autowired
    private DingUtil dingUtil;
 
    private String getToken() {
        //调用API
        getTokenClient = executableClient.newGetClient(GET_TOKEN);
        //设置参数
        getTokenClient.addParameter("appkey", dingConfig.getAppKey());
        getTokenClient.addParameter("appsecret", dingConfig.getAppSecret());
        String apiResult = getTokenClient.get();
        return parsingResult(apiResult);
    }
 
    private String getTiker(String accToken) {
        //调用API
        PostClient postClient = executableClient.newPostClient(GET_TIKER);
        //设置参数
        if (ObjectUtil.isNotNull(accToken)) {
            postClient.addParameter("accessToken", accToken);
            postClient.addParameter("appkey", dingConfig.getAppKey());
            postClient.addParameter("appsecret", dingConfig.getAppSecret());
            String apiResult = postClient.post();
            return parsingResult(apiResult);
        }
        return null;
    }
 
    /**
     * 解析返回token
     *
     * @param apiResult
     * @return
     */
    private static String parsingResult(String apiResult) {
        if (ObjectUtil.isNotNull(apiResult)) {
            JSONObject resJson = JSONObject.parseObject(apiResult);
            if (resJson.getBoolean("success")) {
                JSONObject content = resJson.getJSONObject("content");
                if (content.getBoolean("success")) {
                    JSONObject dataObj = content.getJSONObject("data");
                    String accessToken = dataObj.getString("accessToken");
                    return accessToken;
                }
            } else {
                log.error(resJson.toJSONString());
            }
        }
        return null;
    }
 
 
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        if (contextRefreshedEvent.getApplicationContext().getParent() == null) {
//            runAction();
        }
    }
 
    @Value("${spring.profiles.active}")
    private String env;
 
        @Scheduled(cron ="0 0/2 * * * ?")
//    @Scheduled(cron = "0/1 * * * * ?")   // 每秒执行
    public void runAction() {
//        System.out.println("环境:" + env);
        if ("online".equals(env)) {
            //gettoken
            String getToken = dingUtil.getToken();
            //jsApi
            String jsapiToken = dingUtil.getTiker(getToken);
            //存储到redis
            JSONObject dingObj = new JSONObject();
            dingObj.put("token", getToken);
            dingObj.put("jsApiTiker", jsapiToken);
            redisTemplate.opsForValue().set("ding", dingObj.toJSONString(), 2L, TimeUnit.HOURS);
        }
    }
}