08-21-GPS获取下级PC列表

This commit is contained in:
2025-08-21 17:35:32 +08:00
parent 31d7055e3e
commit e05b53f474
2 changed files with 60 additions and 13 deletions

View File

@ -1,14 +1,16 @@
package org.dromara.gps.controller;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.gps.domain.GpsLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
@ -29,8 +31,9 @@ public class GpsController {
.connectTimeout(Duration.ofSeconds(10)) // 设置连接超时
.build();
@GetMapping("/login")
public R<Void> login(@RequestParam GpsLogin loginData) throws IOException, InterruptedException {
//登录
@PostMapping("/login")
public R<T> login(@RequestBody GpsLogin loginData) throws IOException, InterruptedException {
//对象转HashMap
ObjectMapper mapper = new ObjectMapper();
@ -38,11 +41,14 @@ public class GpsController {
//拼接URL
String baseUrl = "http://openapi.18gps.net//GetDateServices.asmx/loginSystem";
String query = params.entrySet().stream()
.map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(Collectors.joining("&"));
// String query = params.entrySet().stream()
// .map(entry -> entry.getKey() + "=" + entry.getValue())
// .collect(Collectors.joining("&"));
String query = "LoginName="+params.get("loginName")+"&"+"LoginPassword="+params.get("loginPassword")+"&"+"LoginType="+params.get("loginType")+"&"+"language="+params.get("language")+"&"+"apply="+params.get("apply")+"&"+"ISMD5="+params.get("ISMD5")+"&"+"ApiKey="+params.get("apiKey");
String requestUrl = baseUrl + "?" + query;
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(requestUrl))
.timeout(Duration.ofSeconds(10)) // 设置响应超时
@ -56,8 +62,49 @@ public class GpsController {
);
log.info("响应体:{}", response.body());
if (!response.body().contains("\"success\":\"true\"")){
return R.fail();
}
//登录成功转为JSON 从Json中获取id和mds进行缓存
JSONObject jsonObject = JSONObject.parseObject(response.body());
RedisUtils.setCacheObject(loginData.getLoginName()+"_GPS_TOKEN_ID", jsonObject.get("id"), Duration.ofMillis(10));
RedisUtils.setCacheObject(loginData.getLoginName()+"_GPS_TOKEN_MDS", jsonObject.get("mds"), Duration.ofHours(10));
return R.ok();
}
//获取PC列表
@GetMapping("/getPcList")
public R<Object> getPcList(@RequestParam String mds, @RequestParam String id) throws IOException, InterruptedException {
//拼接 url
String baseURL = "http://openapi.18gps.net//GetDateServices.asmx/GetDate";
String query = "method="+"getDeviceListByCustomId"+"&mds="+mds+"&id="+id+"&mapType="+"BAIDU";
String requestUrl = baseURL + "?" + query;
//构造请求
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(requestUrl))
.timeout(Duration.ofSeconds(10)) // 设置响应超时
.GET() // 使用GET请求方式
.build();
//获取响应
HttpResponse<String> response = client.send(
request,
HttpResponse.BodyHandlers.ofString()
);
if (!response.body().contains("\"success\":\"true\"")){
return R.fail();
}
JSONObject jsonObject = JSONObject.parseObject(response.body());
return R.ok(jsonObject);
}
}

View File

@ -5,14 +5,14 @@ import lombok.Data;
@Data
public class GpsLogin {
private String LoginName;
private String loginName;
private String LoginPassword;
private String loginPassword;
/**
* 登陆类型 [单位用户:ENTERPRISE/设备登录:USER]
*/
private String LoginType;
private String loginType;
/***
* 语言类型 [cn,中文;en,英文]
@ -32,6 +32,6 @@ public class GpsLogin {
/**
* 应用域 Id应用域请找台提供。可以不填但最好填。
*/
private String ApiKey;
private String apiKey;
}