From a7c7965a518359a1c81bde0a8c451f2fc1cc25fe Mon Sep 17 00:00:00 2001
From: zhangjian <zhangjianrock@163.com>
Date: 星期五, 07 七月 2023 16:31:40 +0800
Subject: [PATCH] 无人机转发接口

---
 ard-work/pom.xml                                                             |   11 ++
 ard-work/src/main/java/com/ruoyi/utils/forest/UavClient.java                 |   26 ++++++
 ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java |  132 +++++++++++++++++++++++++++++++++
 ard-work/src/main/java/com/ruoyi/utils/forest/UavAuthLifeCircle.java         |   40 ++++++++++
 ard-work/src/main/java/com/ruoyi/utils/forest/UavAuth.java                   |   18 ++++
 5 files changed, 227 insertions(+), 0 deletions(-)

diff --git a/ard-work/pom.xml b/ard-work/pom.xml
index 0047bfb..2d02074 100644
--- a/ard-work/pom.xml
+++ b/ard-work/pom.xml
@@ -16,6 +16,17 @@
     <dependencies>
         <!-- 閫氱敤宸ュ叿-->
         <dependency>
+            <groupId>commons-codec</groupId>
+            <artifactId>commons-codec</artifactId>
+            <version>1.10</version>
+        </dependency>
+
+
+        <dependency>
+            <groupId>com.ruoyi</groupId>
+            <artifactId>ruoyi-common</artifactId>
+        </dependency>
+        <dependency>
             <groupId>com.ruoyi</groupId>
             <artifactId>ruoyi-common</artifactId>
         </dependency>
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
new file mode 100644
index 0000000..a78bb9e
--- /dev/null
+++ b/ard-work/src/main/java/com/ruoyi/device/uav/controller/ArdUavController.java
@@ -0,0 +1,132 @@
+package com.ruoyi.device.uav.controller;
+
+import com.dtflys.forest.exceptions.ForestNetworkException;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.github.pagehelper.util.StringUtil;
+import com.ruoyi.common.core.controller.BaseController;
+import com.ruoyi.utils.forest.UavClient;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.codec.binary.StringUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import javax.crypto.Cipher;
+import javax.crypto.spec.SecretKeySpec;
+import java.util.*;
+
+import org.apache.commons.codec.binary.Base64;
+
+/**
+ * 鐩告満璁惧Controller
+ *
+ * @author ruoyi
+ * @date 2023-01-31
+ */
+@Slf4j(topic = "uav")
+@RestController
+@RequestMapping("/device/uav")
+@Api(tags = "鏃犱汉鏈哄閮ㄦ帴鍙�")
+public class ArdUavController extends BaseController {
+    public static final String USERNAME = "ardbailu";
+    public static final String PASSWORD = "ardkj12345";
+    public static final String SALT = "0123456789012345";
+
+    //token 鐧婚檰鍚庢瘡娆¤姹�,鍦╤eader涓惡甯�
+    private String token = null;
+    private ObjectMapper om = new ObjectMapper();
+
+    @Autowired
+    private UavClient uavClient;
+
+    public void uavLogin() {
+        String codedPassword = this.Encrypt(PASSWORD, SALT);
+        String body = "{\"username\":\"" + USERNAME + "\",\"password\":\"" + codedPassword + "\"}";
+        try {
+            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");
+        } catch (ForestNetworkException e) {
+            e.printStackTrace();
+        } catch (JsonMappingException e) {
+            throw new RuntimeException(e);
+        } catch (JsonProcessingException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    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) {
+        String res = null;
+        String token = getToken();
+        try {
+            res = uavClient.get(url, token, data);
+        } catch (ForestNetworkException e) {
+            if (e.getStatusCode() == 401) {
+                this.uavLogin();
+                token = getToken();
+                res = uavClient.get(url, token, data);
+            }
+        }
+
+        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;
+    }
+
+    public String Encrypt(String sSrc, String sKey) {
+        if (sKey == null) {
+            System.out.print("Key涓虹┖null");
+            return null;
+        }
+        // 鍒ゆ柇Key鏄惁涓�16浣�
+        if (sKey.length() != 16) {
+            System.out.print("Key闀垮害涓嶆槸16浣�");
+            return null;
+        }
+        byte[] encrypted = null;
+        try {
+            byte[] raw = sKey.getBytes("utf-8");
+            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
+            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");//"绠楁硶/妯″紡/琛ョ爜鏂瑰紡"
+            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
+            encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
+        } catch (Exception e) {
+            e.printStackTrace();
+            ;
+        }
+        return new Base64().encodeToString(encrypted);//姝ゅ浣跨敤BASE64鍋氳浆鐮佸姛鑳斤紝鍚屾椂鑳借捣鍒�2娆″姞瀵嗙殑浣滅敤銆�
+    }
+
+}
diff --git a/ard-work/src/main/java/com/ruoyi/utils/forest/UavAuth.java b/ard-work/src/main/java/com/ruoyi/utils/forest/UavAuth.java
new file mode 100644
index 0000000..78a93f0
--- /dev/null
+++ b/ard-work/src/main/java/com/ruoyi/utils/forest/UavAuth.java
@@ -0,0 +1,18 @@
+package com.ruoyi.utils.forest;
+
+
+import com.dtflys.forest.annotation.MethodLifeCycle;
+import com.dtflys.forest.annotation.RequestAttributes;
+
+import java.lang.annotation.*;
+
+@MethodLifeCycle(UavAuthLifeCircle.class)
+@Documented
+/** 閲嶇偣锛� @MethodLifeCycle娉ㄨВ鎸囧畾璇ユ敞瑙g殑鐢熷懡鍛ㄦ湡绫�*/
+@RequestAttributes
+@Retention(RetentionPolicy.RUNTIME)
+/** 鎸囧畾璇ユ敞瑙e彲鐢ㄤ簬绫讳笂鎴栨柟娉曚笂 */
+@Target({ElementType.TYPE, ElementType.METHOD})
+public @interface UavAuth {
+    String token();
+}
diff --git a/ard-work/src/main/java/com/ruoyi/utils/forest/UavAuthLifeCircle.java b/ard-work/src/main/java/com/ruoyi/utils/forest/UavAuthLifeCircle.java
new file mode 100644
index 0000000..3173958
--- /dev/null
+++ b/ard-work/src/main/java/com/ruoyi/utils/forest/UavAuthLifeCircle.java
@@ -0,0 +1,40 @@
+package com.ruoyi.utils.forest;
+
+import com.dtflys.forest.http.ForestRequest;
+import com.dtflys.forest.lifecycles.MethodAnnotationLifeCycle;
+import com.dtflys.forest.reflection.ForestMethod;
+
+public class UavAuthLifeCircle implements MethodAnnotationLifeCycle<UavAuth, Object> {
+
+
+    /**
+     * 褰撴柟娉曡皟鐢ㄦ椂璋冪敤姝ゆ柟娉曪紝姝ゆ椂杩樻病鏈夋墽琛岃姹傚彂閫�
+     * 娆℃柟娉曞彲浠ヨ幏寰楄姹傚搴旂殑鏂规硶璋冪敤淇℃伅锛屼互鍙婂姩鎬佷紶鍏ョ殑鏂规硶璋冪敤鍙傛暟鍒楄〃
+     */
+    @Override
+    public void onInvokeMethod(ForestRequest request, ForestMethod method, Object[] args) {
+        System.out.println("Invoke Method '" + method.getMethodName() + "' Arguments: " + args);
+    }
+
+    /**
+     * 鍙戦�佽姹傚墠鎵ц姝ゆ柟娉�
+     */
+    @Override
+    public boolean beforeExecute(ForestRequest request) {
+        String token = (String) getAttribute(request, "token");
+        System.out.println(" UavAuthLifeCircle token:"+token);
+        if(token!=null){
+            request.addHeader("x-auth-token", token);
+        }
+        return true;
+    }
+
+    /**
+     * 姝ゆ柟娉曞湪璇锋眰鏂规硶鍒濆鍖栫殑鏃跺�欒璋冪敤
+     */
+
+    @Override
+    public void onMethodInitialized(ForestMethod forestMethod, UavAuth uavAuth) {
+        System.out.println("Method '" + forestMethod.getMethodName());
+    }
+}
diff --git a/ard-work/src/main/java/com/ruoyi/utils/forest/UavClient.java b/ard-work/src/main/java/com/ruoyi/utils/forest/UavClient.java
new file mode 100644
index 0000000..4efe3bb
--- /dev/null
+++ b/ard-work/src/main/java/com/ruoyi/utils/forest/UavClient.java
@@ -0,0 +1,26 @@
+package com.ruoyi.utils.forest;
+
+import com.dtflys.forest.annotation.DataVariable;
+import com.dtflys.forest.annotation.Get;
+import com.dtflys.forest.annotation.JSONBody;
+import com.dtflys.forest.annotation.Post;
+import org.springframework.stereotype.Component;
+
+@Component
+public interface UavClient {
+    @Get("http://www.baidu.com")
+    String test();
+
+    public static String IP = "http://112.98.126.2:6100/";
+
+
+    @UavAuth(token = "${token}")
+    @Get(IP + "${url}")
+    String get(@DataVariable("url") String url, @DataVariable("token") String token, @JSONBody String body);
+
+    @UavAuth(token = "${token}")
+    @Post(IP + "${url}")
+    String post(@DataVariable("url") String url, @DataVariable("token") String token, @JSONBody String body);
+
+
+}

--
Gitblit v1.9.3