Parcourir la source

bugfix:ai搜索

lyx il y a 3 mois
Parent
commit
b572b31577

+ 93 - 0
alien-store/src/main/java/shop/alien/store/controller/AiSearchController.java

@@ -0,0 +1,93 @@
+package shop.alien.store.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.cloud.context.config.annotation.RefreshScope;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+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.entity.result.R;
+
+import java.util.HashMap;
+import java.util.Map;
+
+
+@Slf4j
+@Api(tags = {"ai搜索"})
+@CrossOrigin
+@RestController
+@RequestMapping("/aiSearch")
+@RequiredArgsConstructor
+@RefreshScope
+public class AiSearchController {
+
+    @Value("${third-party-ai-search.base-url:http://124.93.18.180:7870/api/v1/search}")
+    private String aiSearchUrl;
+
+    private final RestTemplate restTemplate;
+
+    @RequestMapping("/search")
+    public R search(@RequestBody Map<String,String> map) {
+
+
+        // 初始化请求体Map
+        Map<String, Object> requestBody = new HashMap<>();
+        requestBody.put("query", map.get("storeName"));
+        requestBody.put("limit", map.get("pageSize"));
+        requestBody.put("user_lat", map.get("lat"));
+        requestBody.put("user_lng", map.get("lan"));
+        requestBody.put("category", map.get("category"));
+        HttpHeaders aiHeaders = new HttpHeaders();
+        aiHeaders.setContentType(MediaType.APPLICATION_JSON);
+//        aiHeaders.set("Authorization", "Bearer " + accessToken);
+//        aiHeaders.set("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ1cHN0b3JlQGFkbWluLmNvbSIsImlkIjo2LCJ0aW1lIjoxNzYyOTI1NDAzLjY1MTY5MjZ9.07lz8Ox2cGC28UCmqcKCt5R6Rfwtgs-Eiu0ttgWRxws");
+
+        HttpEntity<Map<String, Object>> request = new HttpEntity<>(requestBody, null);
+        try {
+            ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(aiSearchUrl, request, String.class);
+            String body = stringResponseEntity.getBody();
+            JSONObject jsonObject = JSONObject.parseObject(body);
+            JSONObject jsonObject1 = new JSONObject();
+            jsonObject1.put("total",jsonObject.get("total"));
+            jsonObject1.put("records",jsonObject.get("results"));
+            jsonObject1.put("size",map.get("pageSize"));
+            log.info("调用AI搜索接口 接口返回------{}", body);
+            return R.data(jsonObject1);
+        } catch (Exception e) {
+            log.error("调用AI搜索接口 接口异常------", e);
+        }
+        return  R.fail("请求失败");
+    }
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+}