WPS365
This commit is contained in:
@@ -4,6 +4,7 @@ import com.alibaba.fastjson2.JSONObject;
|
||||
import com.ruoyi.jarvis.config.WPS365Config;
|
||||
import com.ruoyi.jarvis.domain.dto.WPS365TokenInfo;
|
||||
import com.ruoyi.jarvis.service.IWPS365OAuthService;
|
||||
import com.ruoyi.jarvis.service.IWPS365ApiService;
|
||||
import com.ruoyi.jarvis.util.WPS365ApiUtil;
|
||||
import com.ruoyi.common.core.redis.RedisCache;
|
||||
import org.slf4j.Logger;
|
||||
@@ -36,6 +37,9 @@ public class WPS365OAuthServiceImpl implements IWPS365OAuthService {
|
||||
@Autowired
|
||||
private RedisCache redisCache;
|
||||
|
||||
@Autowired
|
||||
private IWPS365ApiService wps365ApiService;
|
||||
|
||||
@Override
|
||||
public String getAuthUrl(String state) {
|
||||
if (wps365Config == null) {
|
||||
@@ -169,9 +173,56 @@ public class WPS365OAuthServiceImpl implements IWPS365OAuthService {
|
||||
tokenInfo.setTokenType(result.getString("token_type"));
|
||||
tokenInfo.setExpiresIn(result.getInteger("expires_in"));
|
||||
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;
|
||||
} catch (Exception e) {
|
||||
|
||||
Reference in New Issue
Block a user