Explorar o código

Merge remote-tracking branch 'origin/master'

zhaoyan %!s(int64=3) %!d(string=hai) anos
pai
achega
336e2702ac

+ 13 - 0
api-common/src/main/java/api/common/util/JsonUtil.java

@@ -2,7 +2,9 @@ package api.common.util;
 
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.JsonNode;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.TextNode;
 import com.fasterxml.jackson.databind.type.TypeFactory;
 
 
@@ -13,6 +15,8 @@ import java.util.Map;
 
 public class JsonUtil {
 
+    private static final ObjectMapper mapper = new ObjectMapper();
+
 
     /**
      * bean 转成 json
@@ -68,4 +72,13 @@ public class JsonUtil {
         }
         return new HashMap();
     }
+
+    public static JsonNode readTree(String json) {
+        try {
+            JsonNode jsonNode = mapper.readTree(json);
+        } catch (JsonProcessingException e) {
+            e.printStackTrace();
+        }
+        return new TextNode("");
+    }
 }

+ 18 - 0
simulation-resource-common/src/main/java/com/css/simulation/resource/common/configuration/algPlatform/ClientDetail.java

@@ -0,0 +1,18 @@
+package com.css.simulation.resource.common.configuration.algPlatform;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+@Data
+@Configuration
+@ConfigurationProperties(prefix = "alg-platform")
+public class ClientDetail {
+
+    private String appid;//客户端id
+    private String secret;//秘钥
+    private String tokenUri;//获取token接口
+    private String algorithmListUri;//获取列表查询接口
+    private String algorithmAddrUri;//获取算法地址接口
+
+}

+ 68 - 0
simulation-resource-common/src/main/java/com/css/simulation/resource/common/controller/AlgPlatformCtrl.java

@@ -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;
+    }
+
+}