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 { 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); } } }