Browse Source

add:新增接口(ai律师申诉)

lyx 6 hours ago
parent
commit
cfdbc877c3

+ 65 - 0
alien-lawyer/src/main/java/shop/alien/lawyer/controller/AiAutoReview.java

@@ -0,0 +1,65 @@
+package shop.alien.lawyer.controller;
+
+import com.alibaba.fastjson2.JSONObject;
+import io.swagger.annotations.Api;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.util.StringUtils;
+import org.springframework.web.bind.annotation.CrossOrigin;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.client.RestTemplate;
+import shop.alien.lawyer.util.ai.AiAuthTokenUtil;
+
+import java.util.Map;
+
+@Slf4j
+@Api(tags = {"ai申诉"})
+@CrossOrigin
+@RestController
+@RequestMapping("/aiAutoReview")
+@RequiredArgsConstructor
+public class AiAutoReview {
+
+    private final AiAuthTokenUtil aiAuthTokenUtil;
+
+    private final RestTemplate restTemplate;
+
+    @Value("${third-party-ai-auto-review.base-url:http://192.168.2.250:9100/ai/auto-review/api/v1/lawyer_complaint_audit_task/submit}")
+    private String aiAutoReviewUrl;
+
+    /**
+     * 调用 AI 服务,获取申诉结果
+     *
+     * @return 申诉结果
+     */
+    @RequestMapping("/aiReview")
+    public ResponseEntity<String> aiChat(@RequestBody Map<String, Object> requestBody){
+        String accessToken = aiAuthTokenUtil.getAccessToken();
+        JSONObject data = new JSONObject();
+        if (!StringUtils.hasText(accessToken)) {
+            data.put("fail","登录失败");
+            return ResponseEntity.badRequest().body(data.toJSONString());
+        }
+
+        // 初始化请求体Map
+        HttpHeaders aiHeaders = new HttpHeaders();
+        aiHeaders.setContentType(MediaType.APPLICATION_JSON);
+        aiHeaders.set("Authorization", "Bearer " + accessToken);
+
+        HttpEntity<Map<String, Object>> request = new HttpEntity<>(requestBody, aiHeaders);
+        try {
+            ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(aiAutoReviewUrl, request, String.class);
+            return stringResponseEntity;
+        } catch (Exception e) {
+            log.error("调用AI文本审核接口 接口异常------", e);
+        }
+        return  ResponseEntity.badRequest().body(null);
+    }
+}

+ 78 - 0
alien-lawyer/src/main/java/shop/alien/lawyer/util/ai/AiAuthTokenUtil.java

@@ -0,0 +1,78 @@
+package shop.alien.lawyer.util.ai;
+
+import com.alibaba.fastjson2.JSONObject;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.*;
+import org.springframework.stereotype.Component;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+import org.springframework.util.StringUtils;
+import org.springframework.web.client.RestTemplate;
+
+/**
+ * AI 服务鉴权配置
+ */
+@Slf4j
+@Component
+@RequiredArgsConstructor
+public class AiAuthTokenUtil {
+
+    private final RestTemplate restTemplate;
+
+    @Value("${third-party-login.base-url}")
+    private String loginUrl;
+
+    @Value("${third-party-user-name.base-url}")
+    private String userName;
+
+    @Value("${third-party-pass-word.base-url}")
+    private String passWord;
+
+    /**
+     * 登录 AI 服务,获取 token
+     *
+     * @return accessToken
+     */
+    public String getAccessToken() {
+        log.info("登录Ai服务获取token...{}", loginUrl);
+        MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
+        formData.add("username", userName);
+        formData.add("password", passWord);
+
+        HttpHeaders headers = new HttpHeaders();
+        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
+        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(formData, headers);
+
+        ResponseEntity<String> response;
+        try {
+            log.info("请求Ai服务登录接口===================>");
+            response = restTemplate.postForEntity(loginUrl, requestEntity, String.class);
+        } catch (Exception e) {
+            log.error("请求AI服务登录接口失败", e);
+            return null;
+        }
+
+        if (response != null && response.getStatusCode() == HttpStatus.OK) {
+            String body = response.getBody();
+            log.info("请求Ai服务登录成功 postForEntity.getBody()\t{}", body);
+            if (StringUtils.hasText(body)) {
+                JSONObject jsonObject = JSONObject.parseObject(body);
+                if (jsonObject != null) {
+                    JSONObject dataJson = jsonObject.getJSONObject("data");
+                    if (dataJson != null) {
+                        return dataJson.getString("access_token");
+                    }
+                }
+            }
+            log.warn("AI服务登录响应解析失败 body: {}", body);
+            return null;
+        }
+
+        log.error("请求AI服务 登录接口失败 http状态:{}", response != null ? response.getStatusCode() : null);
+        return null;
+    }
+}
+
+