From 4bcf7f6452d6ae04f210fc5c8408ad928d6602f4 Mon Sep 17 00:00:00 2001 From: zhangjian <zhangjianrock@163.com> Date: 星期一, 10 七月 2023 15:20:46 +0800 Subject: [PATCH] 无人机转发接口反射调用 --- ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java | 78 +++++++++++++++++++++++---------------- 1 files changed, 46 insertions(+), 32 deletions(-) diff --git a/ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java b/ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java index a78bb9e..cf246b5 100644 --- a/ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java +++ b/ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java @@ -11,14 +11,22 @@ import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.binary.StringUtils; +import org.jetbrains.annotations.Nullable; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpMethod; import org.springframework.web.bind.annotation.*; +import javax.annotation.PostConstruct; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.*; import org.apache.commons.codec.binary.Base64; + +import static org.springframework.http.HttpMethod.GET; +import static org.springframework.http.HttpMethod.POST; /** * 鐩告満璁惧Controller @@ -36,20 +44,27 @@ public static final String SALT = "0123456789012345"; //token 鐧婚檰鍚庢瘡娆¤姹�,鍦╤eader涓惡甯� - private String token = null; + private String token;//鐧诲綍鎴愬姛杩斿洖鐨則oken private ObjectMapper om = new ObjectMapper(); @Autowired private UavClient uavClient; + @PostConstruct + public void created() { + this.uavLogin(); + } + public void uavLogin() { String codedPassword = this.Encrypt(PASSWORD, SALT); String body = "{\"username\":\"" + USERNAME + "\",\"password\":\"" + codedPassword + "\"}"; try { - String res = uavClient.post("login", null, body); + String res = uavClient.POST("login", null, body); Map resMap = om.readValue(res, Map.class); Map data = (Map) resMap.get("data"); - this.token = (String) data.get("access_token"); + if (data != null) { + this.token = (String) data.get("access_token"); + } } catch (ForestNetworkException e) { e.printStackTrace(); } catch (JsonMappingException e) { @@ -59,50 +74,49 @@ } } - public String getToken() { - if (StringUtil.isEmpty(this.token)) { - this.uavLogin(); - } - return this.token; - } - @GetMapping("/") @ApiOperation("鏃犱汉鏈篻et鎺ュ彛") public Object get(@RequestParam String url, @RequestParam String data) { + return doUavRequest(GET, url, data); + + } + + @Nullable + private String doUavRequest(HttpMethod method, String url, String data) { String res = null; - String token = getToken(); + Method requestMethod = null; + //鑾峰彇method try { - res = uavClient.get(url, token, data); - } catch (ForestNetworkException e) { - if (e.getStatusCode() == 401) { + requestMethod = UavClient.class.getMethod(method.name(), String.class, String.class, String.class); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } + //鎵цmethod + try { + res = (String) requestMethod.invoke(this.uavClient, url, this.token, data); + } catch (InvocationTargetException e) { + ForestNetworkException fe = (ForestNetworkException) e.getCause(); + if (fe.getStatusCode() == 401) {//token澶辨晥,閲嶆柊鐧诲綍 this.uavLogin(); - token = getToken(); - res = uavClient.get(url, token, data); } + try {//鍐嶆璋冪敤鎺ュ彛 + res = (String) requestMethod.invoke(this.uavClient, url, this.token, data); + } catch (IllegalAccessException ex) { + throw new RuntimeException(ex); + } catch (InvocationTargetException ex) { + throw new RuntimeException(ex); + } + } catch (IllegalAccessException e) { + throw new RuntimeException(e); } return res; - } @PostMapping("/") @ApiOperation("鏃犱汉鏈簆ost鎺ュ彛") public Object post(@RequestParam String url, @RequestParam String data) { - String res = null; - String token = getToken(); - try { - res = uavClient.post(url, token, data); - } catch (ForestNetworkException e) { - if (e.getStatusCode() == 401) { - this.uavLogin(); - token = getToken(); - res = uavClient.post(url, token, data); - } - }catch (Exception e){ - System.out.println(e.getMessage()); - } - - return res; + return doUavRequest(POST, url, data); } public String Encrypt(String sSrc, String sKey) { -- Gitblit v1.9.3