Merge remote-tracking branch 'origin/main'
This commit is contained in:
@@ -9,6 +9,8 @@ import com.srs.common.core.controller.BaseController;
|
||||
// OkHttp 显式导入
|
||||
import com.srs.common.exception.ServiceException;
|
||||
import com.srs.common.utils.SecurityUtils;
|
||||
import com.srs.common.utils.ServletUtils;
|
||||
import com.srs.framework.web.service.TokenService;
|
||||
import com.srs.teacher.domain.dto.ConversationDTO;
|
||||
import com.srs.web.core.config.DifyConfig;
|
||||
import okhttp3.*;
|
||||
@@ -42,7 +44,8 @@ import java.util.concurrent.CompletableFuture;
|
||||
@RestController
|
||||
@RequestMapping("/aitutor/aichat")
|
||||
public class AiChatController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private TokenService tokenService;
|
||||
/**
|
||||
* HTTP客户端实例
|
||||
* 配置了5分钟的读取超时时间,用于与Dify API进行通信
|
||||
@@ -117,11 +120,13 @@ public class AiChatController extends BaseController {
|
||||
|
||||
// 在主线程中获取当前用户名,避免在异步线程中获取安全上下文
|
||||
String currentUsername = SecurityUtils.getLoginUser().getUsername();
|
||||
|
||||
// 获取JWT token
|
||||
String currentUserToken = tokenService.getToken(ServletUtils.getRequest());
|
||||
// String currentUserToken = SecurityUtils.getLoginUser().getToken();
|
||||
// 异步执行请求处理,避免阻塞主线程
|
||||
CompletableFuture.runAsync(() -> {
|
||||
try {
|
||||
sendToDifyAndStream(requestData, emitter, currentUsername);
|
||||
sendToDifyAndStream(requestData, emitter, currentUsername, currentUserToken);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
try {
|
||||
@@ -147,7 +152,8 @@ public class AiChatController extends BaseController {
|
||||
* @param currentUsername
|
||||
* @throws IOException 当网络请求或IO操作失败时抛出
|
||||
*/
|
||||
private void sendToDifyAndStream(Map<String, Object> requestData, SseEmitter emitter, String currentUsername)
|
||||
private void sendToDifyAndStream(Map<String, Object> requestData, SseEmitter emitter, String currentUsername,
|
||||
String currentUserToken)
|
||||
throws IOException {
|
||||
|
||||
// 构建请求体参数
|
||||
@@ -184,9 +190,9 @@ public class AiChatController extends BaseController {
|
||||
inputs.put("user_name", userName);
|
||||
}
|
||||
|
||||
Object userToken = requestData.get("user_token");
|
||||
if (userToken != null) {
|
||||
inputs.put("user_token", userToken);
|
||||
// Object userToken = requestData.get("user_token");
|
||||
if (currentUserToken != null) {
|
||||
inputs.put("user_token", currentUserToken);
|
||||
}
|
||||
|
||||
Object userRole = requestData.get("user_role");
|
||||
@@ -459,9 +465,9 @@ public class AiChatController extends BaseController {
|
||||
@PreAuthorize("@ss.hasPermi('cph:teacher:list')")
|
||||
@GetMapping("/getMessagesToAdmin")
|
||||
public AjaxResult getMessagesToAdmin(@RequestParam String user,
|
||||
@RequestParam(required = false) String firstId,
|
||||
@RequestParam(defaultValue = "20") int limit,
|
||||
@RequestParam(defaultValue = "-updated_at") String sortBy) {
|
||||
@RequestParam(required = false) String firstId,
|
||||
@RequestParam(defaultValue = "20") int limit,
|
||||
@RequestParam(defaultValue = "-updated_at") String sortBy) {
|
||||
|
||||
try {
|
||||
// 首先尝试从Redis缓存中获取会话ID
|
||||
@@ -497,8 +503,8 @@ public class AiChatController extends BaseController {
|
||||
// 权限标识为学生
|
||||
@GetMapping("/getMessagesToUser")
|
||||
public AjaxResult getMessagesToUser(@RequestParam(required = false) String firstId,
|
||||
@RequestParam(defaultValue = "20") int limit,
|
||||
@RequestParam(defaultValue = "-updated_at") String sortBy) {
|
||||
@RequestParam(defaultValue = "20") int limit,
|
||||
@RequestParam(defaultValue = "-updated_at") String sortBy) {
|
||||
|
||||
try {
|
||||
String user = SecurityUtils.getLoginUser().getUsername();
|
||||
@@ -561,7 +567,7 @@ public class AiChatController extends BaseController {
|
||||
/**
|
||||
* 将会话ID与用户绑定并存储到Redis中
|
||||
*
|
||||
* @param user 用户名
|
||||
* @param user 用户名
|
||||
* @param conversationId 会话ID
|
||||
*/
|
||||
private void cacheConversationId(String user, String conversationId) {
|
||||
|
@@ -209,7 +209,7 @@ public class TokenService
|
||||
* @param request
|
||||
* @return token
|
||||
*/
|
||||
private String getToken(HttpServletRequest request)
|
||||
public String getToken(HttpServletRequest request)
|
||||
{
|
||||
String token = request.getHeader(header);
|
||||
if (StringUtils.isNotEmpty(token) && token.startsWith(Constants.TOKEN_PREFIX))
|
||||
|
Reference in New Issue
Block a user