diff --git a/ebike-user/src/main/java/com/cdzy/user/utils/VerifyUtil.java b/ebike-user/src/main/java/com/cdzy/user/utils/VerifyUtil.java index 8700c8f..53531f0 100644 --- a/ebike-user/src/main/java/com/cdzy/user/utils/VerifyUtil.java +++ b/ebike-user/src/main/java/com/cdzy/user/utils/VerifyUtil.java @@ -155,7 +155,6 @@ public class VerifyUtil { if (userInfo == null) { throw new EbikeException("用户不存在"); } - // 校验是否已进行验证 Integer realNameStatus = userInfo.getUserRealNameStatus(); if (realNameStatus != null && realNameStatus.equals(RealNameAttestationStatus.CERTIFIED)) { throw new EbikeException("用户已实名验证"); @@ -175,29 +174,37 @@ public class VerifyUtil { ebikeRealNameVerifyDto.setTimestamp(encryptedTimestamp); ebikeRealNameVerifyDto.setSign(sign); ebikeRealNameVerifyDto.setKey(realNameVerifyConfig.getApiKey()); - - // 2. 验证用户实名 + // 2. 调用第三方实名验证 JsonNode result = httpPost("验证用户实名", REALNAME_VERIFY_URL, ebikeRealNameVerifyDto, null); - log.info("验证用户实名结果:{}", result); + log.info("验证用户实名结果: {}", result); if (result == null) { throw new EbikeException("验证用户实名失败"); } - - if (result.has("code") && "10000".equals(result.get("code").asText())) { - // 3. 解密结果 - String data = securityContext.decrypt(result.get("data").asText(), realNameVerifyConfig.getClientPrivateKey()); - JsonNode jsonData = objectMapper.readTree(data); - if (jsonData.has("result") && "1".equals(jsonData.get("result").asText())) { - // 4. 更新用户实名信息 - updateRealNameInfo(userValidateDto, true); - log.info("验证用户实名成功"); - } + String code = result.has("code") ? result.get("code").asText() : ""; + if (!"10000".equals(code)) { + String message = result.has("message") ? result.get("message").asText() : "未知错误"; + log.error("验证用户实名失败, code: {}, message: {}", code, message); + throw new EbikeException("验证用户实名失败: " + message); } + // 3. 解密并解析结果 + String data = securityContext.decrypt(result.get("data").asText(), realNameVerifyConfig.getClientPrivateKey()); + JsonNode jsonData = objectMapper.readTree(data); + + if (jsonData.has("result") && "1".equals(jsonData.get("result").asText())) { + // 验证成功直接返回 + updateRealNameInfo(userValidateDto, true); + log.info("验证用户实名成功"); + return; + } else { + // 验证失败 + String failReason = jsonData.has("msg") ? jsonData.get("msg").asText() : "实名验证未通过"; + log.warn("实名验证未通过,原因: {}", failReason); + } + + // 4. 验证失败:更新为未认证 updateRealNameInfo(userValidateDto, false); - String errorCode = result.has("code") ? result.get("code").asText() : "未知错误"; - String errorMessage = result.has("message") ? result.get("message").asText() : "验证失败"; - log.error("验证用户实名失败,{} {}", errorCode, errorMessage); throw new EbikeException("验证用户实名失败"); + } catch (EncryptFailureException e) { throw new EbikeException("加密失败"); } catch (SignFailureException e) {