diff --git a/.idea/encodings.xml b/.idea/encodings.xml index c54f389..b1117c5 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -7,6 +7,8 @@ + + diff --git a/.idea/misc.xml b/.idea/misc.xml index bada8b5..c4e5a98 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -5,10 +5,11 @@ - + \ No newline at end of file diff --git a/navigate-8432/pom.xml b/navigate-8432/pom.xml index aab061b..97f02c0 100644 --- a/navigate-8432/pom.xml +++ b/navigate-8432/pom.xml @@ -96,7 +96,12 @@ com.tencentcloudapi tencentcloud-sdk-java-ocr - 3.1.1 + 3.1.1076 + + + com.tencentcloudapi + tencentcloud-sdk-java-tts + 3.1.1076 \ No newline at end of file diff --git a/navigate-8432/src/main/java/com/ivmiku/tutorial/config/SaTokenConfigure.java b/navigate-8432/src/main/java/com/ivmiku/tutorial/config/SaTokenConfigure.java new file mode 100644 index 0000000..3fa34d7 --- /dev/null +++ b/navigate-8432/src/main/java/com/ivmiku/tutorial/config/SaTokenConfigure.java @@ -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()); + }) + ; + } +} + diff --git a/navigate-8432/src/main/java/com/ivmiku/tutorial/controller/AssistantController.java b/navigate-8432/src/main/java/com/ivmiku/tutorial/controller/AssistantController.java new file mode 100644 index 0000000..afef15d --- /dev/null +++ b/navigate-8432/src/main/java/com/ivmiku/tutorial/controller/AssistantController.java @@ -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 map = assistantService.getResponse(input); + if (map == null) { + return Result.error("请求出错"); + } + return Result.ok(map); + } + + @GetMapping("/tts") + public Object textToSpeech(@RequestParam String input) { + Map map = new HashMap<>(); + map.put("content", assistantService.textToSpeech(input)); + return Result.ok(map); + } +} diff --git a/navigate-8432/src/main/java/com/ivmiku/tutorial/controller/NavigateController.java b/navigate-8432/src/main/java/com/ivmiku/tutorial/controller/NavigateController.java index 927d070..924e7e5 100644 --- a/navigate-8432/src/main/java/com/ivmiku/tutorial/controller/NavigateController.java +++ b/navigate-8432/src/main/java/com/ivmiku/tutorial/controller/NavigateController.java @@ -34,6 +34,9 @@ public class NavigateController { String userId = (String) StpUtil.getLoginId(); if (!origin.isEmpty() && !destination.isEmpty()) { Map route = navigateService.getRoute(origin, destination); + if (route == null) { + return JSON.toJSON(Result.error("路线规划失败!")); + } List stepsList = (List) route.get("steps"); STEPS_MAP.put(userId, stepsList); Map 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 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 map = new HashMap<>(); + map.put("geocode", geoCode); + return Result.ok(map); } } diff --git a/navigate-8432/src/main/java/com/ivmiku/tutorial/service/AssistantService.java b/navigate-8432/src/main/java/com/ivmiku/tutorial/service/AssistantService.java new file mode 100644 index 0000000..f060c58 --- /dev/null +++ b/navigate-8432/src/main/java/com/ivmiku/tutorial/service/AssistantService.java @@ -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 getResponse(String userInput) { + Map message1 = new HashMap<>(); + message1.put("role", "system"); + message1.put("content", "模仿语音助手,对用户的问题给出简短的回答"); + Map 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 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); + } + } +} diff --git a/navigate-8432/src/main/java/com/ivmiku/tutorial/service/NavigateService.java b/navigate-8432/src/main/java/com/ivmiku/tutorial/service/NavigateService.java index 32ba49d..c942e1f 100644 --- a/navigate-8432/src/main/java/com/ivmiku/tutorial/service/NavigateService.java +++ b/navigate-8432/src/main/java/com/ivmiku/tutorial/service/NavigateService.java @@ -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 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"); + } } diff --git a/navigate-8432/src/main/java/com/ivmiku/tutorial/utils/SnowflakeUtil.java b/navigate-8432/src/main/java/com/ivmiku/tutorial/utils/SnowflakeUtil.java new file mode 100644 index 0000000..7d30793 --- /dev/null +++ b/navigate-8432/src/main/java/com/ivmiku/tutorial/utils/SnowflakeUtil.java @@ -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()); + } +}