Merge remote-tracking branch 'origin/main'

This commit is contained in:
2025-08-15 15:43:18 +08:00
15 changed files with 521 additions and 110 deletions

View File

@@ -16,6 +16,7 @@ import okhttp3.*;
import com.srs.common.core.domain.AjaxResult; // ✅ RuoYi 的返回结果类
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@@ -38,41 +39,49 @@ import java.util.concurrent.CompletableFuture;
@RestController
@RequestMapping("/aitutor/aichat")
public class AiChatController extends BaseController {
/**
* Dify API的URL地址
* 用于发送聊天消息请求到Dify服务
*/
private static final String DIFY_API_URL = "http://47.112.118.149:8100/v1/chat-messages";
//private static final String DIFY_API_URL = "http://localhost:8080/v1/chat-messages";
/**
* Dify反馈API的基础URL
* 用于提交消息反馈(点赞、点踩等)
*/
private static final String DIFY_FEEDBACK_BASE_URL = "http://47.112.118.149:8100/v1/messages";
//private static final String DIFY_FEEDBACK_BASE_URL = "http://localhost:8080/v1/messages";
private static final String DIFY_API_HISTORY_URL = "http://47.112.118.149:8100/v1/messages";
//private static final String DIFY_API_HISTORY_URL = "http://localhost:8080/v1/messages";
/**
* Dify会话API的基础URL
* 用于获取会话列表
*/
private static final String DIFY_CONVERSATIONS_URL = "http://47.112.118.149:8100/v1/conversations";
//private static final String DIFY_CONVERSATIONS_URL = "http://localhost:8080/v1/conversations";
//文件上传
private static final String DIFY_FILES_URL = "http://47.112.118.149:8100/v1/files/upload";
/**
* Dify API的访问密钥
* 用于身份验证授权访问Dify服务
*/
private static final String DIFY_API_KEY = "app-2wjqcYI9n6igHTVHdH8qXlnh";
//private static final String DIFY_API_KEY = "app-";
/**
* Dify API的URL地址
* 用于发送聊天消息请求到Dify服务
*/
private static final String DIFY_API_URL = "http://47.112.118.149:8100/v1/chat-messages";
/**
* Dify反馈API的基础URL
* 用于提交消息反馈(点赞、点踩等)
*/
private static final String DIFY_FEEDBACK_BASE_URL = "http://47.112.118.149:8100/v1/messages";
/**
* Dify获取反馈API的基础URL
* 用于获取消息反馈(点赞、点踩等)
*/
private static final String DIFY_API_FEEDBACK_URL = "http://47.112.118.149:8100/v1/app/feedbacks?page=";
/**
* Dify消息历史记录API的基础URL
* 用于获取消息历史记录
*/
private static final String DIFY_API_HISTORY_URL = "http://47.112.118.149:8100/v1/messages";
/**
* Dify会话API的基础URL
* 用于获取会话列表
*/
private static final String DIFY_CONVERSATIONS_URL = "http://47.112.118.149:8100/v1/conversations";
/**
* Dify文件上传API的URL地址
* 用于上传文件到Dify服务支持图文多模态理解
*/
private static final String DIFY_FILES_URL = "http://47.112.118.149:8100/v1/files/upload";
/**
* HTTP客户端实例
@@ -386,12 +395,9 @@ public class AiChatController extends BaseController {
// 参数校验和限制
int limitValue = Math.min(Math.max(Integer.parseInt(limit), 1), 100);
// 构建请求URL
String url = "http://47.112.118.149:8100/v1/app/feedbacks?page=" + page + "&limit=" + limitValue;
// 构建请求
Request request = new Request.Builder()
.url(url)
.url(DIFY_API_FEEDBACK_URL + page + "&limit=" + limitValue)
.addHeader("Authorization", "Bearer " + DIFY_API_KEY)
.addHeader("Content-Type", "application/json")
.get()
@@ -420,27 +426,6 @@ public class AiChatController extends BaseController {
feedbackItem.put("app_id", feedbackNode.has("app_id") ? feedbackNode.get("app_id").asText() : null);
feedbackItem.put("conversation_id", feedbackNode.has("conversation_id") ? feedbackNode.get("conversation_id").asText() : null);
// 提取用户信息
if (feedbackNode.has("from_end_user")) {
JsonNode userNode = feedbackNode.get("from_end_user");
Map<String, Object> userMap = new HashMap<>();
userMap.put("id", userNode.has("id") ? userNode.get("id").asText() : null);
userMap.put("name", userNode.has("name") ? userNode.get("name").asText() : null);
userMap.put("email", userNode.has("email") ? userNode.get("email").asText() : null);
feedbackItem.put("from_end_user", userMap);
}
// 提取消息内容
if (feedbackNode.has("message")) {
JsonNode messageNode = feedbackNode.get("message");
Map<String, Object> messageMap = new HashMap<>();
messageMap.put("id", messageNode.has("id") ? messageNode.get("id").asText() : null);
messageMap.put("query", messageNode.has("query") ? messageNode.get("query").asText() : null);
messageMap.put("answer", messageNode.has("answer") ? messageNode.get("answer").asText() : null);
messageMap.put("created_at", messageNode.has("created_at") ? messageNode.get("created_at").asLong() : null);
feedbackItem.put("message", messageMap);
}
feedbackList.add(feedbackItem);
}
}
@@ -476,6 +461,7 @@ public class AiChatController extends BaseController {
}
// 权限标识为辅导员
@PreAuthorize("@ss.hasPermi('cph:teacher:list')")
@GetMapping("/getMessagesToAdmin")
public AjaxResult getMessagesToAdmin(@RequestParam String user,
@RequestParam(required = false) String firstId,

View File

@@ -0,0 +1,129 @@
package com.srs.web.controller.common;
import com.srs.common.core.controller.BaseController;
import com.srs.common.core.domain.AjaxResult;
import com.srs.common.core.domain.entity.SysUser;
import com.srs.common.core.domain.model.LoginUser;
import com.srs.common.utils.WeChatUtil;
import com.srs.framework.web.service.TokenService;
import com.srs.system.domain.StudentMentalRating;
import com.srs.system.mapper.StudentMentalRatingMapper;
import com.srs.system.mapper.SysUserMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
/**
* Dify心理问题发送企业微信 知无涯
*/
@Slf4j
@RestController
@RequestMapping("/api/wechat")
public class WeChatMentalAlertController extends BaseController {
@Autowired
private SysUserMapper sysUserMapper;
@Autowired
private WeChatUtil weChatUtil;
@Autowired
private TokenService tokenService;
@Autowired
private StudentMentalRatingMapper studentMentalRatingMapper;
/**
* 处理心理预警通知请求
*/
@PostMapping("/mentalAlert")
public AjaxResult handleMentalAlert(@RequestBody MentalAlertRequest request,
HttpServletRequest httpRequest) {
// 校验 token 是否有效
LoginUser loginUser = tokenService.getLoginUser(httpRequest);
if (loginUser == null) {
return AjaxResult.error("Token无效或已过期");
}
// 查询辅导员信息
SysUser teacher = sysUserMapper.selectTeacherByStuNo(request.getUserId());
if (teacher == null || !StringUtils.hasText(teacher.getUserName())) {
log.error("辅导员信息不完整,学号: {}", request.getUserId());
return AjaxResult.error("未分配辅导员或信息不完整");
}
/* 保存学生心理问题评级 */
StudentMentalRating record = new StudentMentalRating();
record.setStudentId(request.getUserId());
record.setRating(request.getRating());
studentMentalRatingMapper.insert(record);
// 构建并发送消息
try {
String content = buildContent(request, teacher);
weChatUtil.sendTextMessage(teacher.getUserName(), content);
return AjaxResult.success("消息已发送至辅导员");
} catch (Exception e) {
log.error("发送企业微信失败", e);
return AjaxResult.error("发送失败: " + e.getMessage());
}
}
/**
* 构建企业微信消息内容
*/
private String buildContent(MentalAlertRequest request, SysUser teacher) {
String teacherName = StringUtils.hasText(teacher.getNickName())
? teacher.getNickName()
: teacher.getUserName();
return String.format(
"【心理预警通知】\n" +
"辅导员:%s%s\n" +
"学生姓名:%s\n" +
"学号:%s\n" +
"问题描述:%s\n" +
"AI建议%s\n" +
"心理问题评级:%s",
teacherName,
teacher.getUserName(),
request.getUserName(),
request.getUserId(),
request.getStudentQuestion(),
request.getAiAnswer(),
request.getRating()
);
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class MentalAlertRequest {
private String userId; // 学生学号
private String userName; // 学生姓名
private String studentQuestion; // 学生提问内容
private String aiAnswer; // AI回复内容
private String rating; // 心理问题评级
}
/**
* 获取全部学生心理评级记录
*/
@GetMapping("/rating/all")
public AjaxResult allRatings() {
return AjaxResult.success(studentMentalRatingMapper.selectAll());
}
/**
* 根据学号获取全部记录
*/
@GetMapping("/rating/{stuNo}")
public AjaxResult listByStuNo(@PathVariable String stuNo) {
return AjaxResult.success(studentMentalRatingMapper.selectByStuNo(stuNo));
}
}

View File

@@ -284,4 +284,11 @@ public class CphAuditDetailsController extends BaseController {
public AjaxResult remove(@PathVariable Long[] ids) {
return toAjax(cphAuditDetailsService.deleteCphAuditDetailsByIds(ids));
}
/**
* 撤销审核
*/
@PostMapping("/cancelAudit/{ids}")
public AjaxResult cancelAudit(@PathVariable Long[] ids) {
return toAjax(cphAuditDetailsService.cancelAuditByIds(ids));
}
}