1
This commit is contained in:
@@ -120,9 +120,7 @@ public class AuthController {
|
||||
public JsonData saveCache(@RequestBody Map<String, Object> data) {
|
||||
String key = (String) data.get("key");
|
||||
String value = (String) data.get("value");
|
||||
|
||||
if (key == null || value == null) return JsonData.buildError("key和value不能为空");
|
||||
|
||||
CacheDataEntity entity = cacheDataRepository.findByCacheKey(key).orElse(new CacheDataEntity());
|
||||
entity.setCacheKey(key);
|
||||
entity.setCacheValue(value);
|
||||
@@ -157,6 +155,7 @@ public class AuthController {
|
||||
if (key == null || key.trim().isEmpty()) {
|
||||
return JsonData.buildError("key不能为空");
|
||||
}
|
||||
System.out.println("key: " + key);
|
||||
cacheDataRepository.deleteByCacheKey(key);
|
||||
return JsonData.buildSuccess("缓存数据删除成功");
|
||||
}
|
||||
@@ -165,18 +164,16 @@ public class AuthController {
|
||||
* 会话引导:检查SQLite中是否存在token
|
||||
*/
|
||||
@GetMapping("/session/bootstrap")
|
||||
public ResponseEntity<?> sessionBootstrap() {
|
||||
public JsonData sessionBootstrap() {
|
||||
Optional<CacheDataEntity> tokenEntity = cacheDataRepository.findByCacheKey("token");
|
||||
if (tokenEntity.isEmpty()) {
|
||||
return ResponseEntity.status(401).body(Map.of("code", 401, "message", "无可用会话,请重新登录"));
|
||||
return JsonData.buildError("无可用会话,请重新登录");
|
||||
}
|
||||
String token = tokenEntity.get().getCacheValue();
|
||||
if (token == null || token.isEmpty()) {
|
||||
return ResponseEntity.status(401).body(Map.of("code", 401, "message", "无可用会话,请重新登录"));
|
||||
return JsonData.buildError("无可用会话,请重新登录");
|
||||
}
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add(HttpHeaders.SET_COOKIE, buildHttpOnlyCookie("FX_TOKEN", token, 2 * 24 * 60 * 60));
|
||||
return ResponseEntity.ok().headers(headers).body(Map.of("code", 200, "message", "会话已恢复"));
|
||||
return JsonData.buildSuccess("会话已恢复");
|
||||
}
|
||||
|
||||
private String buildHttpOnlyCookie(String name, String value, int maxAgeSeconds) {
|
||||
|
||||
@@ -42,6 +42,11 @@ public class DeviceProxyController {
|
||||
return apiForwarder.post("/monitor/device/remove", body, auth);
|
||||
}
|
||||
|
||||
@PostMapping("/api/device/offline")
|
||||
public ResponseEntity<?> deviceOffline(@RequestBody Map<String, Object> body, @RequestHeader(value = "Authorization", required = false) String auth) {
|
||||
return apiForwarder.post("/monitor/device/offline", body, auth);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设备心跳
|
||||
*/
|
||||
|
||||
@@ -83,6 +83,7 @@ public class Alibaba1688ServiceImpl implements Alibaba1688Service {
|
||||
*/
|
||||
@Override
|
||||
public SearchResult get1688Detail(String uploadedUrl) {
|
||||
uploadedUrl = uploadedUrl.split("\\?")[0];
|
||||
String fileName = "temp_" + System.currentTimeMillis() + ".png";
|
||||
List<String> detailUrls = new ArrayList<>();
|
||||
SearchResult result = new SearchResult();
|
||||
@@ -101,21 +102,36 @@ public class Alibaba1688ServiceImpl implements Alibaba1688Service {
|
||||
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
|
||||
formData.add("data", jsonData);
|
||||
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, headers);
|
||||
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
|
||||
JsonNode root = objectMapper.readTree(response.getBody());
|
||||
Iterator<JsonNode> offerIterator = root.path("data").path("offerList").path("offers").elements();
|
||||
//运费
|
||||
Iterator<JsonNode> offerIterator = null;
|
||||
for (int retry = 0; retry < 3 && (offerIterator == null || !offerIterator.hasNext()); retry++) {
|
||||
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
|
||||
JsonNode root = objectMapper.readTree(response.getBody());
|
||||
offerIterator = root.path("data").path("offerList").path("offers").elements();
|
||||
}
|
||||
//运费 - 收集所有运费数据
|
||||
Set<Double> freight = new HashSet<>();
|
||||
for (int i = 0; i < 10 && offerIterator.hasNext(); i++) {
|
||||
while (offerIterator.hasNext()) {
|
||||
JsonNode offer = offerIterator.next();
|
||||
String offerId = offer.path("id").asText();
|
||||
String freightProvFirstFee = offer.path("freightProvFirstFee").asText();
|
||||
Optional.ofNullable(freightProvFirstFee)
|
||||
.map(s -> s.split(";", 2)[0])
|
||||
.map(s -> s.split(":", 2))
|
||||
.filter(parts -> parts.length == 2 && !parts[1].isBlank())
|
||||
.map(parts -> Double.parseDouble(parts[1]) / 100.0)
|
||||
.filter(fee -> fee > 0)
|
||||
.ifPresent(freight::add);
|
||||
}
|
||||
|
||||
offerIterator = null;
|
||||
for (int retry = 0; retry < 3 && (offerIterator == null || !offerIterator.hasNext()); retry++) {
|
||||
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
|
||||
JsonNode root = objectMapper.readTree(response.getBody());
|
||||
offerIterator = root.path("data").path("offerList").path("offers").elements();
|
||||
}
|
||||
|
||||
for (int i = 0; i < 10 && offerIterator.hasNext(); i++) {
|
||||
JsonNode offer = offerIterator.next();
|
||||
String offerId = offer.path("id").asText();
|
||||
prices.add(offer.path("normalPrice").asDouble());
|
||||
detailUrls.add(offerId);
|
||||
}
|
||||
@@ -136,12 +152,12 @@ public class Alibaba1688ServiceImpl implements Alibaba1688Service {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("url"+uploadedUrl);
|
||||
System.out.println("skuPrices:"+skuPrices);
|
||||
result.setSkuPrice(skuPrices);
|
||||
result.setMedian( median);
|
||||
result.setMapRecognitionLink( uploadImageBase64(imageUrl));
|
||||
System.out.println("运费"+freightFee);
|
||||
result.setFreight(freightFee.isEmpty() ? 0.0 :freightFee.get(freightFee.size()/2-1));
|
||||
result.setFreight(freightFee.isEmpty() ? 0.0 : freightFee.get(Math.max(0, freightFee.size()/2-1)));
|
||||
// String weight = getWeight(detailUrls);
|
||||
// result.setWeight(weight);
|
||||
return result;
|
||||
|
||||
@@ -81,6 +81,7 @@ public class AuthServiceImpl implements IAuthService {
|
||||
|
||||
if (accessToken != null) {
|
||||
saveTokenToCache(accessToken);
|
||||
registerDeviceOnLogin(username);
|
||||
}
|
||||
|
||||
result.put("success", true);
|
||||
@@ -201,6 +202,7 @@ public class AuthServiceImpl implements IAuthService {
|
||||
accessToken = newAccessToken;
|
||||
refreshToken = newRefreshToken;
|
||||
saveTokenToCache(newAccessToken);
|
||||
registerDeviceOnLogin(username);
|
||||
}
|
||||
|
||||
result.put("success", true);
|
||||
@@ -238,6 +240,9 @@ public class AuthServiceImpl implements IAuthService {
|
||||
*/
|
||||
public void logout() {
|
||||
try {
|
||||
// 通知服务器设备离线
|
||||
setDeviceOffline();
|
||||
|
||||
// 清除内存中的token
|
||||
accessToken = null;
|
||||
refreshToken = null;
|
||||
@@ -246,4 +251,28 @@ public class AuthServiceImpl implements IAuthService {
|
||||
cacheDataRepository.deleteByCacheKey("token");
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* 登录时注册设备
|
||||
*/
|
||||
private void registerDeviceOnLogin(String username) {
|
||||
try {
|
||||
Map<String, Object> deviceData = new HashMap<>();
|
||||
deviceData.put("username", username);
|
||||
deviceData.put("deviceId", clientId);
|
||||
deviceData.put("os", System.getProperty("os.name"));
|
||||
apiForwarder.post("/monitor/device/register", deviceData, buildAuthHeader());
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设备离线
|
||||
*/
|
||||
private void setDeviceOffline() {
|
||||
try {
|
||||
Map<String, Object> offlineData = new HashMap<>();
|
||||
offlineData.put("deviceId", clientId);
|
||||
apiForwarder.post("/monitor/device/offline", offlineData, buildAuthHeader());
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user