修复钉钉测试连接功能:处理appSecret占位符、修复NullPointerException、增强错误处理

This commit is contained in:
Your Name 2026-01-02 12:22:04 +08:00
parent 35e4f8f677
commit 3d52a3ac4d
2 changed files with 105 additions and 40 deletions

View File

@ -112,7 +112,25 @@ public class DingTalkController {
@Operation(summary = "测试钉钉连接")
@PostMapping("/test")
public Result<Boolean> testConnection(@RequestBody DingTalkConfig config) {
return Result.success(dingTalkService.testConnection(config));
try {
// 如果前端传来的appSecret是占位符,则从数据库获取真实的secret
if ("******".equals(config.getAppSecret())) {
DingTalkConfig existingConfig = dingTalkService.getConfig();
if (existingConfig != null && existingConfig.getAppSecret() != null) {
config.setAppSecret(existingConfig.getAppSecret());
} else {
return Result.error("请先保存配置后再测试连接");
}
}
// 如果appKey或appSecret为空,返回错误
if (config.getAppKey() == null || config.getAppKey().isEmpty()
|| config.getAppSecret() == null || config.getAppSecret().isEmpty()) {
return Result.error("AppKey和AppSecret不能为空");
}
return Result.success(dingTalkService.testConnection(config));
} catch (Exception e) {
return Result.error("测试连接失败: " + e.getMessage());
}
}
@Operation(summary = "发送测试消息")

View File

@ -119,18 +119,30 @@ public class DingTalkService {
ResponseEntity<Map> response = restTemplate.getForEntity(url, Map.class);
Map<String, Object> body = response.getBody();
if (body != null && body.get("errcode").equals(0)) {
String accessToken = (String) body.get("access_token");
int expiresIn = (Integer) body.get("expires_in");
if (body != null) {
Object errcode = body.get("errcode");
// 安全地比较errcode(可能是Integer或Long类型)
if (errcode != null && (errcode.equals(0) || errcode.equals(0L) || "0".equals(errcode.toString()))) {
String accessToken = (String) body.get("access_token");
Object expiresInObj = body.get("expires_in");
int expiresIn = expiresInObj instanceof Integer ? (Integer) expiresInObj :
expiresInObj instanceof Long ? ((Long) expiresInObj).intValue() : 7200;
// 缓存token
config.setAccessToken(accessToken);
config.setTokenExpireTime(LocalDateTime.now().plusSeconds(expiresIn - 300)); // 提前5分钟过期
configMapper.updateById(config);
// 缓存token(只有在config有id时才更新数据库)
if (config.getId() != null) {
config.setAccessToken(accessToken);
config.setTokenExpireTime(LocalDateTime.now().plusSeconds(expiresIn - 300)); // 提前5分钟过期
configMapper.updateById(config);
}
return accessToken;
return accessToken;
} else {
log.error("获取钉钉AccessToken失败: errcode={}, errmsg={}, body={}",
errcode, body.get("errmsg"), body);
return null;
}
} else {
log.error("获取钉钉AccessToken失败: {}", body);
log.error("获取钉钉AccessToken失败: 响应体为空");
return null;
}
} catch (Exception e) {
@ -180,11 +192,18 @@ public class DingTalkService {
ResponseEntity<Map> response = restTemplate.postForEntity(url, entity, Map.class);
Map<String, Object> body = response.getBody();
if (body != null && body.get("errcode").equals(0)) {
log.info("钉钉工作通知发送成功: userIds={}", userIds);
return true;
if (body != null) {
Object errcode = body.get("errcode");
if (errcode != null && (errcode.equals(0) || errcode.equals(0L) || "0".equals(errcode.toString()))) {
log.info("钉钉工作通知发送成功: userIds={}", userIds);
return true;
} else {
log.error("钉钉工作通知发送失败: errcode={}, errmsg={}, body={}",
errcode, body.get("errmsg"), body);
return false;
}
} else {
log.error("钉钉工作通知发送失败: {}", body);
log.error("钉钉工作通知发送失败: 响应体为空");
return false;
}
} catch (Exception e) {
@ -227,11 +246,18 @@ public class DingTalkService {
ResponseEntity<Map> response = restTemplate.postForEntity(webhook, entity, Map.class);
Map<String, Object> body = response.getBody();
if (body != null && body.get("errcode").equals(0)) {
log.info("钉钉机器人消息发送成功: title={}", title);
return true;
if (body != null) {
Object errcode = body.get("errcode");
if (errcode != null && (errcode.equals(0) || errcode.equals(0L) || "0".equals(errcode.toString()))) {
log.info("钉钉机器人消息发送成功: title={}", title);
return true;
} else {
log.error("钉钉机器人消息发送失败: errcode={}, errmsg={}, body={}",
errcode, body.get("errmsg"), body);
return false;
}
} else {
log.error("钉钉机器人消息发送失败: {}", body);
log.error("钉钉机器人消息发送失败: 响应体为空");
return false;
}
} catch (Exception e) {
@ -279,8 +305,13 @@ public class DingTalkService {
ResponseEntity<Map> response = restTemplate.postForEntity(url, entity, Map.class);
Map<String, Object> body = response.getBody();
if (body != null && body.get("errcode").equals(0)) {
return (List<Map<String, Object>>) body.get("result");
if (body != null) {
Object errcode = body.get("errcode");
if (errcode != null && (errcode.equals(0) || errcode.equals(0L) || "0".equals(errcode.toString()))) {
return (List<Map<String, Object>>) body.get("result");
} else {
log.error("获取钉钉部门列表失败: errcode={}, errmsg={}", errcode, body.get("errmsg"));
}
}
return Collections.emptyList();
} catch (Exception e) {
@ -367,9 +398,14 @@ public class DingTalkService {
ResponseEntity<Map> response = restTemplate.postForEntity(url, entity, Map.class);
Map<String, Object> body = response.getBody();
if (body != null && body.get("errcode").equals(0)) {
Map<String, Object> result = (Map<String, Object>) body.get("result");
return (String) result.get("userid");
if (body != null) {
Object errcode = body.get("errcode");
if (errcode != null && (errcode.equals(0) || errcode.equals(0L) || "0".equals(errcode.toString()))) {
Map<String, Object> result = (Map<String, Object>) body.get("result");
return (String) result.get("userid");
} else {
log.error("根据unionId获取userId失败: errcode={}, errmsg={}", errcode, body.get("errmsg"));
}
}
return null;
} catch (Exception e) {
@ -399,24 +435,29 @@ public class DingTalkService {
ResponseEntity<Map> response = restTemplate.postForEntity(url, entity, Map.class);
Map<String, Object> body = response.getBody();
if (body != null && body.get("errcode").equals(0)) {
Map<String, Object> result = (Map<String, Object>) body.get("result");
// 获取部门信息
List<Long> deptIdList = (List<Long>) result.get("dept_id_list");
if (deptIdList != null && !deptIdList.isEmpty()) {
List<Map<String, Object>> departments = new ArrayList<>();
for (Long deptId : deptIdList) {
Map<String, Object> deptInfo = getDepartmentInfo(deptId.toString());
if (deptInfo != null) {
departments.add(deptInfo);
if (body != null) {
Object errcode = body.get("errcode");
if (errcode != null && (errcode.equals(0) || errcode.equals(0L) || "0".equals(errcode.toString()))) {
Map<String, Object> result = (Map<String, Object>) body.get("result");
// 获取部门信息
List<Long> deptIdList = (List<Long>) result.get("dept_id_list");
if (deptIdList != null && !deptIdList.isEmpty()) {
List<Map<String, Object>> departments = new ArrayList<>();
for (Long deptId : deptIdList) {
Map<String, Object> deptInfo = getDepartmentInfo(deptId.toString());
if (deptInfo != null) {
departments.add(deptInfo);
}
}
result.put("departments", departments);
log.info("获取到用户部门信息: userId={}, departments={}", userId, departments.size());
}
result.put("departments", departments);
log.info("获取到用户部门信息: userId={}, departments={}", userId, departments.size());
return result;
} else {
log.error("获取详细用户信息失败: errcode={}, errmsg={}", errcode, body.get("errmsg"));
}
return result;
}
return null;
} catch (Exception e) {
@ -446,8 +487,14 @@ public class DingTalkService {
ResponseEntity<Map> response = restTemplate.postForEntity(url, entity, Map.class);
Map<String, Object> body = response.getBody();
if (body != null && body.get("errcode").equals(0)) {
return (Map<String, Object>) body.get("result");
if (body != null) {
Object errcode = body.get("errcode");
if (errcode != null && (errcode.equals(0) || errcode.equals(0L) || "0".equals(errcode.toString()))) {
return (Map<String, Object>) body.get("result");
} else {
log.error("获取钉钉部门信息失败: deptId={}, errcode={}, errmsg={}",
deptId, errcode, body.get("errmsg"));
}
}
return null;
} catch (Exception e) {