This commit is contained in:
Leo
2026-01-15 20:51:40 +08:00
parent 2b74f77419
commit ff9ab96833

View File

@@ -4,6 +4,7 @@ import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.jarvis.config.WPS365Config; import com.ruoyi.jarvis.config.WPS365Config;
import com.ruoyi.jarvis.domain.dto.WPS365TokenInfo; import com.ruoyi.jarvis.domain.dto.WPS365TokenInfo;
import com.ruoyi.jarvis.service.IWPS365OAuthService; import com.ruoyi.jarvis.service.IWPS365OAuthService;
import com.ruoyi.jarvis.service.IWPS365ApiService;
import com.ruoyi.jarvis.util.WPS365ApiUtil; import com.ruoyi.jarvis.util.WPS365ApiUtil;
import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.core.redis.RedisCache;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -36,6 +37,9 @@ public class WPS365OAuthServiceImpl implements IWPS365OAuthService {
@Autowired @Autowired
private RedisCache redisCache; private RedisCache redisCache;
@Autowired
private IWPS365ApiService wps365ApiService;
@Override @Override
public String getAuthUrl(String state) { public String getAuthUrl(String state) {
if (wps365Config == null) { if (wps365Config == null) {
@@ -169,9 +173,56 @@ public class WPS365OAuthServiceImpl implements IWPS365OAuthService {
tokenInfo.setTokenType(result.getString("token_type")); tokenInfo.setTokenType(result.getString("token_type"));
tokenInfo.setExpiresIn(result.getInteger("expires_in")); tokenInfo.setExpiresIn(result.getInteger("expires_in"));
tokenInfo.setScope(result.getString("scope")); tokenInfo.setScope(result.getString("scope"));
tokenInfo.setUserId(result.getString("user_id"));
log.info("成功获取访问令牌 - userId: {}", tokenInfo.getUserId()); // WPS365的token响应中可能不包含user_id需要调用用户信息API获取
String userId = result.getString("user_id");
if (userId == null || userId.trim().isEmpty()) {
// 尝试通过用户信息API获取用户ID
try {
JSONObject userInfo = wps365ApiService.getUserInfo(tokenInfo.getAccessToken());
if (userInfo != null) {
// 尝试多种可能的用户ID字段名
userId = userInfo.getString("id");
if (userId == null || userId.trim().isEmpty()) {
userId = userInfo.getString("user_id");
}
if (userId == null || userId.trim().isEmpty()) {
userId = userInfo.getString("open_id");
}
if (userId == null || userId.trim().isEmpty()) {
userId = userInfo.getString("uid");
}
// 如果还是获取不到使用access_token的前16位作为标识临时方案
if (userId == null || userId.trim().isEmpty()) {
String accessToken = tokenInfo.getAccessToken();
if (accessToken != null && accessToken.length() > 16) {
userId = "wps365_" + accessToken.substring(0, 16);
log.warn("无法从用户信息API获取用户ID使用access_token前16位作为标识: {}", userId);
} else {
userId = "wps365_default";
log.warn("无法获取用户ID使用默认值: {}", userId);
}
} else {
log.info("通过用户信息API获取到用户ID: {}", userId);
}
} else {
userId = "wps365_default";
log.warn("用户信息API返回为空使用默认用户ID: {}", userId);
}
} catch (Exception e) {
log.warn("调用用户信息API失败使用默认用户ID: {}", e.getMessage());
// 使用access_token的前16位作为标识临时方案
String accessToken = tokenInfo.getAccessToken();
if (accessToken != null && accessToken.length() > 16) {
userId = "wps365_" + accessToken.substring(0, 16);
} else {
userId = "wps365_default";
}
}
}
tokenInfo.setUserId(userId);
log.info("成功获取访问令牌 - userId: {}", userId);
return tokenInfo; return tokenInfo;
} catch (Exception e) { } catch (Exception e) {