feat: 同步时自动禁用钉钉中已离职的用户
This commit is contained in:
parent
ff13b0e8d2
commit
aa94b53a39
@ -950,6 +950,7 @@ public class DingTalkService {
|
||||
int updatedUsers = 0;
|
||||
int skippedUsers = 0;
|
||||
int errorUsers = 0;
|
||||
int disabledUsers = 0; // 已离职被禁用的用户数
|
||||
List<String> errors = new ArrayList<>();
|
||||
|
||||
try {
|
||||
@ -974,6 +975,18 @@ public class DingTalkService {
|
||||
Map<String, Object> dingUserInfo = getDingUserDetail(accessToken, user.getDingUserId());
|
||||
|
||||
if (dingUserInfo != null) {
|
||||
// 检查是否是"找不到用户"的错误(已离职)
|
||||
Object errcode = dingUserInfo.get("_errcode");
|
||||
if (errcode != null && "60121".equals(errcode.toString())) {
|
||||
// 用户在钉钉中不存在(已离职),禁用该用户
|
||||
user.setStatus(0);
|
||||
userMapper.updateById(user);
|
||||
disabledUsers++;
|
||||
log.info("用户在钉钉中不存在(已离职),已禁用: userId={}, userName={}",
|
||||
user.getId(), user.getRealName());
|
||||
continue;
|
||||
}
|
||||
|
||||
// 获取用户的主部门ID
|
||||
Object deptIdListObj = dingUserInfo.get("dept_id_list");
|
||||
if (deptIdListObj instanceof List && !((List<?>) deptIdListObj).isEmpty()) {
|
||||
@ -1049,15 +1062,18 @@ public class DingTalkService {
|
||||
result.put("updatedUsers", updatedUsers);
|
||||
result.put("skippedUsers", skippedUsers);
|
||||
result.put("errorUsers", errorUsers);
|
||||
result.put("disabledUsers", disabledUsers);
|
||||
result.put("errors", errors);
|
||||
|
||||
log.info("钉钉组织架构同步完成: 更新{}人, 跳过{}人, 失败{}人", updatedUsers, skippedUsers, errorUsers);
|
||||
log.info("钉钉组织架构同步完成: 更新{}人, 跳过{}人, 禁用{}人(已离职), 失败{}人",
|
||||
updatedUsers, skippedUsers, disabledUsers, errorUsers);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取钉钉用户详情
|
||||
* 注意:如果用户不存在(errcode=60121),会返回包含 _errcode 字段的 Map
|
||||
*/
|
||||
private Map<String, Object> getDingUserDetail(String accessToken, String dingUserId) {
|
||||
try {
|
||||
@ -1080,6 +1096,13 @@ public class DingTalkService {
|
||||
} else {
|
||||
log.warn("获取钉钉用户详情失败: userId={}, errcode={}, errmsg={}",
|
||||
dingUserId, errcode, body.get("errmsg"));
|
||||
// 如果是"找不到用户"的错误,返回包含错误码的 Map,便于调用方判断
|
||||
if (errcode != null && ("60121".equals(errcode.toString()) || errcode.equals(60121))) {
|
||||
Map<String, Object> errorResult = new HashMap<>();
|
||||
errorResult.put("_errcode", "60121");
|
||||
errorResult.put("_errmsg", "找不到该用户");
|
||||
return errorResult;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
@ -3389,7 +3389,14 @@ const handleSyncOrganization = async () => {
|
||||
const response = await request.post('/dingtalk/sync-organization', {}, { timeout: 120000 })
|
||||
if (response.code === 200) {
|
||||
const result = response.data as any
|
||||
message.success({ content: `同步完成!更新${result.updatedUsers}人,跳过${result.skippedUsers}人,失败${result.errorUsers}人`, key: 'sync' })
|
||||
let msg = `同步完成!更新${result.updatedUsers}人,跳过${result.skippedUsers}人`
|
||||
if (result.disabledUsers > 0) {
|
||||
msg += `,禁用${result.disabledUsers}人(已离职)`
|
||||
}
|
||||
if (result.errorUsers > 0) {
|
||||
msg += `,失败${result.errorUsers}人`
|
||||
}
|
||||
message.success({ content: msg, key: 'sync' })
|
||||
|
||||
// 如果有错误信息,显示详情
|
||||
if (result.errors && result.errors.length > 0) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user