This commit is contained in:
2025-11-06 13:10:09 +08:00
parent 88234c13b2
commit b4b8f03a4f
2 changed files with 297 additions and 19 deletions

View File

@@ -0,0 +1,250 @@
package com.ruoyi.web.controller.jarvis;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jarvis.config.TencentDocConfig;
import com.ruoyi.jarvis.service.ITencentDocService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.TimeUnit;
/**
* 腾讯文档配置管理Controller
* 用于动态配置H-TF订单自动写入腾讯文档的相关参数
*
* @author system
*/
@RestController
@RequestMapping("/jarvis/tencentDoc/config")
public class TencentDocConfigController extends BaseController {
private static final Logger log = LoggerFactory.getLogger(TencentDocConfigController.class);
@Autowired
private TencentDocConfig tencentDocConfig;
@Autowired
private ITencentDocService tencentDocService;
@Autowired
private RedisCache redisCache;
// Redis key前缀
private static final String REDIS_KEY_PREFIX = "tencent:doc:auto:config:";
/**
* 获取当前配置
*/
@GetMapping
public AjaxResult getConfig() {
try {
JSONObject config = new JSONObject();
// 从Redis获取配置如果存在
String accessToken = redisCache.getCacheObject(REDIS_KEY_PREFIX + "accessToken");
String fileId = redisCache.getCacheObject(REDIS_KEY_PREFIX + "fileId");
String sheetId = redisCache.getCacheObject(REDIS_KEY_PREFIX + "sheetId");
// 如果Redis中没有则使用配置文件中的默认值
if (accessToken == null || accessToken.isEmpty()) {
accessToken = tencentDocConfig.getAccessToken();
}
if (fileId == null || fileId.isEmpty()) {
fileId = tencentDocConfig.getFileId();
}
if (sheetId == null || sheetId.isEmpty()) {
sheetId = tencentDocConfig.getSheetId();
}
config.put("accessToken", maskSensitiveInfo(accessToken)); // 脱敏显示
config.put("fileId", fileId);
config.put("sheetId", sheetId);
config.put("appId", tencentDocConfig.getAppId());
config.put("apiBaseUrl", tencentDocConfig.getApiBaseUrl());
// 检查配置是否完整
boolean isComplete = accessToken != null && !accessToken.isEmpty() &&
fileId != null && !fileId.isEmpty() &&
sheetId != null && !sheetId.isEmpty();
config.put("isConfigured", isComplete);
return AjaxResult.success("获取配置成功", config);
} catch (Exception e) {
log.error("获取腾讯文档配置失败", e);
return AjaxResult.error("获取配置失败: " + e.getMessage());
}
}
/**
* 更新配置保存到Redis180天有效期
*
* @param params 包含 accessToken, fileId, sheetId
*/
@Log(title = "腾讯文档配置", businessType = BusinessType.UPDATE)
@PostMapping
public AjaxResult updateConfig(@RequestBody JSONObject params) {
try {
String accessToken = params.getString("accessToken");
String fileId = params.getString("fileId");
String sheetId = params.getString("sheetId");
// 验证必填字段
if (accessToken == null || accessToken.trim().isEmpty()) {
return AjaxResult.error("访问令牌不能为空");
}
if (fileId == null || fileId.trim().isEmpty()) {
return AjaxResult.error("文件ID不能为空");
}
if (sheetId == null || sheetId.trim().isEmpty()) {
return AjaxResult.error("工作表ID不能为空");
}
// 保存到Redis180天有效期
redisCache.setCacheObject(REDIS_KEY_PREFIX + "accessToken", accessToken.trim(), 180, TimeUnit.DAYS);
redisCache.setCacheObject(REDIS_KEY_PREFIX + "fileId", fileId.trim(), 180, TimeUnit.DAYS);
redisCache.setCacheObject(REDIS_KEY_PREFIX + "sheetId", sheetId.trim(), 180, TimeUnit.DAYS);
// 同时更新TencentDocConfig对象内存中
tencentDocConfig.setAccessToken(accessToken.trim());
tencentDocConfig.setFileId(fileId.trim());
tencentDocConfig.setSheetId(sheetId.trim());
log.info("腾讯文档配置已更新 - fileId: {}, sheetId: {}", fileId.trim(), sheetId.trim());
JSONObject result = new JSONObject();
result.put("message", "配置更新成功已保存到Redis180天有效期");
result.put("fileId", fileId.trim());
result.put("sheetId", sheetId.trim());
return AjaxResult.success("配置更新成功", result);
} catch (Exception e) {
log.error("更新腾讯文档配置失败", e);
return AjaxResult.error("配置更新失败: " + e.getMessage());
}
}
/**
* 测试配置是否有效
* 尝试读取指定表格的工作表列表
*/
@GetMapping("/test")
public AjaxResult testConfig() {
try {
// 获取当前配置
String accessToken = redisCache.getCacheObject(REDIS_KEY_PREFIX + "accessToken");
String fileId = redisCache.getCacheObject(REDIS_KEY_PREFIX + "fileId");
if (accessToken == null || accessToken.isEmpty()) {
accessToken = tencentDocConfig.getAccessToken();
}
if (fileId == null || fileId.isEmpty()) {
fileId = tencentDocConfig.getFileId();
}
// 验证配置
if (accessToken == null || accessToken.isEmpty()) {
return AjaxResult.error("访问令牌未配置");
}
if (fileId == null || fileId.isEmpty()) {
return AjaxResult.error("文件ID未配置");
}
// 测试API调用获取工作表列表
log.info("测试腾讯文档配置 - fileId: {}", fileId);
JSONObject result = tencentDocService.getSheetList(accessToken, fileId);
if (result != null) {
JSONObject testResult = new JSONObject();
testResult.put("status", "success");
testResult.put("message", "配置有效API调用成功");
testResult.put("fileId", fileId);
testResult.put("apiResponse", result);
return AjaxResult.success("配置测试成功", testResult);
} else {
return AjaxResult.error("配置测试失败API返回null");
}
} catch (Exception e) {
log.error("测试腾讯文档配置失败", e);
return AjaxResult.error("配置测试失败: " + e.getMessage());
}
}
/**
* 清除配置从Redis中删除
*/
@Log(title = "腾讯文档配置", businessType = BusinessType.DELETE)
@DeleteMapping
public AjaxResult clearConfig() {
try {
redisCache.deleteObject(REDIS_KEY_PREFIX + "accessToken");
redisCache.deleteObject(REDIS_KEY_PREFIX + "fileId");
redisCache.deleteObject(REDIS_KEY_PREFIX + "sheetId");
log.info("腾讯文档配置已清除");
return AjaxResult.success("配置已清除");
} catch (Exception e) {
log.error("清除腾讯文档配置失败", e);
return AjaxResult.error("清除配置失败: " + e.getMessage());
}
}
/**
* 获取文档的工作表列表用于选择工作表ID
*
* @param fileId 文件ID
*/
@GetMapping("/sheets")
public AjaxResult getSheetList(@RequestParam String fileId) {
try {
// 获取访问令牌
String accessToken = redisCache.getCacheObject(REDIS_KEY_PREFIX + "accessToken");
if (accessToken == null || accessToken.isEmpty()) {
accessToken = tencentDocConfig.getAccessToken();
}
if (accessToken == null || accessToken.isEmpty()) {
return AjaxResult.error("访问令牌未配置请先设置accessToken");
}
if (fileId == null || fileId.isEmpty()) {
return AjaxResult.error("文件ID不能为空");
}
// 调用API获取工作表列表
JSONObject result = tencentDocService.getSheetList(accessToken, fileId);
if (result != null) {
return AjaxResult.success("获取工作表列表成功", result);
} else {
return AjaxResult.error("获取工作表列表失败API返回null");
}
} catch (Exception e) {
log.error("获取工作表列表失败", e);
return AjaxResult.error("获取工作表列表失败: " + e.getMessage());
}
}
/**
* 脱敏显示敏感信息
* 例如eyJhbGciOi... -> eyJhb...***...o6U显示前6个和后3个字符
*/
private String maskSensitiveInfo(String info) {
if (info == null || info.isEmpty()) {
return "未配置";
}
if (info.length() <= 10) {
return info.substring(0, 3) + "***";
}
return info.substring(0, 6) + "***" + info.substring(info.length() - 3);
}
}