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> getConfig() { Map 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> testDecrypt( @RequestParam String msgSignature, @RequestParam String timestamp, @RequestParam String nonce, @RequestBody String encryptedXml) { Map 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> testVerifyUrl( @RequestParam String msgSignature, @RequestParam String timestamp, @RequestParam String nonce, @RequestParam String echostr) { Map 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> getSessionAiContext( @PathVariable String sessionId) { Map result = new HashMap<>(); result.put("sessionId", sessionId); List history = sessionManagerService.getSessionMessages(sessionId); result.put("historyCount", history.size()); List> historyList = new java.util.ArrayList<>(); for (Message msg : history) { Map 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(); } }