ai-robot-channel/src/main/java/com/wecom/robot/controller/DebugController.java

142 lines
5.0 KiB
Java
Raw Normal View History

2026-02-23 01:45:23 +00:00
package com.wecom.robot.controller;
import com.wecom.robot.config.WecomConfig;
import com.wecom.robot.dto.ApiResponse;
import com.wecom.robot.entity.Message;
import com.wecom.robot.service.SessionManagerService;
import com.wecom.robot.util.WXBizMsgCrypt;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/debug")
@RequiredArgsConstructor
public class DebugController {
private final WecomConfig wecomConfig;
private final SessionManagerService sessionManagerService;
@GetMapping("/config")
public ApiResponse<Map<String, Object>> getConfig() {
Map<String, Object> config = new HashMap<>();
config.put("corpId", wecomConfig.getCorpId());
config.put("token", wecomConfig.getToken());
config.put("encodingAesKey", wecomConfig.getEncodingAesKey());
config.put("encodingAesKeyLength", wecomConfig.getEncodingAesKey() != null ? wecomConfig.getEncodingAesKey().length() : 0);
try {
byte[] aesKey = Base64.getDecoder().decode(wecomConfig.getEncodingAesKey() + "=");
config.put("aesKeyLength", aesKey.length);
config.put("aesKeyHex", bytesToHex(aesKey));
} catch (Exception e) {
config.put("aesKeyError", e.getMessage());
}
return ApiResponse.success(config);
}
@PostMapping("/decrypt")
public ApiResponse<Map<String, Object>> testDecrypt(
@RequestParam String msgSignature,
@RequestParam String timestamp,
@RequestParam String nonce,
@RequestBody String encryptedXml) {
Map<String, Object> result = new HashMap<>();
result.put("msgSignature", msgSignature);
result.put("timestamp", timestamp);
result.put("nonce", nonce);
try {
WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(
wecomConfig.getToken(),
wecomConfig.getEncodingAesKey(),
wecomConfig.getCorpId()
);
String decrypted = wxcpt.DecryptMsg(msgSignature, timestamp, nonce, encryptedXml);
result.put("decrypted", decrypted);
result.put("success", true);
} catch (Exception e) {
result.put("error", e.getMessage());
result.put("success", false);
log.error("解密测试失败", e);
}
return ApiResponse.success(result);
}
@GetMapping("/verify-url")
public ApiResponse<Map<String, Object>> testVerifyUrl(
@RequestParam String msgSignature,
@RequestParam String timestamp,
@RequestParam String nonce,
@RequestParam String echostr) {
Map<String, Object> result = new HashMap<>();
result.put("msgSignature", msgSignature);
result.put("timestamp", timestamp);
result.put("nonce", nonce);
result.put("echostr", echostr);
try {
WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(
wecomConfig.getToken(),
wecomConfig.getEncodingAesKey(),
wecomConfig.getCorpId()
);
String decrypted = wxcpt.VerifyURL(msgSignature, timestamp, nonce, echostr);
result.put("decrypted", decrypted);
result.put("success", true);
} catch (Exception e) {
result.put("error", e.getMessage());
result.put("success", false);
log.error("URL验证测试失败", e);
}
return ApiResponse.success(result);
}
@GetMapping("/ai/session/{sessionId}/context")
public ApiResponse<Map<String, Object>> getSessionAiContext(
@PathVariable String sessionId) {
2026-02-23 01:45:23 +00:00
Map<String, Object> result = new HashMap<>();
result.put("sessionId", sessionId);
List<Message> history = sessionManagerService.getSessionMessages(sessionId);
result.put("historyCount", history.size());
List<Map<String, Object>> historyList = new java.util.ArrayList<>();
for (Message msg : history) {
Map<String, Object> msgMap = new HashMap<>();
msgMap.put("senderType", msg.getSenderType());
msgMap.put("senderId", msg.getSenderId());
msgMap.put("content", msg.getContent());
msgMap.put("msgType", msg.getMsgType());
msgMap.put("createdAt", msg.getCreatedAt() != null ? msg.getCreatedAt().toString() : null);
historyList.add(msgMap);
}
result.put("history", historyList);
return ApiResponse.success(result);
}
private String bytesToHex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(String.format("%02x", b));
}
return sb.toString();
}
}