From 460679ea62e41cd911a698369244572380c3b93b Mon Sep 17 00:00:00 2001 From: zhangxiongfeng Date: Wed, 29 Jul 2026 17:15:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=A7=E5=8D=8E=E5=8F=A3=E9=97=A8=E5=AF=B9?= =?UTF-8?q?=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 604d96b3faea05d5ecb2522a4072b40527564235) --- .../zcloud/basic/info/command/UserAddExe.java | 126 +++++++++++++++++- 1 file changed, 121 insertions(+), 5 deletions(-) diff --git a/web-app/src/main/java/com/zcloud/basic/info/command/UserAddExe.java b/web-app/src/main/java/com/zcloud/basic/info/command/UserAddExe.java index e47e20e5..8e1271ef 100644 --- a/web-app/src/main/java/com/zcloud/basic/info/command/UserAddExe.java +++ b/web-app/src/main/java/com/zcloud/basic/info/command/UserAddExe.java @@ -235,6 +235,10 @@ public class UserAddExe { } private DaHuaSyncResult syncDaHuaPerson(UserE userE) { + return syncDaHuaPerson(userE, userE.getFaceFile(), true); + } + + private DaHuaSyncResult syncDaHuaPerson(UserE userE, Object avatarSource, boolean throwOnFail) { Map personMap = new HashMap<>(); personMap.put("name", userE.getName()); String idCard = userE.getUserIdCard(); @@ -254,16 +258,28 @@ public class UserAddExe { personMap.put("departmentId", 1L); personMap.put("departmentType", 2); personMap.put("type", 3); - personMap.put("nation", 1); - personMap.put("nationName", "汉族"); + if (userE.getNation() != null && !userE.getNation().trim().isEmpty()) { + try { + personMap.put("nation", Integer.valueOf(userE.getNation().trim())); + } catch (Exception e) { + log.warn("用户民族编码转Integer失败,用默认值1,nation={}", userE.getNation()); + personMap.put("nation", 1); + } + } else { + personMap.put("nation", 1); + } + personMap.put("nationName", (userE.getNationName() != null && !userE.getNationName().trim().isEmpty()) ? userE.getNationName().trim() : "汉族"); personMap.put("service", "evo-thirdParty"); - Object faceFile = userE.getFaceFile(); + Object faceFile = avatarSource != null ? avatarSource : userE.getFaceFile(); java.util.function.Function extractThumbFromSingle = (singleObj) -> { if (singleObj == null) { return null; } + if (singleObj instanceof String) { + return singleObj.toString(); + } if (singleObj instanceof Map) { Map faceFileMap = (Map) singleObj; Object thumbUrlObj = faceFileMap.get("thumbUrl"); @@ -447,10 +463,18 @@ public class UserAddExe { if (plainIdCard != null && plainIdCard.length() >= 14) { maskedIdCard = plainIdCard.substring(0, 6) + "********" + plainIdCard.substring(plainIdCard.length() - 4); } + result.setDahuaCode(plainIdCard); + userE.setDahuaCode(plainIdCard); if (success == null || !success) { String errCode = (String) syncResult.getOrDefault("errCode", ""); String errMessage = (String) syncResult.getOrDefault("errMessage", "大华人员同步失败"); - throw new BizException("同步大华平台失败:" + errMessage); + if (throwOnFail) { + throw new BizException("同步大华平台失败:" + errMessage); + } else { + log.warn("同步大华平台未成功(重复信息或其他原因,不影响本地数据保存): errCode={}, errMessage={}, userId={}, idCardMask={}", + errCode, errMessage, userE.getId(), maskedIdCard); + return result; + } } Object data = syncResult.get("data"); if (data != null) { @@ -850,12 +874,56 @@ public class UserAddExe { @Transactional(rollbackFor = Exception.class) public boolean executeXgf(UserXgfAddCmd cmd) { + log.info("开始新增相关方用户,name: {}, corpinfoId: {}, phone: {}", cmd.getName(), cmd.getCorpinfoId(), cmd.getPhone()); + try { + ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + if (attrs != null) { + HttpServletRequest req = attrs.getRequest(); + String orgidHeader = req.getHeader("orgid"); + if (orgidHeader != null && cmd.getCorpinfoId() == null) { + try { + cmd.setCorpinfoId(Long.parseLong(orgidHeader.trim())); + } catch (Exception e) { + log.warn("orgid 转 Long 失败: {}", orgidHeader); + } + } + } + } catch (Exception e) { + log.warn("获取请求头异常: {}", e.getMessage()); + } + Long tenantId = null; SSOUser ssoUser = AuthContext.getCurrentUser(); - Long tenantId = ssoUser.getTenantId(); + if (ssoUser != null) { + tenantId = ssoUser.getTenantId(); + } UserE userE = new UserE(); BeanUtils.copyProperties(cmd, userE); + // tenantId 优先级:cmd 传值 -> AuthContext 上下文 -> ssoUser + if (!ObjectUtils.isEmpty(userE.getTenantId())) { + tenantId = userE.getTenantId(); + } else if (!ObjectUtils.isEmpty(userE.getCorpinfoId())) { + tenantId = userE.getCorpinfoId(); + } else { + try { + Long ctxTenantId = AuthContext.getTenantId(); + if (ctxTenantId != null) { + tenantId = ctxTenantId; + } + } catch (Exception e) { + log.warn("从 AuthContext 获取 tenantId 失败: {}", e.getMessage()); + } + } + if (tenantId == null && ObjectUtils.isEmpty(userE.getCorpinfoId())) { + throw new BizException("无法获取租户信息,请登录后重试或在请求中指定 corpinfoId"); + } + if (ObjectUtils.isEmpty(userE.getCorpinfoId())) { + userE.setCorpinfoId(tenantId); + } userE.initAdd(tenantId, userE); CorpInfoDO corpInfoDO = corpInfoRepository.getById(userE.getCorpinfoId()); + if (corpInfoDO == null) { + throw new BizException("企业信息不存在,corpinfoId=" + userE.getCorpinfoId()); + } userE.setUserType(CorpTypeEnum.getUserTypeByCode(corpInfoDO.getType())); Long roleId = userRepository.getDefaultRoleId(); if (roleId == null) { @@ -928,6 +996,54 @@ public class UserAddExe { userExpandInfoRepository.updateByPhone(userE.getPhone(),userE.getFlowFlag()); } + // ========== 相关方用户同步大华(统一用 /person/sync;重复信息返回false直接跳过不影响本地)========== + Long xgfUserId = addFlag ? userE.getId() : userDOUpdate.getId(); + if (xgfUserId == null) { + log.warn("相关方用户保存后未获取到userId,跳过大华同步及dahua信息写回, addFlag={}", addFlag); + } else { + Object xgfAvatarSource = cmd.getUserImg(); + if (xgfAvatarSource == null) { + xgfAvatarSource = cmd.getUserAvatarUrl(); + } + DaHuaSyncResult xgfSyncResult = null; + try { + xgfSyncResult = syncDaHuaPerson(userE, xgfAvatarSource, false); + } catch (Exception e) { + log.error("相关方用户同步大华异常(不影响本地保存,继续执行写回身份证号为dahuaCode), userId={}, addFlag={}, error={}", + xgfUserId, addFlag, e.getMessage(), e); + } + // 无论大华同步成功/失败/返回false/抛异常,都尝试写回dahuaId/dahuaCode: + // dahuaId = 大华返回的id(成功时才有,失败/异常/重复时为null) + // dahuaCode = 前端传的身份证号(经过Base64解码的明文,syncDaHuaPerson里即使失败也已经set到result了) + Integer xgfDahuaId = xgfSyncResult != null ? xgfSyncResult.dahuaId : null; + String xgfDahuaCode = xgfSyncResult != null ? xgfSyncResult.dahuaCode : null; + // 如果xgfSyncResult异常或dahuaCode为空兜底:直接把userE.getUserIdCard()做一次Base64解码写入dahuaCode,保证100%写回 + if ((xgfDahuaCode == null || xgfDahuaCode.trim().isEmpty()) && userE.getUserIdCard() != null && !userE.getUserIdCard().trim().isEmpty()) { + String idCardRaw = userE.getUserIdCard().trim(); + try { + xgfDahuaCode = new String(java.util.Base64.getDecoder().decode(idCardRaw), java.nio.charset.StandardCharsets.UTF_8); + } catch (Exception e) { + xgfDahuaCode = idCardRaw; + } + } + if (xgfDahuaId != null || (xgfDahuaCode != null && !xgfDahuaCode.trim().isEmpty())) { + try { + UserE updateDahua = new UserE(); + updateDahua.setId(xgfUserId); + updateDahua.setDahuaId(xgfDahuaId); + updateDahua.setDahuaCode(xgfDahuaCode); + userRepository.updateDahuaInfo(updateDahua.getId(), updateDahua.getDahuaId(), updateDahua.getDahuaCode()); + log.info("相关方用户写回dahuaId/dahuaCode成功, userId={}, addFlag={}, dahuaId={}, dahuaCode={}", + xgfUserId, addFlag, xgfDahuaId, xgfDahuaCode); + } catch (Exception e) { + log.error("相关方用户写回dahuaId/dahuaCode失败, userId={}, addFlag={}, dahuaId={}, dahuaCode={}", + xgfUserId, addFlag, xgfDahuaId, xgfDahuaCode, e); + } + } else { + log.warn("相关方用户既无dahuaId也无dahuaCode可写回,跳过update, userId={}, addFlag={}", xgfUserId, addFlag); + } + } + // ========================================================================= //页面相关保存user_corp /**