59 行
2.1 KiB
Java
59 行
2.1 KiB
Java
|
|
package com.xuqm.im.service;
|
||
|
|
|
||
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
||
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||
|
|
import org.springframework.beans.factory.annotation.Value;
|
||
|
|
import org.springframework.http.HttpEntity;
|
||
|
|
import org.springframework.http.HttpHeaders;
|
||
|
|
import org.springframework.http.HttpMethod;
|
||
|
|
import org.springframework.http.ResponseEntity;
|
||
|
|
import org.springframework.stereotype.Component;
|
||
|
|
import org.springframework.web.client.RestTemplate;
|
||
|
|
import org.springframework.web.util.UriComponentsBuilder;
|
||
|
|
|
||
|
|
@Component
|
||
|
|
public class ImFeatureConfigClient {
|
||
|
|
|
||
|
|
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
|
||
|
|
|
||
|
|
private final RestTemplate restTemplate;
|
||
|
|
|
||
|
|
@Value("${im.tenant-service-url:http://192.168.116.9:8081}")
|
||
|
|
private String tenantServiceUrl;
|
||
|
|
|
||
|
|
@Value("${im.internal-token:xuqm-internal-token}")
|
||
|
|
private String internalToken;
|
||
|
|
|
||
|
|
public ImFeatureConfigClient() {
|
||
|
|
this.restTemplate = new RestTemplate();
|
||
|
|
}
|
||
|
|
|
||
|
|
public boolean allowStrangerMessage(String appId) {
|
||
|
|
String url = UriComponentsBuilder.fromHttpUrl(tenantServiceUrl)
|
||
|
|
.path("/api/internal/sdk/apps/{appId}/services/{platform}/{serviceType}")
|
||
|
|
.buildAndExpand(appId, "ANDROID", "IM")
|
||
|
|
.toUriString();
|
||
|
|
HttpHeaders headers = new HttpHeaders();
|
||
|
|
headers.set("X-Internal-Token", internalToken);
|
||
|
|
try {
|
||
|
|
ResponseEntity<JsonNode> response = restTemplate.exchange(
|
||
|
|
url,
|
||
|
|
HttpMethod.GET,
|
||
|
|
new HttpEntity<>(headers),
|
||
|
|
JsonNode.class
|
||
|
|
);
|
||
|
|
JsonNode body = response.getBody();
|
||
|
|
if (response.getStatusCode().is2xxSuccessful() && body != null && body.path("code").asInt() == 200) {
|
||
|
|
String config = body.path("data").path("config").asText("");
|
||
|
|
if (config.isBlank()) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return OBJECT_MAPPER.readTree(config).path("allowStrangerMessage").asBoolean(false);
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|