|
@@ -0,0 +1,102 @@
|
|
|
+package com.sxtvs.api.youmei.service;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.github.benmanes.caffeine.cache.Cache;
|
|
|
+import com.github.benmanes.caffeine.cache.Caffeine;
|
|
|
+import com.sxtvs.api.youmei.dto.CheckWordResponse;
|
|
|
+import com.sxtvs.api.youmei.entity.YoumeiAccount;
|
|
|
+import com.sxtvs.api.youmei.mapper.YoumeiAccountMapper;
|
|
|
+import lombok.Cleanup;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.hc.client5.http.classic.methods.HttpPost;
|
|
|
+import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
|
|
|
+import org.apache.hc.client5.http.fluent.ContentResponseHandler;
|
|
|
+import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
|
|
+import org.apache.hc.client5.http.impl.classic.HttpClients;
|
|
|
+import org.apache.hc.core5.http.NameValuePair;
|
|
|
+import org.apache.hc.core5.http.message.BasicNameValuePair;
|
|
|
+import org.apache.http.Consts;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class YoumeiAccountServiceImpl extends ServiceImpl<YoumeiAccountMapper, YoumeiAccount> implements IYoumeiAccountService {
|
|
|
+
|
|
|
+ private final ObjectMapper objectMapper;
|
|
|
+
|
|
|
+ public YoumeiAccountServiceImpl(ObjectMapper objectMapper) {
|
|
|
+ this.objectMapper = objectMapper;
|
|
|
+ }
|
|
|
+
|
|
|
+ private final HashMap<Integer, String> belongMap = new HashMap<>() {{
|
|
|
+ put(105, "涉国家统一、主权和领土完整");
|
|
|
+ put(109, "涉民族宗教");
|
|
|
+ put(112, "涉黄、暴、恐、赌、毒");
|
|
|
+ put(111, "涉低俗辱骂");
|
|
|
+ put(108, "涉违法违规");
|
|
|
+ put(118, "其他敏感内容");
|
|
|
+ }};
|
|
|
+
|
|
|
+ private final Cache<String, Boolean> cache = Caffeine.newBuilder()
|
|
|
+ .expireAfterWrite(10, TimeUnit.MINUTES)
|
|
|
+ .maximumSize(10000)
|
|
|
+ .build();
|
|
|
+
|
|
|
+ private final Cache<String, String> tokenCache = Caffeine.newBuilder()
|
|
|
+ .expireAfterWrite(1, TimeUnit.MINUTES)
|
|
|
+ .build();
|
|
|
+
|
|
|
+
|
|
|
+ public String getWbjcTokenCache() {
|
|
|
+ return tokenCache.get("sxgdwbjc", this::getWbjcToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getWbjcToken(String accountName) {
|
|
|
+ return this.lambdaQuery()
|
|
|
+ .eq(YoumeiAccount::getOpenId, accountName)
|
|
|
+ .one().getAccessToken();
|
|
|
+ }
|
|
|
+
|
|
|
+ private final CloseableHttpClient client = HttpClients.createDefault();
|
|
|
+
|
|
|
+ @SneakyThrows
|
|
|
+ public Boolean wordCheck(String text) {
|
|
|
+ List<NameValuePair> formparams = new ArrayList<>();
|
|
|
+ formparams.add(new BasicNameValuePair("accessToken", getWbjcTokenCache()));
|
|
|
+ formparams.add(new BasicNameValuePair("text", text));
|
|
|
+ @Cleanup
|
|
|
+ UrlEncodedFormEntity params = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
|
|
|
+
|
|
|
+ var httpPost = new HttpPost("https://api-open-wx-www.yqt365.com/dataapp/api/umei/fw/open/wbjc/article_correct_external");
|
|
|
+ httpPost.setEntity(params);
|
|
|
+
|
|
|
+ var body = client.execute(httpPost, new ContentResponseHandler());
|
|
|
+
|
|
|
+ CheckWordResponse checkWordDto = objectMapper.readValue(body.asString(StandardCharsets.UTF_8), CheckWordResponse.class);
|
|
|
+ CheckWordResponse.DataDTO data = checkWordDto.getData();
|
|
|
+
|
|
|
+ for (CheckWordResponse.DataDTO.ChecklistDTO checklistDTO : data.getChecklist()) {
|
|
|
+ var belongId = checklistDTO.getType().getBelongId();
|
|
|
+ if (belongMap.containsKey(belongId)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public Boolean wordCheckCache(String text) {
|
|
|
+ return cache.get(text, this::wordCheck);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|