|
@@ -0,0 +1,68 @@
|
|
|
+package com.css.simulation.resource.common.controller;
|
|
|
+
|
|
|
+import api.common.pojo.common.ResponseBodyVO;
|
|
|
+import api.common.pojo.param.RedisParameter;
|
|
|
+import api.common.util.HttpUtil;
|
|
|
+import api.common.util.JsonUtil;
|
|
|
+import api.common.util.ObjectUtil;
|
|
|
+import com.css.simulation.resource.common.configuration.algPlatform.ClientDetail;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+
|
|
|
+@Controller
|
|
|
+@RequestMapping("/algPlatform")
|
|
|
+public class AlgPlatformCtrl {
|
|
|
+
|
|
|
+ private static final String TOKEN_KEY = "ALGPLATFORM:TOKEN";
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ RedisController redisController;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ ClientDetail clientDetail;
|
|
|
+
|
|
|
+ @RequestMapping("/getAlgorithmList")
|
|
|
+ @ResponseBody
|
|
|
+ public ResponseBodyVO getAlgorithmList(@RequestBody String param) throws Exception {
|
|
|
+ //取得token
|
|
|
+ String token = getToken();
|
|
|
+ String algorithmListUri = clientDetail.getAlgorithmListUri() + "?access_token=" + token + param;
|
|
|
+ String result = HttpUtil.get(HttpUtil.getHttpClient(), HttpUtil.getRequestConfig(), algorithmListUri);
|
|
|
+ ResponseBodyVO responseBodyVO= new ResponseBodyVO(ResponseBodyVO.Response.SUCCESS, "请求成功!", result);
|
|
|
+ return responseBodyVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getToken() throws Exception {
|
|
|
+ RedisParameter redisParameter = new RedisParameter();
|
|
|
+ redisParameter.setKey(TOKEN_KEY);
|
|
|
+ ResponseBodyVO<String> responseBodyVO = redisController.get(redisParameter);
|
|
|
+ String token = responseBodyVO.getInfo();
|
|
|
+ if(ObjectUtil.isNull(token)){//token失效重新获取
|
|
|
+ String tokenUri = clientDetail.getTokenUri();
|
|
|
+ String appid = clientDetail.getAppid();
|
|
|
+ String secret = clientDetail.getSecret();
|
|
|
+ String URI = tokenUri + "?grant_type=client_credentials&appid=" + appid + "&secret=" + secret;
|
|
|
+ String result = HttpUtil.get(HttpUtil.getHttpClient(), HttpUtil.getRequestConfig(), URI);
|
|
|
+ JsonNode jsonNode = JsonUtil.readTree(result);
|
|
|
+ JsonNode dataNode = jsonNode.path("data");
|
|
|
+ if(ObjectUtil.isNotNull(dataNode)){
|
|
|
+ JsonNode tokenNode = dataNode.path("access_token");
|
|
|
+ if(ObjectUtil.isNotNull(tokenNode)){
|
|
|
+ token = tokenNode.toString();
|
|
|
+ int expiresSec = dataNode.path("expires_in_sec").asInt();
|
|
|
+ redisParameter.setMinutes(expiresSec / 60);
|
|
|
+ redisParameter.setValue(token);
|
|
|
+ redisController.set(redisParameter);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|