refactor(dify模块): 对dify模块的apikey进行了Redis缓存并对dify访问的userid进行了自动获取
This commit is contained in:
parent
a4ebc7875c
commit
1dfc8ea017
@ -2,10 +2,12 @@ package com.zsc.edu.dify;
|
|||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.cache.annotation.EnableCaching;
|
||||||
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
@EnableAspectJAutoProxy
|
@EnableAspectJAutoProxy
|
||||||
|
@EnableCaching
|
||||||
public class DifyBackendApplication {
|
public class DifyBackendApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
package com.zsc.edu.dify.modules.dify.controller;
|
package com.zsc.edu.dify.modules.dify.controller;
|
||||||
|
|
||||||
|
import com.zsc.edu.dify.exception.ExceptionUtil;
|
||||||
|
import com.zsc.edu.dify.framework.security.SecurityUtil;
|
||||||
import com.zsc.edu.dify.modules.dify.service.AppEntityService;
|
import com.zsc.edu.dify.modules.dify.service.AppEntityService;
|
||||||
import com.zsc.edu.dify.modules.operationLog.entity.OperationLogAnnotation;
|
import com.zsc.edu.dify.modules.operationLog.entity.OperationLogAnnotation;
|
||||||
|
import com.zsc.edu.dify.modules.system.service.UserService;
|
||||||
import io.github.guoshiqiufeng.dify.chat.DifyChat;
|
import io.github.guoshiqiufeng.dify.chat.DifyChat;
|
||||||
import io.github.guoshiqiufeng.dify.chat.dto.request.ChatMessageSendRequest;
|
import io.github.guoshiqiufeng.dify.chat.dto.request.ChatMessageSendRequest;
|
||||||
import io.github.guoshiqiufeng.dify.chat.dto.request.MessageConversationsRequest;
|
import io.github.guoshiqiufeng.dify.chat.dto.request.MessageConversationsRequest;
|
||||||
@ -43,8 +46,9 @@ public class V1ChatController {
|
|||||||
@RequestBody ChatMessageSendRequest sendRequest,
|
@RequestBody ChatMessageSendRequest sendRequest,
|
||||||
@PathVariable String appId
|
@PathVariable String appId
|
||||||
) {
|
) {
|
||||||
sendRequest.setApiKey(appEntityService.getApikeyByRedis(appId));
|
sendRequest.setApiKey(appEntityService.getApikey(appId));
|
||||||
return difyChat.send(sendRequest);
|
sendRequest.setUserId(SecurityUtil.getUserInfo().id.toString());
|
||||||
|
return ExceptionUtil.difyException(()->difyChat.send(sendRequest));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -61,7 +65,7 @@ public class V1ChatController {
|
|||||||
@RequestBody ChatMessageSendRequest sendRequest,
|
@RequestBody ChatMessageSendRequest sendRequest,
|
||||||
@PathVariable String appid
|
@PathVariable String appid
|
||||||
) {
|
) {
|
||||||
sendRequest.setApiKey(appEntityService.getApikeyByRedis(appid));
|
sendRequest.setApiKey(appEntityService.getApikey(appid));
|
||||||
return difyChat.sendChatMessageStream(sendRequest);
|
return difyChat.sendChatMessageStream(sendRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +81,8 @@ public class V1ChatController {
|
|||||||
@RequestBody MessageConversationsRequest request,
|
@RequestBody MessageConversationsRequest request,
|
||||||
@PathVariable String appId
|
@PathVariable String appId
|
||||||
) {
|
) {
|
||||||
request.setApiKey(appEntityService.getApikeyByRedis(appId));
|
request.setApiKey(appEntityService.getApikey(appId));
|
||||||
|
request.setUserId(SecurityUtil.getUserInfo().id.toString());
|
||||||
return difyChat.conversations(request);
|
return difyChat.conversations(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,11 +90,11 @@ public class V1ChatController {
|
|||||||
* 停止流式消息
|
* 停止流式消息
|
||||||
*
|
*
|
||||||
* @param taskId 任务id
|
* @param taskId 任务id
|
||||||
* @param userId 用户id
|
|
||||||
*/
|
*/
|
||||||
@PatchMapping("/stream/stop")
|
@PatchMapping("/stream/stop")
|
||||||
public void stopMessagesStream(@RequestParam String taskId, @RequestParam String userId, @RequestParam String appId) {
|
public void stopMessagesStream(@RequestParam String taskId, @RequestParam String appId) {
|
||||||
String apiKey = appEntityService.getApikeyByRedis(appId);
|
String apiKey = appEntityService.getApikey(appId);
|
||||||
|
String userId = SecurityUtil.getUserInfo().id.toString();
|
||||||
difyChat.stopMessagesStream(apiKey, taskId,userId);
|
difyChat.stopMessagesStream(apiKey, taskId,userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,13 +102,13 @@ public class V1ChatController {
|
|||||||
* 删除会话
|
* 删除会话
|
||||||
*
|
*
|
||||||
* @param conversationId 会话id
|
* @param conversationId 会话id
|
||||||
* @param userId 用户id
|
|
||||||
*/
|
*/
|
||||||
@DeleteMapping("/conversation")
|
@DeleteMapping("/conversation")
|
||||||
@PreAuthorize("hasAuthority('dify:chat:delete')")
|
@PreAuthorize("hasAuthority('dify:chat:delete')")
|
||||||
@OperationLogAnnotation(content = "'dify对话", operationType = "删除")
|
@OperationLogAnnotation(content = "'dify对话", operationType = "删除")
|
||||||
public void deleteConversation(@RequestParam String conversationId, @RequestParam String userId, @RequestParam String appId) {
|
public void deleteConversation(@RequestParam String conversationId, @RequestParam String appId) {
|
||||||
String apiKey = appEntityService.getApikeyByRedis(appId);
|
String apiKey = appEntityService.getApikey(appId);
|
||||||
|
String userId = SecurityUtil.getUserInfo().id.toString();
|
||||||
difyChat.deleteConversation(conversationId, apiKey,userId);
|
difyChat.deleteConversation(conversationId, apiKey,userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package com.zsc.edu.dify.modules.dify.controller;
|
package com.zsc.edu.dify.modules.dify.controller;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
import com.zsc.edu.dify.framework.security.SecurityUtil;
|
||||||
import com.zsc.edu.dify.modules.dify.entity.WorkflowData;
|
import com.zsc.edu.dify.modules.dify.entity.WorkflowData;
|
||||||
import com.zsc.edu.dify.modules.dify.repo.AppEntityRepository;
|
|
||||||
import com.zsc.edu.dify.modules.dify.service.AppEntityService;
|
import com.zsc.edu.dify.modules.dify.service.AppEntityService;
|
||||||
import com.zsc.edu.dify.modules.dify.service.DifyWorkflowService;
|
import com.zsc.edu.dify.modules.dify.service.DifyWorkflowService;
|
||||||
import com.zsc.edu.dify.modules.operationLog.entity.OperationLogAnnotation;
|
import com.zsc.edu.dify.modules.operationLog.entity.OperationLogAnnotation;
|
||||||
@ -41,6 +41,7 @@ public class V1WorkflowController {
|
|||||||
@PreAuthorize("hasAuthority('dify:workflow:run')")
|
@PreAuthorize("hasAuthority('dify:workflow:run')")
|
||||||
@OperationLogAnnotation(content = "'dify工作流'", operationType = "运行")
|
@OperationLogAnnotation(content = "'dify工作流'", operationType = "运行")
|
||||||
public WorkflowRunResponse runWorkflow(@RequestBody WorkflowRunRequest request, @PathVariable String appId) {
|
public WorkflowRunResponse runWorkflow(@RequestBody WorkflowRunRequest request, @PathVariable String appId) {
|
||||||
|
request.setUserId(SecurityUtil.getUserInfo().id.toString());
|
||||||
return difyWorkflowService.run(request, appId);
|
return difyWorkflowService.run(request, appId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,12 +60,12 @@ public class V1WorkflowController {
|
|||||||
* 停止工作流(流式)
|
* 停止工作流(流式)
|
||||||
*
|
*
|
||||||
* @param taskId
|
* @param taskId
|
||||||
* @param userId
|
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@PatchMapping("/stop/{appId}")
|
@PatchMapping("/stop/{appId}")
|
||||||
public WorkflowStopResponse stopWorkflowStream(String taskId, String userId, @PathVariable String appId) {
|
public WorkflowStopResponse stopWorkflowStream(String taskId, @PathVariable String appId) {
|
||||||
String apiKey =appEntityService.getApikeyByRedis(appId);
|
String apiKey =appEntityService.getApikey(appId);
|
||||||
|
String userId = SecurityUtil.getUserInfo().id.toString();
|
||||||
return difyWorkflow.stopWorkflowStream(apiKey, taskId,userId);
|
return difyWorkflow.stopWorkflowStream(apiKey, taskId,userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -77,7 +78,7 @@ public class V1WorkflowController {
|
|||||||
@GetMapping("/info/{appId}")
|
@GetMapping("/info/{appId}")
|
||||||
@PreAuthorize("hasAuthority('dify:workflow:info')")
|
@PreAuthorize("hasAuthority('dify:workflow:info')")
|
||||||
public WorkflowInfoResponse info(String workflowRunId, @PathVariable String appId) {
|
public WorkflowInfoResponse info(String workflowRunId, @PathVariable String appId) {
|
||||||
String apiKey =appEntityService.getApikeyByRedis(appId);
|
String apiKey =appEntityService.getApikey(appId);
|
||||||
return difyWorkflow.info(workflowRunId, apiKey);
|
return difyWorkflow.info(workflowRunId, apiKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,7 +91,7 @@ public class V1WorkflowController {
|
|||||||
@PostMapping("/logs/{appId}")
|
@PostMapping("/logs/{appId}")
|
||||||
@PreAuthorize("hasAuthority('dify:workflow:log')")
|
@PreAuthorize("hasAuthority('dify:workflow:log')")
|
||||||
public DifyPageResult<WorkflowLogs> logs(@RequestBody WorkflowLogsRequest request, @PathVariable String appId) {
|
public DifyPageResult<WorkflowLogs> logs(@RequestBody WorkflowLogsRequest request, @PathVariable String appId) {
|
||||||
String apiKey = appEntityService.getApikeyByRedis(appId);
|
String apiKey = appEntityService.getApikey(appId);
|
||||||
request.setApiKey(apiKey);
|
request.setApiKey(apiKey);
|
||||||
final DifyPageResult<WorkflowLogs> logs = difyWorkflow.logs(request);
|
final DifyPageResult<WorkflowLogs> logs = difyWorkflow.logs(request);
|
||||||
return logs;
|
return logs;
|
||||||
|
@ -11,5 +11,5 @@ public interface AppEntityService extends IService<AppEntity> {
|
|||||||
|
|
||||||
boolean enabledApp(String id);
|
boolean enabledApp(String id);
|
||||||
|
|
||||||
String getApikeyByRedis(String appId);
|
String getApikey(String appId);
|
||||||
}
|
}
|
||||||
|
@ -35,6 +35,11 @@ public class AppEntityServiceImpl extends ServiceImpl<AppEntityRepository, AppEn
|
|||||||
return addApps(difyServer.apps(mode, name));
|
return addApps(difyServer.apps(mode, name));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加应用到数据库中
|
||||||
|
* @param appsResponseList
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
public List<AppsResponseVO> addApps(List<AppsResponseVO> appsResponseList) {
|
public List<AppsResponseVO> addApps(List<AppsResponseVO> appsResponseList) {
|
||||||
for(AppsResponseVO appsResponse : appsResponseList){
|
for(AppsResponseVO appsResponse : appsResponseList){
|
||||||
boolean isHave = baseMapper.exists(new QueryWrapper<AppEntity>().eq("id", appsResponse.getId()));
|
boolean isHave = baseMapper.exists(new QueryWrapper<AppEntity>().eq("id", appsResponse.getId()));
|
||||||
@ -58,6 +63,11 @@ public class AppEntityServiceImpl extends ServiceImpl<AppEntityRepository, AppEn
|
|||||||
return appsResponseList;
|
return appsResponseList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 启用禁用应用
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean enabledApp(String id) {
|
public boolean enabledApp(String id) {
|
||||||
AppEntity appEntity = baseMapper.selectById(id);
|
AppEntity appEntity = baseMapper.selectById(id);
|
||||||
@ -69,9 +79,15 @@ public class AppEntityServiceImpl extends ServiceImpl<AppEntityRepository, AppEn
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据appId获取apikey并缓存至redis
|
||||||
|
* @param appId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
@Cacheable(cacheNames="apikey",key="#apikey")
|
@Cacheable(cacheNames="dify:flow:apikey",key="#appId")
|
||||||
public String getApikeyByRedis(String appId){
|
public String getApikey(String appId){
|
||||||
return appEntityRepository.selectApiKey(appId);
|
return appEntityRepository.selectApiKey(appId);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,8 @@ package com.zsc.edu.dify.modules.dify.service.Impl;
|
|||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.zsc.edu.dify.modules.dify.entity.WorkflowData;
|
import com.zsc.edu.dify.modules.dify.entity.WorkflowData;
|
||||||
import com.zsc.edu.dify.modules.dify.mapper.WorkflowMapper;
|
import com.zsc.edu.dify.modules.dify.mapper.WorkflowMapper;
|
||||||
import com.zsc.edu.dify.modules.dify.repo.AppEntityRepository;
|
|
||||||
import com.zsc.edu.dify.modules.dify.repo.WorkflowRepository;
|
import com.zsc.edu.dify.modules.dify.repo.WorkflowRepository;
|
||||||
|
import com.zsc.edu.dify.modules.dify.service.AppEntityService;
|
||||||
import com.zsc.edu.dify.modules.dify.service.DifyWorkflowService;
|
import com.zsc.edu.dify.modules.dify.service.DifyWorkflowService;
|
||||||
import io.github.guoshiqiufeng.dify.workflow.DifyWorkflow;
|
import io.github.guoshiqiufeng.dify.workflow.DifyWorkflow;
|
||||||
import io.github.guoshiqiufeng.dify.workflow.dto.request.WorkflowRunRequest;
|
import io.github.guoshiqiufeng.dify.workflow.dto.request.WorkflowRunRequest;
|
||||||
@ -17,15 +17,21 @@ import org.springframework.stereotype.Service;
|
|||||||
@Service
|
@Service
|
||||||
public class DifyWorkflowServiceImpl extends ServiceImpl<WorkflowRepository, WorkflowData> implements DifyWorkflowService {
|
public class DifyWorkflowServiceImpl extends ServiceImpl<WorkflowRepository, WorkflowData> implements DifyWorkflowService {
|
||||||
@Resource
|
@Resource
|
||||||
AppEntityRepository appEntityRepository;
|
AppEntityService appEntityService;
|
||||||
@Resource
|
@Resource
|
||||||
DifyWorkflow difyWorkflow;
|
DifyWorkflow difyWorkflow;
|
||||||
@Resource
|
@Resource
|
||||||
WorkflowMapper mapper;
|
WorkflowMapper mapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 运行保存信息
|
||||||
|
* @param request
|
||||||
|
* @param appId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public WorkflowRunResponse run(WorkflowRunRequest request, String appId){
|
public WorkflowRunResponse run(WorkflowRunRequest request, String appId){
|
||||||
String apiKey = appEntityRepository.selectApiKey(appId);
|
String apiKey = appEntityService.getApikey(appId);
|
||||||
request.setApiKey(apiKey);
|
request.setApiKey(apiKey);
|
||||||
WorkflowRunResponse response = difyWorkflow.runWorkflow(request);
|
WorkflowRunResponse response = difyWorkflow.runWorkflow(request);
|
||||||
WorkflowData workflowData =mapper.toEntity(response);
|
WorkflowData workflowData =mapper.toEntity(response);
|
||||||
|
@ -4,6 +4,7 @@ import com.zsc.edu.dify.framework.security.UserDetailsImpl;
|
|||||||
import com.zsc.edu.dify.modules.system.dto.*;
|
import com.zsc.edu.dify.modules.system.dto.*;
|
||||||
import com.zsc.edu.dify.modules.system.entity.User;
|
import com.zsc.edu.dify.modules.system.entity.User;
|
||||||
import com.baomidou.mybatisplus.extension.service.IService;
|
import com.baomidou.mybatisplus.extension.service.IService;
|
||||||
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>
|
* <p>
|
||||||
|
@ -14,8 +14,11 @@ import com.zsc.edu.dify.modules.system.mapper.UserMapper;
|
|||||||
import com.zsc.edu.dify.modules.system.repo.*;
|
import com.zsc.edu.dify.modules.system.repo.*;
|
||||||
import com.zsc.edu.dify.modules.system.service.UserService;
|
import com.zsc.edu.dify.modules.system.service.UserService;
|
||||||
import com.zsc.edu.dify.modules.system.utils.sendMail;
|
import com.zsc.edu.dify.modules.system.utils.sendMail;
|
||||||
|
import com.zsc.edu.dify.modules.system.vo.UserDetail;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.BeanUtils;
|
||||||
|
import org.springframework.cache.annotation.Cacheable;
|
||||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
@ -169,4 +172,5 @@ public class UserServiceImpl extends ServiceImpl<UserRepository, User> implement
|
|||||||
userRolesRepository.insert(userRoles);
|
userRolesRepository.insert(userRoles);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user