吉安感知网项目-后端
linwei
2026-06-03 30dc5b1fa3bb545cb1c2f2445f26cb033d8295b9
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
177
178
179
package org.sxkj.gd.xingtu;
 
import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.sxkj.common.jaxt.JianXingTuApiClient;
import org.sxkj.gd.workorder.dto.GdXingtuFlyTaskSaveDTO;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
 
/**
 * @Description 星图接口服务
 * @Author AIX
 * @Date 2026/1/26 15:22
 * @Version 1.0
 */
@Slf4j
@Service
public class JianXingtuApiService {
 
    private final RestTemplate restTemplate;
    private final BladeRedis bladeRedis;
 
    public JianXingtuApiService(RestTemplate restTemplate, BladeRedis bladeRedis) {
        this.restTemplate = restTemplate;
        this.bladeRedis = bladeRedis;
    }
 
    private static final String RedisKey = "xingtu:token";
 
    /**
     * 获取星图登录token
     *
     * @return token
     */
    @SneakyThrows
    public String getToken() {
 
        if (Boolean.TRUE.equals(bladeRedis.exists(RedisKey))) {
            return bladeRedis.get(RedisKey);
        }
 
        String url = JianXingTuApiClient.getLoginUrl();
 
        Map<String, Object> params = new HashMap<>();
        // 添加认证参数
        params.put("username", "admin");
        params.put("password", "geovis@123");
        params.put("mobile", "true");
 
        // 发起请求
        ResponseEntity<R> response = restTemplate.postForEntity(url, params, R.class);
        if (response.getStatusCode().is2xxSuccessful()) {
            R body = response.getBody();
            if (Objects.requireNonNull(body).isSuccess()) {
 
                // 获取原始JSON字符串
                String jsonData = response.getBody().getData().toString();
 
                Map<String, String> jsonNode = parseKeyValue(jsonData);
                // 提取字段值
                String accessToken = jsonNode.get("access_token");
                String expiresIn = jsonNode.get("expires_in");
 
                bladeRedis.setEx(RedisKey, accessToken, Long.valueOf(expiresIn));
 
                return accessToken;
            }
        }
 
        return "获取token失败";
    }
 
    /**
     * 设备列表-无人机
     *
     * @param name 名称
     * @return 接口响应
     */
    public R getDevicePilotList(String name) {
        String url = JianXingTuApiClient.getDevicePilotListUrl();
        return postWithAuthHeader(url, buildListParams(name));
    }
 
    /**
     * 设备列表-机巢
     *
     * @param name 名称
     * @return 接口响应
     */
    public R getDeviceAirportList(String name) {
        String url = JianXingTuApiClient.getDeviceAirportListUrl();
        return postWithAuthHeader(url, buildListParams(name));
    }
 
    /**
     * 新增飞行任务
     *
     * @param param 请求参数
     * @return 接口响应
     */
    public R saveFlyTask(GdXingtuFlyTaskSaveDTO param) {
        String url = JianXingTuApiClient.getFlyTaskSaveUrl();
        return postWithAuthHeader(url, param);
    }
 
    private Map<String, Object> buildListParams(String name) {
        Map<String, Object> params = new HashMap<>();
        if (StringUtil.isNotBlank(name)) {
            params.put("name", name);
        }
        return params;
    }
 
    private R postWithAuthHeader(String url, Object body) {
        String token = getToken();
        if (StringUtil.isBlank(token) || "获取token失败".equals(token)) {
            return R.fail("获取token失败");
        }
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.set("authorization", token);
            log.info("请求外部接口: {} {}", url, JSON.toJSONString(body));
            HttpEntity<Object> entity = new HttpEntity<>(body, headers);
            ResponseEntity<R> response = restTemplate.exchange(url, HttpMethod.POST, entity, R.class);
            log.info("响应外部接口: {}", response);
            if (response.getStatusCode().is2xxSuccessful()) {
                R responseBody = response.getBody();
                if (responseBody != null) {
                    return responseBody;
                }
                return R.fail("接口返回为空");
            }
            return R.fail("请求外部接口失败");
        } catch (Exception e) {
            log.error("请求外部接口异常: ", e);
            return R.fail("请求外部接口异常: " + e.getMessage());
        }
    }
 
    /**
     * 解析JSON字符串为Map
     *
     * @param input JSON字符串
     * @return 解析后的Map
     */
    public static Map<String, String> parseKeyValue(String input) {
        Map<String, String> result = new HashMap<>();
 
        // 移除首尾花括号
        input = input.substring(1, input.length() - 1);
 
        String[] pairs = input.split(",");
        for (String pair : pairs) {
            int equalsIndex = pair.indexOf("=");
            if (equalsIndex > 0) {
                String key = pair.substring(0, equalsIndex).trim();
                String value = pair.substring(equalsIndex + 1);
                result.put(key, value);
            }
        }
 
        return result;
    }
 
 
}