1
This commit is contained in:
@@ -8,6 +8,7 @@ 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 com.ruoyi.jarvis.service.ITencentDocTokenService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -33,29 +34,43 @@ public class TencentDocConfigController extends BaseController {
|
||||
@Autowired
|
||||
private ITencentDocService tencentDocService;
|
||||
|
||||
@Autowired
|
||||
private ITencentDocTokenService tencentDocTokenService;
|
||||
|
||||
@Autowired
|
||||
private RedisCache redisCache;
|
||||
|
||||
// Redis key前缀
|
||||
// Redis key前缀(用于存储文档配置)
|
||||
private static final String REDIS_KEY_PREFIX = "tencent:doc:auto:config:";
|
||||
|
||||
/**
|
||||
* 获取当前配置
|
||||
* 注意:accessToken 由系统自动管理(通过授权登录),此接口只返回状态
|
||||
*/
|
||||
@GetMapping
|
||||
public AjaxResult getConfig() {
|
||||
try {
|
||||
JSONObject config = new JSONObject();
|
||||
|
||||
// 从Redis获取配置(如果存在)
|
||||
String accessToken = redisCache.getCacheObject(REDIS_KEY_PREFIX + "accessToken");
|
||||
// 1. 检查 accessToken 状态(从Token服务)
|
||||
boolean hasAccessToken = false;
|
||||
String accessTokenStatus = "未授权";
|
||||
try {
|
||||
String accessToken = tencentDocTokenService.getValidAccessToken();
|
||||
if (accessToken != null && !accessToken.isEmpty()) {
|
||||
hasAccessToken = true;
|
||||
accessTokenStatus = "已授权";
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// Token不存在或已过期
|
||||
accessTokenStatus = "未授权:" + e.getMessage();
|
||||
}
|
||||
|
||||
// 2. 从Redis获取文档配置
|
||||
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();
|
||||
}
|
||||
@@ -63,17 +78,27 @@ public class TencentDocConfigController extends BaseController {
|
||||
sheetId = tencentDocConfig.getSheetId();
|
||||
}
|
||||
|
||||
config.put("accessToken", maskSensitiveInfo(accessToken)); // 脱敏显示
|
||||
config.put("hasAccessToken", hasAccessToken);
|
||||
config.put("accessTokenStatus", accessTokenStatus);
|
||||
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);
|
||||
boolean isConfigured = hasAccessToken &&
|
||||
fileId != null && !fileId.isEmpty() &&
|
||||
sheetId != null && !sheetId.isEmpty();
|
||||
config.put("isConfigured", isConfigured);
|
||||
|
||||
// 提供配置建议
|
||||
if (!hasAccessToken) {
|
||||
config.put("hint", "请先访问 /jarvis/tendoc/authUrl 完成授权登录");
|
||||
} else if (fileId == null || fileId.isEmpty() || sheetId == null || sheetId.isEmpty()) {
|
||||
config.put("hint", "请配置目标文档的 fileId 和 sheetId");
|
||||
} else {
|
||||
config.put("hint", "配置完整,H-TF订单将自动写入腾讯文档");
|
||||
}
|
||||
|
||||
return AjaxResult.success("获取配置成功", config);
|
||||
} catch (Exception e) {
|
||||
@@ -84,21 +109,18 @@ public class TencentDocConfigController extends BaseController {
|
||||
|
||||
/**
|
||||
* 更新配置(保存到Redis,180天有效期)
|
||||
* 注意:accessToken 由系统自动管理,无需手动配置
|
||||
*
|
||||
* @param params 包含 accessToken, fileId, sheetId
|
||||
* @param params 包含 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不能为空");
|
||||
}
|
||||
@@ -106,22 +128,34 @@ public class TencentDocConfigController extends BaseController {
|
||||
return AjaxResult.error("工作表ID不能为空");
|
||||
}
|
||||
|
||||
// 检查是否已授权
|
||||
boolean hasAccessToken = false;
|
||||
try {
|
||||
String accessToken = tencentDocTokenService.getValidAccessToken();
|
||||
hasAccessToken = (accessToken != null && !accessToken.isEmpty());
|
||||
} catch (Exception e) {
|
||||
log.warn("检查授权状态时出错: {}", e.getMessage());
|
||||
}
|
||||
|
||||
if (!hasAccessToken) {
|
||||
return AjaxResult.error("尚未完成腾讯文档授权,请先访问 /jarvis/tendoc/authUrl 完成授权");
|
||||
}
|
||||
|
||||
// 保存到Redis(180天有效期)
|
||||
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());
|
||||
log.info("H-TF订单自动写入配置已更新 - fileId: {}, sheetId: {}", fileId.trim(), sheetId.trim());
|
||||
|
||||
JSONObject result = new JSONObject();
|
||||
result.put("message", "配置更新成功,已保存到Redis(180天有效期)");
|
||||
result.put("fileId", fileId.trim());
|
||||
result.put("sheetId", sheetId.trim());
|
||||
result.put("hint", "现在录入分销标识为 H-TF 的订单时,将自动追加到此腾讯文档");
|
||||
|
||||
return AjaxResult.success("配置更新成功", result);
|
||||
} catch (Exception e) {
|
||||
@@ -137,26 +171,30 @@ public class TencentDocConfigController extends BaseController {
|
||||
@GetMapping("/test")
|
||||
public AjaxResult testConfig() {
|
||||
try {
|
||||
// 获取当前配置
|
||||
String accessToken = redisCache.getCacheObject(REDIS_KEY_PREFIX + "accessToken");
|
||||
String fileId = redisCache.getCacheObject(REDIS_KEY_PREFIX + "fileId");
|
||||
// 1. 获取访问令牌(从Token服务)
|
||||
String accessToken;
|
||||
try {
|
||||
accessToken = tencentDocTokenService.getValidAccessToken();
|
||||
} catch (Exception e) {
|
||||
return AjaxResult.error("获取访问令牌失败:" + e.getMessage() +
|
||||
"。请先访问 /jarvis/tendoc/authUrl 完成授权");
|
||||
}
|
||||
|
||||
if (accessToken == null || accessToken.isEmpty()) {
|
||||
accessToken = tencentDocConfig.getAccessToken();
|
||||
return AjaxResult.error("访问令牌未配置,请先完成腾讯文档授权");
|
||||
}
|
||||
|
||||
// 2. 获取文档配置
|
||||
String fileId = redisCache.getCacheObject(REDIS_KEY_PREFIX + "fileId");
|
||||
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未配置");
|
||||
return AjaxResult.error("文件ID未配置,请先配置目标文档");
|
||||
}
|
||||
|
||||
// 测试API调用:获取工作表列表
|
||||
// 3. 测试API调用:获取工作表列表
|
||||
log.info("测试腾讯文档配置 - fileId: {}", fileId);
|
||||
JSONObject result = tencentDocService.getSheetList(accessToken, fileId);
|
||||
|
||||
@@ -178,19 +216,23 @@ public class TencentDocConfigController extends BaseController {
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除配置(从Redis中删除)
|
||||
* 清除配置(从Redis中删除文档配置)
|
||||
* 注意:这不会清除授权令牌,如需清除令牌请访问 /jarvis/tendoc/clearToken
|
||||
*/
|
||||
@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("腾讯文档配置已清除");
|
||||
log.info("H-TF订单自动写入配置已清除");
|
||||
|
||||
return AjaxResult.success("配置已清除");
|
||||
JSONObject result = new JSONObject();
|
||||
result.put("message", "文档配置已清除");
|
||||
result.put("hint", "授权令牌未清除。如需清除授权,请访问 /jarvis/tendoc/clearToken");
|
||||
|
||||
return AjaxResult.success("配置已清除", result);
|
||||
} catch (Exception e) {
|
||||
log.error("清除腾讯文档配置失败", e);
|
||||
return AjaxResult.error("清除配置失败: " + e.getMessage());
|
||||
@@ -205,14 +247,17 @@ public class TencentDocConfigController extends BaseController {
|
||||
@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();
|
||||
// 获取访问令牌(从Token服务)
|
||||
String accessToken;
|
||||
try {
|
||||
accessToken = tencentDocTokenService.getValidAccessToken();
|
||||
} catch (Exception e) {
|
||||
return AjaxResult.error("获取访问令牌失败:" + e.getMessage() +
|
||||
"。请先访问 /jarvis/tendoc/authUrl 完成授权");
|
||||
}
|
||||
|
||||
if (accessToken == null || accessToken.isEmpty()) {
|
||||
return AjaxResult.error("访问令牌未配置,请先设置accessToken");
|
||||
return AjaxResult.error("访问令牌未配置,请先完成腾讯文档授权");
|
||||
}
|
||||
|
||||
if (fileId == null || fileId.isEmpty()) {
|
||||
@@ -220,6 +265,7 @@ public class TencentDocConfigController extends BaseController {
|
||||
}
|
||||
|
||||
// 调用API获取工作表列表
|
||||
log.info("获取腾讯文档工作表列表 - fileId: {}", fileId);
|
||||
JSONObject result = tencentDocService.getSheetList(accessToken, fileId);
|
||||
|
||||
if (result != null) {
|
||||
@@ -228,23 +274,9 @@ public class TencentDocConfigController extends BaseController {
|
||||
return AjaxResult.error("获取工作表列表失败:API返回null");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("获取工作表列表失败", e);
|
||||
log.error("获取工作表列表失败 - fileId: {}", fileId, 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user