| New file |
| | |
| | | package org.sxkj.gd.xingtu; |
| | | |
| | | import lombok.SneakyThrows; |
| | | import org.springblade.core.redis.cache.BladeRedis; |
| | | import org.springblade.core.tool.api.R; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.client.RestTemplate; |
| | | import org.sxkj.common.jaxt.JianXingTuApiClient; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.Objects; |
| | | |
| | | /** |
| | | * @Description 星图接口服务 |
| | | * @Author AIX |
| | | * @Date 2026/1/26 15:22 |
| | | * @Version 1.0 |
| | | */ |
| | | @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失败"; |
| | | } |
| | | |
| | | /** |
| | | * 解析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; |
| | | } |
| | | |
| | | |
| | | } |