From 6317f78628949be9cd969fd83266b4f7caba5c27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E9=9B=AA?= <1044122544@qq.com> Date: Fri, 27 Jun 2025 18:40:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E4=BB=98=E5=AE=9D=E5=88=B7=E8=84=B8?= =?UTF-8?q?=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/AliScanFaceServiceImpl.java | 117 ++++++++++++------ 1 file changed, 80 insertions(+), 37 deletions(-) diff --git a/src/main/java/com/dpkj/modules/scanface/ali/service/impl/AliScanFaceServiceImpl.java b/src/main/java/com/dpkj/modules/scanface/ali/service/impl/AliScanFaceServiceImpl.java index bd52948..7b0e8e1 100644 --- a/src/main/java/com/dpkj/modules/scanface/ali/service/impl/AliScanFaceServiceImpl.java +++ b/src/main/java/com/dpkj/modules/scanface/ali/service/impl/AliScanFaceServiceImpl.java @@ -58,45 +58,52 @@ public class AliScanFaceServiceImpl implements IAliScanFaceService { public Result aliFacePay(AliOrderVo aliOrderVo) { try { /** - * 1、获取刷脸"刷脸去初始化服务"的ftoken返回值 + * 0、执行ABCP初始化 */ - this.iniAbcpAbsolute(); - //参数 - JSONObject zolozConfig = new JSONObject().fluentPut("installAngle", 90); - JSONObject params = new JSONObject() - .fluentPut("serviceId", aliFaceConfig.getServiceId()) - .fluentPut("zolozConfig", zolozConfig); - String json = params.toJSONString(); - String service_code = AliFaceConstants.SMILEVERIFYNIN_V1; //调用的组件编码:初始化 - Result startServiceIniResult = this.startServiceIni(json, service_code); - log.info("[AliScanFaceServiceImpl][aliFacePay][72][1、获取刷脸去初始化服务的结果] :{}", startServiceIniResult.toString()); - if (startServiceIniResult.isSuccess()) { - Map res = (Map) startServiceIniResult.getResult(); - String ftoken = res.get("ftoken"); - log.info("[AliScanFaceServiceImpl][aliFacePay][76][[1、获取刷脸去初始化服务的结果-ftoken值] :{}", ftoken); - + Result result = this.iniAbcpAbsolute(); + if (result.isSuccess()) { /** - * 2、调用后端的支付宝统一收单交易支付接口、存入hisPay + * 1、获取刷脸"刷脸去初始化服务"的ftoken返回值 */ - aliOrderVo.setAuthCode(ftoken);//Demo值:"fp128d26333fa66e66e7f34c493d30cdh76" - JSONObject serverParams = (JSONObject) JSON.toJSON(aliOrderVo); - log.info("[AliScanFaceServiceImpl][aliFacePay][83][调用后端的支付宝统一收单交易支付接口参数] :{}", serverParams.toString()); + //参数 + JSONObject zolozConfig = new JSONObject().fluentPut("installAngle", 90); + JSONObject params = new JSONObject() + .fluentPut("serviceId", aliFaceConfig.getServiceId()) + .fluentPut("zolozConfig", zolozConfig); + String json = params.toJSONString(); + String service_code = AliFaceConstants.SMILEVERIFYNIN_V1; //调用的组件编码:初始化 + Result startServiceIniResult = this.startServiceIni(json, service_code); + log.info("[AliScanFaceServiceImpl][aliFacePay][72][1、获取刷脸去初始化服务的结果] :{}", startServiceIniResult.toString()); + if (startServiceIniResult.isSuccess()) { + Map res = (Map) startServiceIniResult.getResult(); + String ftoken = res.get("ftoken"); + log.info("[AliScanFaceServiceImpl][aliFacePay][76][[1、获取刷脸去初始化服务的结果-ftoken值] :{}", ftoken); - String url = serverUrl + "openapi/aliPayOrderApi/createOrder"; - log.info("[AliScanFaceServiceImpl][aliFacePay][86][调用后端的支付宝统一收单交易支付接口路径] :{}", url); - String req = HttpRequest.post(url) - .header(Header.CONTENT_TYPE, ContentType.JSON.toString(CharsetUtil.CHARSET_UTF_8)) - .body(serverParams.toJSONString()) - .execute() - .body(); - JSONObject serverResult = JSONObject.parseObject(req); - log.info("[AliScanFaceServiceImpl][aliFacePay][93][调用后端的支付宝统一收单交易支付接口结果] :{}", serverResult.toString()); + /** + * 2、调用后端的支付宝统一收单交易支付接口、存入hisPay + */ + aliOrderVo.setAuthCode(ftoken);//Demo值:"fp128d26333fa66e66e7f34c493d30cdh76" + JSONObject serverParams = (JSONObject) JSON.toJSON(aliOrderVo); + log.info("[AliScanFaceServiceImpl][aliFacePay][83][调用后端的支付宝统一收单交易支付接口参数] :{}", serverParams.toString()); - return Result.ok(serverResult); - } else { - //调用ABCP 刷脸初始化服务失败 - log.error("[AliScanFaceServiceImpl][aliFacePay][299]调用ABCP 刷脸初始化服务失败 :{}", startServiceIniResult.getMessage()); - return Result.error(startServiceIniResult.getMessage()); + String url = serverUrl + "openapi/aliPayOrderApi/createOrder"; + log.info("[AliScanFaceServiceImpl][aliFacePay][86][调用后端的支付宝统一收单交易支付接口路径] :{}", url); + String req = HttpRequest.post(url) + .header(Header.CONTENT_TYPE, ContentType.JSON.toString(CharsetUtil.CHARSET_UTF_8)) + .body(serverParams.toJSONString()) + .execute() + .body(); + JSONObject serverResult = JSONObject.parseObject(req); + log.info("[AliScanFaceServiceImpl][aliFacePay][93][调用后端的支付宝统一收单交易支付接口结果] :{}", serverResult.toString()); + + return Result.ok(serverResult); + } else { + //调用ABCP 刷脸初始化服务失败 + log.error("[AliScanFaceServiceImpl][aliFacePay][299]调用ABCP 刷脸初始化服务失败 :{}", startServiceIniResult.getMessage()); + return Result.error(startServiceIniResult.getMessage()); + } + }else { + return Result.error("执行iniAbcpAbsolute初始化失败"); } } catch (Exception e) { e.printStackTrace(); @@ -132,23 +139,59 @@ public class AliScanFaceServiceImpl implements IAliScanFaceService { //指定支付宝LOT SDK的本地库路径 AbcpInvoke.SetAPIPathFile(aliFaceConfig.getDllPath()); + // 使用 CountDownLatch 实现线程同步 + CountDownLatch latch = new CountDownLatch(1); + CountDownLatch latchFinish = new CountDownLatch(1); + + //获取返回数据 + AtomicReference processCode = new AtomicReference<>(); + AtomicReference processResult = new AtomicReference<>(); + AtomicReference finishCode = new AtomicReference<>(); + AtomicReference finishResult = new AtomicReference<>(); + //创建回调实例 AbcpInvoke.CallbackRsp callbackRsp = new AbcpInvoke.CallbackRsp() { @Override public void OnProcess(int code, String subCode, String subMsg, String result) { - log.info("[AliScanFaceServiceImpl][OnProcess][123][ABCP调用iniAbcpAbsolute][code:{}][subCode:{}][subMsg:{}][result:{}]", code, subCode, subMsg, result); + log.info("[AliScanFaceServiceImpl][OnProcess][155][0、ABCP调用iniAbcpAbsolute-OnProcess][code:{}][subCode:{}][subMsg:{}][result:{}] ", code, subCode, subMsg, result); + try { + processCode.set(code); + processResult.set(result); + } finally { + latch.countDown(); // 确保无论如何都释放锁 + } } @Override public void OnFinish(int code, String subCode, String subMsg, String result) { - log.info("[AliScanFaceServiceImpl][OnFinish][128][ABCP调用iniAbcpAbsolute][code:{}][subCode:{}][subMsg:{}][result:{}]", code, subCode, subMsg, result); + log.info("[AliScanFaceServiceImpl][OnFinish][167][0、ABCP调用iniAbcpAbsolute-OnFinish][code:{}][subCode:{}][subMsg:{}][result:{}]", code, subCode, subMsg, result); + try { + finishCode.set(code); + finishResult.set(result); + } finally { + latchFinish.countDown(); // 确保无论如何都释放锁 + } } }; //初始化 + log.info("[AliScanFaceServiceImpl][iniAbcpAbsolute][175][0、ABCP调用iniAbcpAbsolute初始化参数][appId:{}][appVersion:{}][json:{}] ", aliFaceConfig.getAppId(), aliFaceConfig.getAppVersion(), json); AbcpInvoke.AbcpInit(aliFaceConfig.getAppId(), aliFaceConfig.getAppVersion(), json, callbackRsp); - return Result.ok("支付宝ABCP初始化成功"); + // 等待finish回调完成(设置超时避免死锁) + boolean awaitFinishSuccess = latchFinish.await(120, TimeUnit.SECONDS); + if (!awaitFinishSuccess) { + log.info("[AliScanFaceServiceImpl][iniAbcpAbsolute][181] [0、ABCP调用iniAbcpAbsolute初始化-120秒-等待finish回调超时]"); + return Result.error("初始化等待finish回调超时"); + } else { + JSONObject jsonObject = JSONObject.parseObject(finishResult.get()); + log.info("[AliScanFaceServiceImpl][iniAbcpAbsolute][184][0、ABCP调用iniAbcpAbsolute初始化-finish回调结果-finishCode :{}] [返回结果:{}]", finishCode, jsonObject.toString()); + if (finishCode.get() == 1000) { + return Result.ok("ABCP调用iniAbcpAbsolute初始化成功"); + } else { + return Result.error("ABCP调用刷脸初始化服务finish失败"); + } + } } else { log.info("[AliScanFaceServiceImpl][iniAbcpAbsolute][55][{} :文件不存在]", aliFaceConfig.getDllPath()); return Result.error("支付宝ABCP初始化失败:" + aliFaceConfig.getDllPath() + "不存在");