1
This commit is contained in:
@@ -351,18 +351,24 @@ public class TencentDocController extends BaseController {
|
||||
/**
|
||||
* 根据单号填充物流链接 - 读取表格数据,根据单号查询订单系统中的物流链接,并填充到表格
|
||||
* 优化:记录上次处理的最大行数,每次从最大行数-100开始读取,避免重复处理历史数据
|
||||
* 自动获取和管理访问令牌,无需前端传递
|
||||
* 自动获取和管理访问令牌,点击同步时自动刷新token
|
||||
*/
|
||||
@PostMapping("/fillLogisticsByOrderNo")
|
||||
public AjaxResult fillLogisticsByOrderNo(@RequestBody Map<String, Object> params) {
|
||||
try {
|
||||
// 自动获取有效的访问令牌
|
||||
// 直接尝试刷新token(如果失败,说明需要首次授权)
|
||||
String accessToken;
|
||||
try {
|
||||
accessToken = tencentDocTokenService.getValidAccessToken();
|
||||
accessToken = tencentDocTokenService.refreshAccessToken();
|
||||
log.info("成功刷新访问令牌");
|
||||
} catch (Exception e) {
|
||||
log.error("获取访问令牌失败", e);
|
||||
return AjaxResult.error("获取访问令牌失败: " + e.getMessage() + "。请先完成授权或检查配置。");
|
||||
log.error("刷新访问令牌失败", e);
|
||||
// 如果刷新失败,尝试获取缓存的token
|
||||
try {
|
||||
accessToken = tencentDocTokenService.getValidAccessToken();
|
||||
} catch (Exception e2) {
|
||||
return AjaxResult.error("访问令牌无效,请先完成授权。获取授权URL: GET /jarvis/tendoc/authUrl");
|
||||
}
|
||||
}
|
||||
|
||||
String fileId = (String) params.get("fileId");
|
||||
|
||||
@@ -75,36 +75,42 @@ public class TencentDocTokenServiceImpl implements ITencentDocTokenService {
|
||||
|
||||
// 先尝试使用refresh_token刷新
|
||||
if (redisCache.hasKey(refreshTokenKey)) {
|
||||
String refreshToken = redisCache.getCacheObject(refreshTokenKey).toString();
|
||||
try {
|
||||
JSONObject tokenInfo = TencentDocApiUtil.refreshAccessToken(
|
||||
tencentDocConfig.getAppId(),
|
||||
tencentDocConfig.getAppSecret(),
|
||||
refreshToken,
|
||||
tencentDocConfig.getRefreshTokenUrl()
|
||||
);
|
||||
|
||||
if (tokenInfo != null && tokenInfo.containsKey("access_token")) {
|
||||
String newAccessToken = tokenInfo.getString("access_token");
|
||||
String newRefreshToken = tokenInfo.getString("refresh_token");
|
||||
Integer expiresIn = tokenInfo.getIntValue("expires_in");
|
||||
Object refreshTokenObj = redisCache.getCacheObject(refreshTokenKey);
|
||||
if (refreshTokenObj != null) {
|
||||
String refreshToken = refreshTokenObj.toString();
|
||||
try {
|
||||
JSONObject tokenInfo = TencentDocApiUtil.refreshAccessToken(
|
||||
tencentDocConfig.getAppId(),
|
||||
tencentDocConfig.getAppSecret(),
|
||||
refreshToken,
|
||||
tencentDocConfig.getRefreshTokenUrl()
|
||||
);
|
||||
|
||||
// 保存新的token
|
||||
saveToken(newAccessToken, newRefreshToken, expiresIn);
|
||||
|
||||
log.info("成功刷新访问令牌");
|
||||
return newAccessToken;
|
||||
if (tokenInfo != null && tokenInfo.containsKey("access_token")) {
|
||||
String newAccessToken = tokenInfo.getString("access_token");
|
||||
String newRefreshToken = tokenInfo.getString("refresh_token");
|
||||
Integer expiresIn = tokenInfo.getIntValue("expires_in");
|
||||
|
||||
// 保存新的token
|
||||
saveToken(newAccessToken, newRefreshToken, expiresIn);
|
||||
|
||||
log.info("成功刷新访问令牌");
|
||||
return newAccessToken;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("使用refresh_token刷新失败: {}", e.getMessage());
|
||||
// refresh_token可能已过期,清除它
|
||||
redisCache.deleteObject(refreshTokenKey);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("使用refresh_token刷新失败,尝试重新获取授权: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有refresh_token或刷新失败,需要重新获取授权
|
||||
// 注意:服务端应用需要使用应用级授权,这里需要根据实际情况调整
|
||||
log.warn("无法自动刷新token,需要手动授权或配置应用级token");
|
||||
throw new RuntimeException("无法获取访问令牌,请检查配置或手动授权");
|
||||
log.warn("无法自动刷新token,没有有效的refresh_token,需要重新授权");
|
||||
throw new RuntimeException("无法获取访问令牌,请先完成授权(访问 /jarvis/tendoc/authUrl 获取授权URL)");
|
||||
|
||||
} catch (RuntimeException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
log.error("刷新访问令牌失败", e);
|
||||
throw new RuntimeException("刷新访问令牌失败: " + e.getMessage(), e);
|
||||
|
||||
Reference in New Issue
Block a user