增强钉钉登录地址获取接口:添加登录功能启用检查和异常处理,增加详细日志

This commit is contained in:
Your Name 2026-01-02 12:58:35 +08:00
parent 0006a0b25e
commit d8a5191e3b

View File

@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
@ -16,6 +17,7 @@ import java.util.Map;
/**
* 钉钉集成 Controller
*/
@Slf4j
@Tag(name = "钉钉集成", description = "钉钉集成管理API")
@RestController
@RequestMapping("/api/dingtalk")
@ -165,26 +167,44 @@ public class DingTalkController {
@Operation(summary = "钉钉OAuth登录发起", description = "生成钉钉登录URL")
@GetMapping("/oauth/login")
public Result<Map<String, String>> initiateLogin(@RequestParam(required = false) String redirectUrl) {
if (!dingTalkService.isConfigured()) {
return Result.error("钉钉登录未配置或未启用,请联系管理员在系统设置中配置");
try {
// 检查是否配置
if (!dingTalkService.isConfigured()) {
return Result.error("钉钉登录未配置或未启用,请联系管理员在系统设置中配置");
}
// 检查登录功能是否启用
if (!dingTalkService.isFeatureEnabled("login")) {
return Result.error("钉钉登录功能未启用,请在系统设置中启用钉钉登录功能");
}
DingTalkConfig config = dingTalkService.getConfig();
if (config == null) {
return Result.error("钉钉配置不存在");
}
if (config.getAppKey() == null || config.getAppKey().isEmpty()) {
return Result.error("钉钉AppKey未配置");
}
// 构建回调URL
String callbackUrl = redirectUrl != null ? redirectUrl : "http://localhost:3000/auth/dingtalk/callback";
// 构建钉钉OAuth URL
String authUrl = String.format(
"https://login.dingtalk.com/oauth2/auth?redirect_uri=%s&response_type=code&client_id=%s&scope=openid&state=STATE&prompt=consent",
java.net.URLEncoder.encode(callbackUrl, java.nio.charset.StandardCharsets.UTF_8),
config.getAppKey()
);
Map<String, String> result = new HashMap<>();
result.put("authUrl", authUrl);
result.put("appKey", config.getAppKey());
return Result.success(result);
} catch (Exception e) {
log.error("获取钉钉登录地址失败", e);
return Result.error("获取钉钉登录地址失败: " + e.getMessage());
}
DingTalkConfig config = dingTalkService.getConfig();
// 构建回调URL
String callbackUrl = redirectUrl != null ? redirectUrl : "http://localhost:3000/auth/dingtalk/callback";
// 构建钉钉OAuth URL
String authUrl = String.format(
"https://login.dingtalk.com/oauth2/auth?redirect_uri=%s&response_type=code&client_id=%s&scope=openid&state=STATE&prompt=consent",
java.net.URLEncoder.encode(callbackUrl, java.nio.charset.StandardCharsets.UTF_8),
config.getAppKey()
);
Map<String, String> result = new HashMap<>();
result.put("authUrl", authUrl);
result.put("appKey", config.getAppKey());
return Result.success(result);
}
@Operation(summary = "检查钉钉登录是否已配置")