吉安感知网项目-后端
xiebin
2026-01-06 d207a86cdf1ab52ef8cb7cd83bad8fceab8038cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package org.sxkj.odm.service;
 
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.sxkj.common.utils.SpringContextUtil;
import org.sxkj.odm.config.OdmProperties;
import org.sxkj.odm.constant.OdmConstant;
 
import javax.naming.directory.SearchResult;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * request 请求发送
 */
@Slf4j
@Component
public class PyTifSendRequest {
 
    @Autowired
    private RestTemplate restTemplate;
 
    @Autowired
    private OdmProperties odmProperties;
 
    /**
     * 调用接口获取数据-post-json
     *
     * @param url 请求url
     * @param map 数据
     * @return
     */
    public String sendPostJsonByMap(String url, Map<String, Object> map) {
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String json = JSONObject.toJSONString(map);
        //封装请求头
        HttpEntity<String> entity = new HttpEntity<>(json, headers);
        try {
            //发送请求
            String result = restTemplate.postForObject(url, entity, String.class);
            log.info("结果:{}", result);
            // 返回
            return result;
        } catch (Exception e) {
            log.error("请求异常:{}", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 调用白膜API规划飞行路径
     *
     * @param url           请求url
     * @param routeSegments 路线段列表,每个元素包含起始点和终点的经纬度及高度信息
     *                      格式: [{"start_latitude": 纬度, "start_longitude": 经度, "start_height": 高度,
     *                      "end_latitude": 纬度, "end_longitude": 经度, "end_height": 高度}]
     * @return API响应结果
     */
    public String planFlightPath(String url, List<Map<String, Object>> routeSegments) {
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
 
        //构建请求体 - 直接使用Map而不是JSONObject
        Map<String, Object> requestBody = new HashMap<>();
        requestBody.put("route_segments", routeSegments);
 
        //封装请求
        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(requestBody, headers);
 
        try {
            //发送POST请求
            String result = restTemplate.postForObject(url, entity, String.class);
            log.info("飞行路径规划结果:{}", result);
            return result;
        } catch (Exception e) {
            log.error("飞行路径规划请求异常:{}", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }
 
 
    /**
     * 调用接口获取数据-get-json
     *
     * @param url 请求url
     * @return
     */
    public SearchResult sendGetByMap(String url) {
        try {
            //发送请求
            SearchResult searchResult = restTemplate.getForObject(url, SearchResult.class);
            log.info("结果:{}", searchResult);
            // 返回
            return searchResult;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 调用接口获取数据-get-json
     *
     * @param url 请求地址
     * @param map 请求参数
     * @return 响应结果
     */
    public String sendGetJsonByMap(String url, Map<String, Object> map) {
        try {
            // 构建带参数的URL
            StringBuilder urlBuilder = new StringBuilder(url);
            if (map != null && !map.isEmpty()) {
                urlBuilder.append("?");
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    if (entry.getValue() != null) {
                        urlBuilder.append(entry.getKey())
                            .append("=")
                            .append(URLEncoder.encode(String.valueOf(entry.getValue()), "UTF-8"))
                            .append("&");
                    }
                }
                // 删除最后一个多余的&
                urlBuilder.deleteCharAt(urlBuilder.length() - 1);
            }
 
            // 发送GET请求
            String result = restTemplate.getForObject(urlBuilder.toString(), String.class);
            log.info("结果:{}", result);
            return result;
        } catch (Exception e) {
            log.error("发送GET请求失败,url: {}, 参数: {}", url, map, e);
            e.printStackTrace();
        }
        return null;
    }
 
 
    /**
     * 调用接口获取数据-post-json
     *
     * @param url  请求url
     * @param json 数据
     * @return
     */
    public JSONObject sendPostJsonByJson(String url, String json) {
        url = odmProperties.getApiBaseUrl() + url;
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        //封装请求头
        HttpEntity<String> entity = new HttpEntity<>(json, headers);
        try {
            //发送请求
            String result = restTemplate.postForObject(url, entity, String.class);
            JSONObject jsonObject = JSONObject.parseObject(result);
            // 返回
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}