package com.ycl.util; 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 lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component; import javax.annotation.Resource; import static com.ycl.common.constant.DingConst.GET_TIKER; import static com.ycl.common.constant.DingConst.GET_TOKEN; @Slf4j @Component public class DingUtil { private static GetClient getTokenClient; @Resource private ExecutableClient executableClient; @Resource private DingConfig dingConfig; public 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); } public 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; } }