forked from iVMiku/guidance-backend
main #3
2
.idea/encodings.xml
generated
2
.idea/encodings.xml
generated
@@ -7,6 +7,8 @@
|
||||
<file url="file://$PROJECT_DIR$/community-8073/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/gateway-8133/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/gateway-8133/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/navigate-8432/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/navigate-8432/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||
<file url="file://$PROJECT_DIR$/user-8072/src/main/java" charset="UTF-8" />
|
||||
|
||||
3
.idea/misc.xml
generated
3
.idea/misc.xml
generated
@@ -5,10 +5,11 @@
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
<option value="$PROJECT_DIR$/navigate-8432/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
@@ -96,7 +96,12 @@
|
||||
<dependency>
|
||||
<groupId>com.tencentcloudapi</groupId>
|
||||
<artifactId>tencentcloud-sdk-java-ocr</artifactId>
|
||||
<version>3.1.1</version>
|
||||
<version>3.1.1076</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.tencentcloudapi</groupId>
|
||||
<artifactId>tencentcloud-sdk-java-tts</artifactId>
|
||||
<version>3.1.1076</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.ivmiku.tutorial.config;
|
||||
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.filter.SaServletFilter;
|
||||
import cn.dev33.satoken.same.SaSameUtil;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* Sa-Token 权限认证 配置类
|
||||
*/
|
||||
@Configuration
|
||||
public class SaTokenConfigure implements WebMvcConfigurer {
|
||||
// 注册 Sa-Token 全局过滤器
|
||||
@Bean
|
||||
public SaServletFilter getSaServletFilter() {
|
||||
return new SaServletFilter()
|
||||
.addInclude("/**")
|
||||
.addExclude("/favicon.ico")
|
||||
.setAuth(obj -> {
|
||||
// 校验 Same-Token 身份凭证 —— 以下两句代码可简化为:SaSameUtil.checkCurrentRequestToken();
|
||||
String token = SaHolder.getRequest().getHeader(SaSameUtil.SAME_TOKEN);
|
||||
SaSameUtil.checkToken(token);
|
||||
})
|
||||
.setError(e -> {
|
||||
return SaResult.error(e.getMessage());
|
||||
})
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.ivmiku.tutorial.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckLogin;
|
||||
import com.ivmiku.tutorial.response.Result;
|
||||
import com.ivmiku.tutorial.service.AssistantService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/assistant")
|
||||
@SaCheckLogin
|
||||
public class AssistantController {
|
||||
@Resource
|
||||
private AssistantService assistantService;
|
||||
|
||||
@GetMapping("/response")
|
||||
public Object getResponse(@RequestParam String input) {
|
||||
Map<String, Object> map = assistantService.getResponse(input);
|
||||
if (map == null) {
|
||||
return Result.error("请求出错");
|
||||
}
|
||||
return Result.ok(map);
|
||||
}
|
||||
|
||||
@GetMapping("/tts")
|
||||
public Object textToSpeech(@RequestParam String input) {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("content", assistantService.textToSpeech(input));
|
||||
return Result.ok(map);
|
||||
}
|
||||
}
|
||||
@@ -34,6 +34,9 @@ public class NavigateController {
|
||||
String userId = (String) StpUtil.getLoginId();
|
||||
if (!origin.isEmpty() && !destination.isEmpty()) {
|
||||
Map<String, Object> route = navigateService.getRoute(origin, destination);
|
||||
if (route == null) {
|
||||
return JSON.toJSON(Result.error("路线规划失败!"));
|
||||
}
|
||||
List<RouteStep> stepsList = (List<RouteStep>) route.get("steps");
|
||||
STEPS_MAP.put(userId, stepsList);
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
@@ -50,13 +53,24 @@ public class NavigateController {
|
||||
map.put("step", STEPS_MAP.get(userId).get(step-1));
|
||||
return JSON.toJSON(Result.ok(map));
|
||||
}
|
||||
return JSON.toJSON(Result.error("非法的请求参数"));
|
||||
return Result.error("非法的请求参数");
|
||||
}
|
||||
|
||||
@PostMapping("/ticket")
|
||||
public Object scanTicket(@RequestPart MultipartFile file) throws IOException {
|
||||
BufferedImage image = ImageIO.read(file.getInputStream());
|
||||
Map<String, Object> map = navigateService.scanTicket(image);
|
||||
return JSON.toJSON(Result.ok(map));
|
||||
return Result.ok(map);
|
||||
}
|
||||
|
||||
@GetMapping("/geocode")
|
||||
public Object getGeoCode(@RequestParam String address, @RequestParam(defaultValue = "") String city) {
|
||||
String geoCode = navigateService.getGeoCode(address, city);
|
||||
if (geoCode == null) {
|
||||
return JSON.toJSON(Result.error("未获取到有效信息"));
|
||||
}
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("geocode", geoCode);
|
||||
return Result.ok(map);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
package com.ivmiku.tutorial.service;
|
||||
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.ivmiku.tutorial.utils.SnowflakeUtil;
|
||||
import com.tencentcloudapi.common.AbstractModel;
|
||||
import com.tencentcloudapi.common.Credential;
|
||||
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
|
||||
import com.tencentcloudapi.common.profile.ClientProfile;
|
||||
import com.tencentcloudapi.common.profile.HttpProfile;
|
||||
import com.tencentcloudapi.tts.v20190823.TtsClient;
|
||||
import com.tencentcloudapi.tts.v20190823.models.TextToVoiceRequest;
|
||||
import com.tencentcloudapi.tts.v20190823.models.TextToVoiceResponse;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class AssistantService {
|
||||
private final String auth = "bad1f6cad39a4c26aa9fe9e4324e096f:ZDczMDIwOTg3NjlhODdmYWVjYTY0YjM1";
|
||||
private final String secretId = "AKID09INNYxYEFFJH3g9VhljVF3qbDiFdx50";
|
||||
private final String secretKey = "KajjcNyNaaUCqQroqpzNoMtTHNj4Lbil";
|
||||
|
||||
public Map<String, Object> getResponse(String userInput) {
|
||||
Map<String, Object> message1 = new HashMap<>();
|
||||
message1.put("role", "system");
|
||||
message1.put("content", "模仿语音助手,对用户的问题给出简短的回答");
|
||||
Map<String, Object> message2 = new HashMap<>();
|
||||
message2.put("role", "user");
|
||||
message2.put("content", userInput);
|
||||
JSONArray array = new JSONArray();
|
||||
array.add(message1);
|
||||
array.add(message2);
|
||||
JSONObject params = new JSONObject();
|
||||
params.put("model", "general");
|
||||
params.put("messages", array);
|
||||
HttpResponse response = HttpRequest.post("https://spark-api-open.xf-yun.com/v1/chat/completions")
|
||||
.header("Content-Type", "application/json")
|
||||
.header("Authorization", "Bearer " + auth)
|
||||
.body(params.toJSONString())
|
||||
.execute();
|
||||
JSONObject result = JSONObject.parseObject(response.body());
|
||||
response.close();
|
||||
if (result.getInteger("code") != 0) {
|
||||
return null;
|
||||
}
|
||||
JSONArray choices = result.getJSONArray("choices");
|
||||
JSONObject message = choices.getJSONObject(0);
|
||||
JSONObject content = message.getJSONObject("message");
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("content", content.get("content"));
|
||||
return map;
|
||||
}
|
||||
|
||||
public String textToSpeech(String content) {
|
||||
try{
|
||||
Credential cred = new Credential(secretId, secretKey);
|
||||
HttpProfile httpProfile = new HttpProfile();
|
||||
httpProfile.setEndpoint("tts.tencentcloudapi.com");
|
||||
ClientProfile clientProfile = new ClientProfile();
|
||||
clientProfile.setHttpProfile(httpProfile);
|
||||
TtsClient client = new TtsClient(cred, "ap-shanghai", clientProfile);
|
||||
TextToVoiceRequest req = new TextToVoiceRequest();
|
||||
req.setText(content);
|
||||
req.setSessionId(SnowflakeUtil.getNext());
|
||||
req.setVoiceType(101006L);
|
||||
TextToVoiceResponse resp = client.TextToVoice(req);
|
||||
return resp.getAudio();
|
||||
} catch (TencentCloudSDKException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,11 @@ public class NavigateService {
|
||||
.execute();
|
||||
String result = response.body();
|
||||
response.close();
|
||||
JSONObject route = JSONObject.parseObject(JSONObject.parseObject(result).getString("route"));
|
||||
JSONObject resultObj = JSONObject.parseObject(result);
|
||||
if (resultObj.getInteger("status")==0) {
|
||||
return null;
|
||||
}
|
||||
JSONObject route = JSONObject.parseObject(resultObj.getString("route"));
|
||||
JSONArray paths = JSONArray.parseArray(route.getString("paths"));
|
||||
JSONObject path = paths.getJSONObject(0);
|
||||
String distance = path.getString("distance");
|
||||
@@ -81,4 +85,25 @@ public class NavigateService {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public String getGeoCode(String address, String city) {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("key", key);
|
||||
params.put("address", address);
|
||||
if (!city.isEmpty()) {
|
||||
params.put("city", city);
|
||||
}
|
||||
HttpResponse response = HttpRequest.get("https://restapi.amap.com/v3/geocode/geo")
|
||||
.form(params)
|
||||
.execute();
|
||||
String result = response.body();
|
||||
response.close();
|
||||
JSONObject resultObj = JSONObject.parseObject(result);
|
||||
if (resultObj.getInteger("status")==0) {
|
||||
return null;
|
||||
}
|
||||
JSONArray array = resultObj.getJSONArray("geocodes");
|
||||
JSONObject location = array.getJSONObject(0);
|
||||
return location.getString("location");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.ivmiku.tutorial.utils;
|
||||
|
||||
import cn.hutool.core.lang.generator.SnowflakeGenerator;
|
||||
|
||||
public class SnowflakeUtil {
|
||||
private static SnowflakeGenerator generator;
|
||||
|
||||
static {
|
||||
generator = new SnowflakeGenerator();
|
||||
}
|
||||
public static String getNext() {
|
||||
return String.valueOf(generator.next());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user