支付宝刷脸初始化、调用服务
This commit is contained in:
parent
da3aa8d49c
commit
909ad5cc8c
|
@ -15,13 +15,38 @@ import org.springframework.stereotype.Component;
|
|||
@ConfigurationProperties(prefix = "dpkj.ali.face")
|
||||
public class AliFaceConfig {
|
||||
|
||||
/**
|
||||
* ABCP_SDK部署后DLL文件路径
|
||||
*/
|
||||
private String dllPath;
|
||||
|
||||
/**
|
||||
* 应用管理-appid
|
||||
*/
|
||||
private String appId;
|
||||
|
||||
/**
|
||||
* IOT应用版本
|
||||
*/
|
||||
private String appVersion;
|
||||
|
||||
/**
|
||||
* 签约商家的 PID,以 2088 开头,企业主体
|
||||
*/
|
||||
private String merchantId;
|
||||
|
||||
/**
|
||||
* 商家机具终端编号,每台设备保持唯一
|
||||
*/
|
||||
private String deviceNum;
|
||||
|
||||
/**
|
||||
* 服务商的 PID
|
||||
*/
|
||||
private String partnerId;
|
||||
|
||||
/**
|
||||
* 核心入参 serviceId
|
||||
*/
|
||||
private String serviceId;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
package com.dpkj.modules.scanface.ali.constants;
|
||||
|
||||
/**
|
||||
* @description: 支付宝刷脸模块常量
|
||||
* @author: Zhangxue
|
||||
* @time: 2025/4/24 9:25
|
||||
*/
|
||||
public final class AliFaceConstants {
|
||||
|
||||
/**
|
||||
* 刷脸去初始化服务
|
||||
* https://ant-iot.alipay.com/open/iotbpaas/service/serviceManage/serviceDetail
|
||||
*/
|
||||
public static final String SMILEVERIFYNIN_V1 = "BPaaSSmileVerifyNonInitV1";
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 刷脸支付服务
|
||||
* https://ant-iot.alipay.com/open/iotbpaas/app/appManage/newAppPublish/serviceDetail
|
||||
*/
|
||||
public static final String SMILEPAY = "BPaaSFaceSmilePayVerify";
|
||||
|
||||
|
||||
}
|
|
@ -3,19 +3,21 @@ package com.dpkj.modules.scanface.ali.controller;
|
|||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.dpkj.common.vo.Result;
|
||||
import com.dpkj.modules.scanface.ali.config.AliFaceConfig;
|
||||
import com.dpkj.modules.scanface.ali.constants.AliFaceConstants;
|
||||
import com.dpkj.modules.scanface.ali.dll.AbcpInvoke;
|
||||
import com.dpkj.modules.scanface.ali.dll.AliScanFaceDll;
|
||||
import com.dpkj.modules.scanface.ali.service.IAliScanFaceService;
|
||||
import com.dpkj.modules.scanface.ali.vo.MyCallbackRsp;
|
||||
import com.sun.jna.Callback;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Auther: 萧道子
|
||||
|
@ -35,92 +37,67 @@ public class AliScanFaceController {
|
|||
|
||||
/**
|
||||
* ABCP初始化,
|
||||
* https://opendocs.alipay.com/iot/05e9ye
|
||||
* 文档地址:https://opendocs.alipay.com/iot/05e9ye
|
||||
* ABCP初始化:商家App 启动时通过调用接口 abcp_init 执行 ABCP SDK 的初始化,初始化动作只需启动时调用一次,初始化的时候需保证机具网络处于联网状态。
|
||||
*
|
||||
* @return
|
||||
* @throws AliScanFaceDll.DllRegistrationException
|
||||
*/
|
||||
@GetMapping("iniAbcp")
|
||||
Result<Object> iniAbcp() throws AliScanFaceDll.DllRegistrationException {
|
||||
return aliScanFaceService.iniAbcp();
|
||||
Result<Object> iniAbcp() {
|
||||
return aliScanFaceService.iniAbcpAbsolute();
|
||||
}
|
||||
|
||||
/**
|
||||
* ABCP服务调用
|
||||
* https://opendocs.alipay.com/iot/05e9ye
|
||||
* ABCP服务调用 刷脸去初始化服务
|
||||
* 文档地址:https://opendocs.alipay.com/iot/05e9ye
|
||||
* 初始化成功后,商家App 可根据业务需求,调用接口 abcp_start_service 执行 ABCP 所提供的服务。
|
||||
* 上述服务调用过程可重复多次调用,通过传入不同的 service_code 来调用不同的 ABCP 服务。
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("abcpStartService")
|
||||
Result<Object> abcpStartService() {
|
||||
@GetMapping("abcpStartServiceIni")
|
||||
Result<Object> abcpStartServiceIni() {
|
||||
aliScanFaceService.iniAbcpAbsolute();
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**ABCP服务调用*/
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
AliScanFaceDll.Dll dll = AliScanFaceDll.instance();
|
||||
dll.bp_api_set_isv_lan(3);
|
||||
|
||||
Integer arg= AbcpInvoke.getMsSeedId();
|
||||
String app_id= "2021005138692337";
|
||||
String service_code= "BPaaSSmileCard";
|
||||
String json_param= "";
|
||||
AbcpInvoke.ABCPProcess msABCPProcess = new AbcpInvoke.ABCPProcess();
|
||||
AbcpInvoke.ABCPFinish msABCPFinish = new AbcpInvoke.ABCPFinish();
|
||||
dll.abcp_start_service(arg,app_id,service_code,json_param,msABCPProcess,msABCPFinish);
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
//测试使用绝对路径
|
||||
public static void main2(String[] args) throws NoSuchFieldException, IllegalAccessException, AliScanFaceDll.DllRegistrationException {
|
||||
File dllFile = new File("C:\\opt\\ant-abcp\\bpaas_api.dll");
|
||||
System.out.println("DLL 存在: " + dllFile.exists());
|
||||
|
||||
//System.setProperty("java.library.path", "C:/opt/ant-abcp");
|
||||
// 加载DLL(直接指定绝对路径)
|
||||
System.load("C:/opt/ant-abcp/bpaas_api.dll");
|
||||
|
||||
String appId = "2021005138692337";
|
||||
String appVersion = "1.0.0.0";
|
||||
JSONObject params = new JSONObject()
|
||||
.fluentPut("appId", appId) //应用ID
|
||||
.fluentPut("merchantId", "2088641941653700") //签约商家的 PID,以 2088 开头
|
||||
.fluentPut("deviceNum", "P060003750")//商家机具终端编号,每台设备保持唯一
|
||||
.fluentPut("partnerId", "2088641941653700");//服务商的 PID
|
||||
//参数
|
||||
JSONObject params = new JSONObject().fluentPut("serviceId", aliFaceConfig.getServiceId()); //服务场景
|
||||
String json = params.toJSONString();
|
||||
// 指定支付宝LOT SDK的本地库路径
|
||||
AbcpInvoke.SetAPIPathFile("C:\\opt\\ant-abcp\\bpaas_api.dll");
|
||||
// 2. 创建回调实例
|
||||
MyCallbackRsp callback = new MyCallbackRsp();
|
||||
AbcpInvoke.AbcpInit(appId, appVersion, json, callback);
|
||||
|
||||
String service_code = AliFaceConstants.SMILEVERIFYNIN_V1; //调用的组件编码:初始化
|
||||
return aliScanFaceService.startServiceIni(json, service_code);
|
||||
}
|
||||
|
||||
@GetMapping("test")
|
||||
public Result<?> test() {
|
||||
try {
|
||||
AliScanFaceDll.Dll dll = AliScanFaceDll.instance();
|
||||
/**
|
||||
* ABCP服务调用 刷脸支付服务
|
||||
* 文档地址:https://opendocs.alipay.com/iot/05e9ye
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("startServiceFaceSmilePay")
|
||||
Result<Object> startServiceFaceSmilePay() {
|
||||
//参数
|
||||
JSONObject params = new JSONObject(); //
|
||||
String json = params.toJSONString();
|
||||
|
||||
dll.bp_api_set_isv_lan(3);
|
||||
|
||||
JSONObject params = new JSONObject()
|
||||
.fluentPut("appId", "2021005138692337") //应用ID
|
||||
.fluentPut("merchantId", "2088641941653700") //签约商家的 PID,以 2088 开头
|
||||
.fluentPut("deviceNum", "P060003750")//商家机具终端编号,每台设备保持唯一
|
||||
.fluentPut("partnerId", "2088641941653700");//服务商的 PID
|
||||
String json = params.toJSONString();
|
||||
|
||||
AbcpInvoke.ABCPProcess msABCPProcess = new AbcpInvoke.ABCPProcess();
|
||||
AbcpInvoke.ABCPFinish msABCPFinish = new AbcpInvoke.ABCPFinish();
|
||||
dll.abcp_init(256, "2021005138692337", "1.0.0.0", json, msABCPProcess, msABCPFinish);
|
||||
|
||||
return Result.ok("成功");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return Result.error("失败");
|
||||
}
|
||||
String service_code = AliFaceConstants.SMILEPAY; //调用的组件编码:刷脸支付
|
||||
return aliScanFaceService.startServiceFaceSmilePay(json, service_code);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* ABCP服务停用
|
||||
* @param traceId
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("stopService")
|
||||
Result<Object> stopService(@RequestParam String traceId) {
|
||||
//参数
|
||||
JSONObject params = new JSONObject()
|
||||
.fluentPut("traceId",traceId)
|
||||
.fluentPut("service_code",AliFaceConstants.SMILEVERIFYNIN_V1); //
|
||||
String json = params.toJSONString();
|
||||
|
||||
String service_code = AliFaceConstants.SMILEVERIFYNIN_V1; //调用的组件编码
|
||||
return aliScanFaceService.stopService(json, service_code);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.dpkj.modules.scanface.ali.dll;
|
||||
|
||||
import com.dpkj.common.vo.Result;
|
||||
import com.sun.jna.Callback;
|
||||
import com.sun.jna.Native;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
@ -13,21 +14,6 @@ import java.util.concurrent.locks.ReentrantLock;
|
|||
@Slf4j
|
||||
public class AbcpInvoke {
|
||||
|
||||
/**
|
||||
* 获取 Dll 实例,同时注册 Dll 控件。
|
||||
*
|
||||
* @return AlipayDll 实例
|
||||
* @throws AliScanFaceDll.DllRegistrationException 如果注册控件失败,抛出此异常
|
||||
*/
|
||||
public static AliScanFaceDll.Dll instance() throws AliScanFaceDll.DllRegistrationException {
|
||||
try {
|
||||
return Native.load("AliScanFace", AliScanFaceDll.Dll.class);
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
log.info("[AliScanFaceDll][instance][阿里扫脸动态库] SDK注册失败:{}", e.getMessage());
|
||||
throw new AliScanFaceDll.DllRegistrationException("Failed to load AliScanFace library: ", e);
|
||||
}
|
||||
}
|
||||
|
||||
public interface CallbackStr {
|
||||
public void OnString(String result);
|
||||
}
|
||||
|
@ -47,10 +33,10 @@ public class AbcpInvoke {
|
|||
msAbcpNativeDll = (AbcpNativeDll) Native.load(fileDylib, AbcpNativeDll.class);
|
||||
} catch (Throwable e) {
|
||||
msAbcpNativeDll = null;
|
||||
System.out.printf("## ERROR : load. ## [%s][%s] %n", fileDylib, e.getMessage());
|
||||
log.error("## 本地库未加载,返回错误 ERROR : load. ## [%s][%s] %n", fileDylib, e.getMessage());
|
||||
}
|
||||
} else {
|
||||
System.out.printf("## ERROR : dylib NOT exist ## %s %n", fileDylib);
|
||||
log.error("## ERROR : dylib NOT exist ## %s %n", fileDylib);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -71,13 +57,13 @@ public class AbcpInvoke {
|
|||
try {
|
||||
msAbcpNativeDll.bp_api_set_isv_lan(3);// 0:cpp | 1:cshap | 2:jni | 3:jna
|
||||
} catch (Throwable e) {
|
||||
System.out.printf("## ERROR ## %s %n", e.getMessage());
|
||||
log.error("## ERROR ## %s %n", e.getMessage());
|
||||
}
|
||||
try {
|
||||
msAbcpNativeDll.abcp_init(seedId, appId, appVersion, json, msABCPProcess, msABCPFinish);
|
||||
} catch (Throwable e) {
|
||||
callback.OnFinish(3, "E69001", e.getMessage(), "");
|
||||
System.out.printf("## ERROR ## %s %n", e.getMessage());
|
||||
log.error("## ERROR ## %s %n", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -97,7 +83,7 @@ public class AbcpInvoke {
|
|||
msAbcpNativeDll.abcp_get_meta_info(seedId, appId, json, msABCPProcess, msABCPFinish);
|
||||
} catch (Throwable e) {
|
||||
callback.OnFinish(3, "E69001", e.getMessage(), "");
|
||||
System.out.printf("## ERROR ## %s %n", e.getMessage());
|
||||
log.error("## ERROR ## %s %n", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -109,16 +95,23 @@ public class AbcpInvoke {
|
|||
appId = FixNullString(appId); // involved null ptr!
|
||||
serviceCode = FixNullString(serviceCode); // involved null ptr!
|
||||
json = FixNullString(json); // involved null ptr!
|
||||
|
||||
Integer seedId;
|
||||
msLock.lock();
|
||||
Integer seedId = msSeedId++;
|
||||
msMapAbcpReponse.put(seedId, callback);
|
||||
msLock.unlock();
|
||||
try {
|
||||
seedId = msSeedId++;
|
||||
msMapAbcpReponse.put(seedId, callback);
|
||||
log.info("注册回调: seedId={}", seedId);
|
||||
} finally {
|
||||
msLock.unlock();
|
||||
}
|
||||
|
||||
try {
|
||||
log.info("调用 abcp_start_service: seedId={}, appId={}, serviceCode={}, jsonLength={}",seedId, appId, serviceCode, json.length());
|
||||
msAbcpNativeDll.abcp_start_service(seedId, appId, serviceCode, json, msABCPProcess, msABCPFinish);
|
||||
} catch (Throwable e) {
|
||||
log.error("##本地方法调用异常 ERROR ## %s %n", e.getMessage());
|
||||
callback.OnFinish(3, "E69001", e.getMessage(), "");
|
||||
System.out.printf("## ERROR ## %s %n", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -139,7 +132,7 @@ public class AbcpInvoke {
|
|||
msAbcpNativeDll.abcp_stop_service(seedId, appId, serviceCode, json, msABCPProcess, msABCPFinish);
|
||||
} catch (Throwable e) {
|
||||
callback.OnFinish(3, "E69001", e.getMessage(), "");
|
||||
System.out.printf("## ERROR ## %s %n", e.getMessage());
|
||||
log.error("## ERROR ## %s %n", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -160,7 +153,7 @@ public class AbcpInvoke {
|
|||
msAbcpNativeDll.abcp_zimid_init_for_test(seedId, metainfo, smileType, smileFlag, msABCPString);
|
||||
} catch (Throwable e) {
|
||||
callback.OnString("{}");
|
||||
System.out.printf("## ERROR ## %s %n", e.getMessage());
|
||||
log.error("## ERROR ## %s %n", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -180,7 +173,7 @@ public class AbcpInvoke {
|
|||
msAbcpNativeDll.abcp_call(seedId, appId, "SubscribeEvent", json, msABCPProcess, msABCPFinish);
|
||||
} catch (Throwable e) {
|
||||
callback.OnFinish(3, "E69001", e.getMessage(), "");
|
||||
System.out.printf("## ERROR ## %s %n", e.getMessage());
|
||||
log.error("## ERROR ## %s %n", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -200,7 +193,7 @@ public class AbcpInvoke {
|
|||
msAbcpNativeDll.abcp_call(seedId, appId, "UnsubscribeEvent", json, msABCPProcess, msABCPFinish);
|
||||
} catch (Throwable e) {
|
||||
callback.OnFinish(3, "E69001", e.getMessage(), "");
|
||||
System.out.printf("## ERROR ## %s %n", e.getMessage());
|
||||
log.error("## ERROR ## %s %n", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,79 +0,0 @@
|
|||
package com.dpkj.modules.scanface.ali.dll;
|
||||
|
||||
import com.sun.jna.Callback;
|
||||
import com.sun.jna.Library;
|
||||
import com.sun.jna.Native;
|
||||
import com.sun.jna.Pointer;
|
||||
import com.sun.jna.win32.DLLCallback;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @Auther: 萧道子
|
||||
* src/main/resources/win32-x86/AliScanFace.dll
|
||||
* @Date: 2025/4/16
|
||||
* @Description: 阿里扫脸程序
|
||||
*/
|
||||
@Slf4j
|
||||
public class AliScanFaceDll {
|
||||
|
||||
/**
|
||||
* 获取 Dll 实例,同时注册 Dll 控件。
|
||||
*
|
||||
* @return AlipayDll 实例
|
||||
* @throws DllRegistrationException 如果注册控件失败,抛出此异常
|
||||
*/
|
||||
public static Dll instance() throws DllRegistrationException {
|
||||
try {
|
||||
return Native.load("AliScanFace", Dll.class);
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
log.info("[AliScanFaceDll][instance][阿里扫脸动态库] SDK注册失败:{}", e.getMessage());
|
||||
throw new DllRegistrationException("Failed to load AliScanFace library: ", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 定义自定义异常类,用于表示注册控件时发生的错误
|
||||
*/
|
||||
public static class DllRegistrationException extends Exception {
|
||||
public DllRegistrationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public DllRegistrationException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public interface ProcessCallback extends DLLCallback {
|
||||
void invoke(Object obj);
|
||||
}
|
||||
|
||||
public interface FinishCallback extends DLLCallback {
|
||||
void invoke(Object obj);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 定义接口映射本地库中的函数。
|
||||
*/
|
||||
public interface Dll extends Library {
|
||||
void bp_api_set_isv_lan(Integer type_isv_language);// 0:cpp | 1:cshap | 2:jni | 3:jna
|
||||
|
||||
void abcp_init(Integer arg, String app_id, String app_version, String json_param, Callback on_process, Callback on_finish);
|
||||
|
||||
void abcp_start_service(Integer arg, String app_id, String service_code, String json_param, Callback on_process, Callback on_finish);
|
||||
|
||||
void abcp_stop_service(Integer arg, String app_id, String service_code, String json_param, Callback on_process, Callback on_finish);
|
||||
|
||||
void abcp_get_meta_info(Integer arg, String app_id, String json_param, Callback on_process, Callback on_finish);
|
||||
|
||||
void abcp_stop_smile(Integer arg, String app_id, String json_param, Callback on_process, Callback on_finish);
|
||||
|
||||
void abcp_zimid_init_for_test(Integer arg, String zimmetainfo, String smileType, String smileFlag, Callback callback);
|
||||
|
||||
void abcp_call(Integer arg, String app_id, String call_method, String json_param, Callback on_process, Callback on_finish);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,12 +1,47 @@
|
|||
package com.dpkj.modules.scanface.ali.service;
|
||||
|
||||
import com.dpkj.common.vo.Result;
|
||||
import com.dpkj.modules.scanface.ali.dll.AliScanFaceDll;
|
||||
|
||||
public interface IAliScanFaceService {
|
||||
|
||||
/**
|
||||
* 初始化,调用阿里的ABCP_SDK部署出来的代码中的API文件
|
||||
* 初始化
|
||||
Result<Object> iniAbcp() ;
|
||||
*/
|
||||
Result<Object> iniAbcp() throws AliScanFaceDll.DllRegistrationException;
|
||||
|
||||
/**
|
||||
* 初始化,调用阿里的ABCP_SDK部署出来的代码中的API文件
|
||||
* https://opendocs.alipay.com/iot/05e9ye#ABCP%E5%88%9D%E5%A7%8B%E5%8C%96
|
||||
*/
|
||||
Result<Object> iniAbcpAbsolute();
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* ABCP服务调用 刷脸去初始化服务
|
||||
* 接受调用者传入的参数信息,执行指定服务(service_code),然后通过回调返回服务结果。
|
||||
* https://opendocs.alipay.com/iot/05e9ye#ABCP%E6%9C%8D%E5%8A%A1%E8%B0%83%E7%94%A8
|
||||
* @param json 组装参数
|
||||
* @param service_code 所要调用的组件编码
|
||||
* @return
|
||||
*/
|
||||
Result<Object> startServiceIni(String json,String service_code);
|
||||
|
||||
/**
|
||||
* ABCP服务调用 :刷脸支付服务
|
||||
* @param json
|
||||
* @param service_code
|
||||
* @return
|
||||
*/
|
||||
Result<Object> startServiceFaceSmilePay(String json,String service_code);
|
||||
|
||||
|
||||
/**
|
||||
* ABCP服务停用
|
||||
* 终止正在进行中的某次(service_code + traceId)或某类(service_code)服务调用,然后通过回调返回服务终止结果
|
||||
* @param json 组装参数
|
||||
* @param service_code 组件编码
|
||||
* @return
|
||||
*/
|
||||
Result<Object> stopService(String json,String service_code);
|
||||
}
|
||||
|
|
|
@ -3,16 +3,22 @@ package com.dpkj.modules.scanface.ali.service.impl;
|
|||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.dpkj.common.vo.Result;
|
||||
import com.dpkj.modules.scanface.ali.config.AliFaceConfig;
|
||||
import com.dpkj.modules.scanface.ali.constants.AliFaceConstants;
|
||||
import com.dpkj.modules.scanface.ali.dll.AbcpInvoke;
|
||||
import com.dpkj.modules.scanface.ali.dll.AliScanFaceDll;
|
||||
import com.dpkj.modules.scanface.ali.service.IAliScanFaceService;
|
||||
import com.dpkj.modules.scanface.ali.vo.MyCallbackRsp;
|
||||
import com.sun.jna.Callback;
|
||||
import jodd.util.StringUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.thymeleaf.util.StringUtils;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/**
|
||||
* @Auther: 萧道子
|
||||
|
@ -32,60 +38,268 @@ public class AliScanFaceServiceImpl implements IAliScanFaceService {
|
|||
*
|
||||
* @return
|
||||
*/
|
||||
public Result<Object> iniAbcpAbsolute() {
|
||||
File dllFile = new File("C:\\opt\\ant-abcp\\bpaas_api.dll");
|
||||
System.out.println("DLL 存在: " + dllFile.exists());
|
||||
if(dllFile.exists()){
|
||||
//System.setProperty("java.library.path", "C:/opt/ant-abcp");
|
||||
// 加载DLL(直接指定绝对路径)
|
||||
System.load("C:/opt/ant-abcp/bpaas_api.dll");
|
||||
|
||||
JSONObject params = new JSONObject()
|
||||
.fluentPut("appId", aliFaceConfig.getAppId()) //应用ID
|
||||
.fluentPut("merchantId", aliFaceConfig.getMerchantId()) //签约商家的 PID,以 2088 开头
|
||||
.fluentPut("deviceNum", aliFaceConfig.getDeviceNum())//商家机具终端编号,每台设备保持唯一
|
||||
.fluentPut("partnerId", aliFaceConfig.getPartnerId());//服务商的 PID
|
||||
String json = params.toJSONString();
|
||||
// 指定支付宝LOT SDK的本地库路径
|
||||
AbcpInvoke.SetAPIPathFile("C:\\opt\\ant-abcp\\bpaas_api.dll");
|
||||
// 2. 创建回调实例
|
||||
MyCallbackRsp callback = new MyCallbackRsp();
|
||||
AbcpInvoke.AbcpInit(aliFaceConfig.getAppId(), aliFaceConfig.getAppVersion(), json, callback);
|
||||
}else{
|
||||
log.info("[AliScanFaceServiceImpl][iniAbcpAbsolute][55][C:/opt/ant-abcp/bpaas_api.dll 文件不存在]");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result<Object> iniAbcp() {
|
||||
public Result<Object> iniAbcpAbsolute() {
|
||||
try {
|
||||
log.info("[AliScanFaceServiceImpl][iniAbcpAli][26][ABCP初始化参数][appId:{}],[merchantId:{}],[deviceNum:{}],[partnerId:{}]",
|
||||
aliFaceConfig.getAppId(), aliFaceConfig.getMerchantId(), aliFaceConfig.getDeviceNum(), aliFaceConfig.getPartnerId());
|
||||
File dllFile = new File(aliFaceConfig.getDllPath());
|
||||
System.out.println("DLL 存在: " + dllFile.exists());
|
||||
if (dllFile.exists()) {
|
||||
|
||||
AliScanFaceDll.Dll dll = AliScanFaceDll.instance();
|
||||
dll.bp_api_set_isv_lan(3);// 0:cpp | 1:cshap | 2:jni | 3:jna
|
||||
//组装参数
|
||||
JSONObject params = new JSONObject()
|
||||
.fluentPut("appId", aliFaceConfig.getAppId()) //应用ID
|
||||
.fluentPut("merchantId", aliFaceConfig.getMerchantId()) //签约商家的 PID,以 2088 开头
|
||||
.fluentPut("deviceNum", aliFaceConfig.getDeviceNum())//商家机具终端编号,每台设备保持唯一
|
||||
.fluentPut("partnerId", aliFaceConfig.getPartnerId());//服务商的 PID
|
||||
String json = params.toJSONString();
|
||||
|
||||
JSONObject params = new JSONObject()
|
||||
.fluentPut("appId", aliFaceConfig.getAppId()) //应用ID
|
||||
.fluentPut("merchantId", aliFaceConfig.getMerchantId()) //签约商家的 PID,以 2088 开头
|
||||
.fluentPut("deviceNum", aliFaceConfig.getDeviceNum())//商家机具终端编号,每台设备保持唯一
|
||||
.fluentPut("partnerId", aliFaceConfig.getPartnerId());//服务商的 PID
|
||||
String json = params.toJSONString();
|
||||
//指定支付宝LOT SDK的本地库路径
|
||||
AbcpInvoke.SetAPIPathFile(aliFaceConfig.getDllPath());
|
||||
|
||||
//创建回调实例
|
||||
MyCallbackRsp callback = new MyCallbackRsp();
|
||||
|
||||
AbcpInvoke.ABCPProcess msABCPProcess = new AbcpInvoke.ABCPProcess();
|
||||
AbcpInvoke.ABCPFinish msABCPFinish = new AbcpInvoke.ABCPFinish();
|
||||
Integer seedId = AbcpInvoke.getMsSeedId();
|
||||
dll.abcp_init(seedId, aliFaceConfig.getAppId(), aliFaceConfig.getAppVersion(), json, msABCPProcess, msABCPFinish);
|
||||
//初始化
|
||||
AbcpInvoke.AbcpInit(aliFaceConfig.getAppId(), aliFaceConfig.getAppVersion(), json, callback);
|
||||
|
||||
log.info("[AliScanFaceServiceImpl][iniAbcp][49][ABCP初始化process] :{}", msABCPProcess.toString());
|
||||
log.info("[AliScanFaceServiceImpl][iniAbcp][49][ABCP初始化结果] :{}", msABCPFinish.toString());
|
||||
return Result.ok("成功");
|
||||
return Result.ok("支付宝ABCP初始化成功");
|
||||
} else {
|
||||
log.info("[AliScanFaceServiceImpl][iniAbcpAbsolute][55][{} :文件不存在]", aliFaceConfig.getDllPath());
|
||||
return Result.error("支付宝ABCP初始化失败:" + aliFaceConfig.getDllPath() + "不存在");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return Result.error("失败");
|
||||
return Result.error("初始化失败");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用相对路径调用ABCP_SDK部署出来的AbcpInvoke类
|
||||
*
|
||||
* @return
|
||||
* @Override public Result<Object> iniAbcp() {
|
||||
* try {
|
||||
* log.info("[AliScanFaceServiceImpl][iniAbcpAli][26][ABCP初始化参数][appId:{}],[merchantId:{}],[deviceNum:{}],[partnerId:{}]",
|
||||
* aliFaceConfig.getAppId(), aliFaceConfig.getMerchantId(), aliFaceConfig.getDeviceNum(), aliFaceConfig.getPartnerId());
|
||||
* <p>
|
||||
* AliScanFaceDll.Dll dll = AliScanFaceDll.instance();
|
||||
* dll.bp_api_set_isv_lan(3);// 0:cpp | 1:cshap | 2:jni | 3:jna
|
||||
* <p>
|
||||
* JSONObject params = new JSONObject()
|
||||
* .fluentPut("appId", aliFaceConfig.getAppId()) //应用ID
|
||||
* .fluentPut("merchantId", aliFaceConfig.getMerchantId()) //签约商家的 PID,以 2088 开头
|
||||
* .fluentPut("deviceNum", aliFaceConfig.getDeviceNum())//商家机具终端编号,每台设备保持唯一
|
||||
* .fluentPut("partnerId", aliFaceConfig.getPartnerId());//服务商的 PID
|
||||
* String json = params.toJSONString();
|
||||
* <p>
|
||||
* <p>
|
||||
* AbcpInvoke.ABCPProcess msABCPProcess = new AbcpInvoke.ABCPProcess();
|
||||
* AbcpInvoke.ABCPFinish msABCPFinish = new AbcpInvoke.ABCPFinish();
|
||||
* Integer seedId = AbcpInvoke.getMsSeedId();
|
||||
* dll.abcp_init(seedId, aliFaceConfig.getAppId(), aliFaceConfig.getAppVersion(), json, msABCPProcess, msABCPFinish);
|
||||
* <p>
|
||||
* log.info("[AliScanFaceServiceImpl][iniAbcp][49][ABCP初始化process] :{}", msABCPProcess.toString());
|
||||
* log.info("[AliScanFaceServiceImpl][iniAbcp][49][ABCP初始化结果] :{}", msABCPFinish.toString());
|
||||
* return Result.ok("成功");
|
||||
* } catch (Exception e) {
|
||||
* e.printStackTrace();
|
||||
* return Result.error("失败");
|
||||
* }
|
||||
* }
|
||||
*/
|
||||
|
||||
/**
|
||||
* ABCP服务调用 刷脸初始化服务
|
||||
* 接受调用者传入的参数信息,执行指定服务(service_code),然后通过回调返回服务结果。
|
||||
* https://opendocs.alipay.com/iot/05e9ye#ABCP%E6%9C%8D%E5%8A%A1%E8%B0%83%E7%94%A8
|
||||
* @param json 组装参数
|
||||
* @param service_code 所要调用的组件编码
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result<Object> startServiceIni(String json, String service_code) {
|
||||
try {
|
||||
String appId = aliFaceConfig.getAppId();
|
||||
|
||||
// 使用 CountDownLatch 实现线程同步
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
AtomicReference<Result<Object>> resultRef = new AtomicReference<>();
|
||||
|
||||
//获取返回数据
|
||||
AtomicReference<Integer> returnCode = new AtomicReference<>();
|
||||
AtomicReference<String> returnResult = 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调用刷脸初始化服务][code:{}][subCode:{}][subMsg:{}][result:{}]", code, subCode, subMsg, result);
|
||||
try {
|
||||
returnCode.set(code);
|
||||
returnResult.set(result);
|
||||
} finally {
|
||||
latch.countDown(); // 确保无论如何都释放锁
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void OnFinish(int code, String subCode, String subMsg, String result) {
|
||||
log.info("[AliScanFaceServiceImpl][OnFinish][128][code:{}][subCode:{}][subMsg:{}][result:{}]", code, subCode, subMsg, result);
|
||||
}
|
||||
};
|
||||
|
||||
//调用
|
||||
log.info("[AliScanFaceServiceImpl][startService][141][ABCP调用刷脸初始化服务-调用AbcpStartService参数][appId:{}][service_code:{}][json:{}] ", appId, service_code, json.toString());
|
||||
AbcpInvoke.AbcpStartService(appId, service_code, json, callbackRsp);
|
||||
|
||||
// 等待回调完成(设置超时避免死锁)
|
||||
boolean awaitSuccess = latch.await(10, TimeUnit.SECONDS);
|
||||
if (!awaitSuccess) {
|
||||
return Result.error("等待回调超时");
|
||||
} else {
|
||||
if (returnCode.get() == 0) {
|
||||
Map<String, String> res = new HashMap<>();
|
||||
JSONObject jsonObject = JSONObject.parseObject(returnResult.get());
|
||||
if (jsonObject.containsKey("traceId")) {
|
||||
res.put("traceId", jsonObject.getString("traceId"));
|
||||
resultRef.set(Result.ok("ABCP调用刷脸初始化服务成功", res));
|
||||
} else {
|
||||
return Result.ok("ABCP调用刷脸初始化服务失败,返回结果无traceId");
|
||||
}
|
||||
} else {
|
||||
return Result.error("ABCP调用刷脸初始化服务失败");
|
||||
}
|
||||
}
|
||||
|
||||
//结果返回
|
||||
return resultRef.get();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return Result.error("ABCP调用刷脸初始化服务失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ABCP服务调用 :刷脸支付服务
|
||||
*
|
||||
* @param json
|
||||
* @param service_code
|
||||
* @return
|
||||
*/
|
||||
public Result<Object> startServiceFaceSmilePay(String json, String service_code) {
|
||||
try {
|
||||
String appId = aliFaceConfig.getAppId();
|
||||
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
AtomicReference<Result<Object>> finalResultRef = new AtomicReference<>();
|
||||
|
||||
AbcpInvoke.CallbackRsp callbackRsp = new AbcpInvoke.CallbackRsp() {
|
||||
@Override
|
||||
public void OnProcess(int code, String subCode, String subMsg, String result) {
|
||||
log.info("[AliScanFaceServiceImpl][startServiceFaceSmilePay][OnProcess][188][ABCP调用刷脸支付服务-OnProcess][code:{}][subCode:{}][subMsg:{}][result:{}]", code, subCode, subMsg, result);
|
||||
if (code != 0) {
|
||||
finalResultRef.set(Result.error("流程中断: " + subMsg));
|
||||
latch.countDown(); // 失败时立即释放锁
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void OnFinish(int code, String subCode, String subMsg, String result) {
|
||||
log.info("[AliScanFaceServiceImpl][startServiceFaceSmilePay][OnFinish][128][ABCP调用刷脸支付服务-OnFinish][code:{}][subCode:{}][subMsg:{}][result:{}]", code, subCode, subMsg, result);
|
||||
try {
|
||||
if (code == 0 && StringUtil.isNotBlank(result)) {
|
||||
JSONObject resultJson = JSONObject.parseObject(result);
|
||||
if (resultJson.containsKey("ftoken")) {
|
||||
String ftoken = resultJson.getString("ftoken");
|
||||
finalResultRef.set(Result.ok("ABCP刷脸支付服务成功", ftoken));
|
||||
} else {
|
||||
finalResultRef.set(Result.error("ABCP刷脸支付服务结果中ftoken不存在"));
|
||||
}
|
||||
} else {
|
||||
finalResultRef.set(Result.error("ABCP刷脸支付服务失败: " + subMsg));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
finalResultRef.set(Result.error("ABCP刷脸支付服务结果解析异常: " + e.getMessage()));
|
||||
} finally {
|
||||
latch.countDown();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//调用
|
||||
log.info("[AliScanFaceServiceImpl][startServiceFaceSmilePay][141][ABCP调用刷脸初始化服务-调用AbcpStartService参数][appId:{}][service_code:{}][json:{}] ", appId, service_code, json.toString());
|
||||
AbcpInvoke.AbcpStartService(appId, service_code, json, callbackRsp);
|
||||
|
||||
// 等待回调完成(60秒超时)
|
||||
boolean awaitSuccess = latch.await(60, TimeUnit.SECONDS);
|
||||
if (!awaitSuccess) {
|
||||
return Result.error("等待结果超时");
|
||||
}
|
||||
|
||||
return finalResultRef.get();
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return Result.error("ABCP调用刷脸支付服务失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* ABCP服务停用
|
||||
* 终止正在进行中的某次(service_code + traceId)或某类(service_code)服务调用,然后通过回调返回服务终止结果
|
||||
* @param json 组装参数
|
||||
* @param service_code 组件编码
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public Result<Object> stopService(String json, String service_code) {
|
||||
try {
|
||||
String appId = aliFaceConfig.getAppId();
|
||||
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
AtomicReference<Result<Object>> resultRef = new AtomicReference<>();
|
||||
AtomicReference<Integer> returnCode = new AtomicReference<>();
|
||||
AtomicReference<String> returnResult = new AtomicReference<>();
|
||||
|
||||
AbcpInvoke.CallbackRsp callbackRsp = new AbcpInvoke.CallbackRsp() {
|
||||
@Override
|
||||
public void OnProcess(int code, String subCode, String subMsg, String result) {
|
||||
log.info("[AliScanFaceServiceImpl][261][ABCP服务停用-OnProcess][code:{}][subCode:{}][subMsg:{}][result:{}]", code, subCode, subMsg, result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void OnFinish(int code, String subCode, String subMsg, String result) {
|
||||
log.info("[AliScanFaceServiceImpl][OnFinish][266][ABCP服务停用-OnFinish] [code:{}][subCode:{}][subMsg:{}][result:{}]", code, subCode, subMsg, result);
|
||||
try {
|
||||
returnCode.set(code);
|
||||
returnResult.set(result);
|
||||
} finally {
|
||||
latch.countDown(); // 确保无论如何都释放锁
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//调用
|
||||
log.info("[AliScanFaceServiceImpl][stopService][222][调用AbcpTaskStopService参数][appId:{}][service_code:{}][json:{}] ", appId, service_code, json.toString());
|
||||
AbcpInvoke.AbcpTaskStopService(appId, service_code, json, callbackRsp);
|
||||
|
||||
// 等待回调完成(设置超时避免死锁)
|
||||
boolean awaitSuccess = latch.await(60, TimeUnit.SECONDS);
|
||||
if (!awaitSuccess) {
|
||||
return Result.error("等待回调超时");
|
||||
} else {
|
||||
if (returnCode.get() == 0) {
|
||||
System.out.println("停止结果:"+returnResult.get());
|
||||
} else {
|
||||
return Result.error("ABCP停止服务失败");
|
||||
}
|
||||
}
|
||||
|
||||
//结果返回
|
||||
return resultRef.get();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return Result.error("ABCP服务停用失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,28 +1,31 @@
|
|||
package com.dpkj.modules.scanface.ali.vo;
|
||||
|
||||
import com.dpkj.common.vo.Result;
|
||||
import com.dpkj.modules.scanface.ali.dll.AbcpInvoke;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @description: 自定义回调实现类
|
||||
* @author: Zhangxue
|
||||
* @time: 2025/4/15 20:41
|
||||
*/
|
||||
@Slf4j
|
||||
public class MyCallbackRsp implements AbcpInvoke.CallbackRsp {
|
||||
|
||||
@Override
|
||||
public void OnProcess(int code, String subCode, String subMsg, String result) {
|
||||
// 处理初始化过程中的中间状态(如进度更新)
|
||||
System.out.printf("[OnProcess] code=%d, subCode=%s, subMsg=%s, result=%s%n",
|
||||
code, subCode, subMsg, result);
|
||||
log.info("[MyCallbackRsp][OnProcess][18][OnProcess] code=%d, subCode=%s, subMsg=%s, result=%s%n", code, subCode, subMsg, result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void OnFinish(int code, String subCode, String subMsg, String result) {
|
||||
// 处理初始化完成结果
|
||||
if (code == 0) {
|
||||
System.out.println("初始化成功!");
|
||||
System.out.println("返回结果: " + result);
|
||||
log.info("[MyCallbackRsp][OnFinish][24] :{}", "初始化成功!");
|
||||
log.info("[MyCallbackRsp][OnFinish][27][返回结果:] :{}", result);
|
||||
} else {
|
||||
System.err.printf("初始化失败!错误码: %s, 错误信息: %s%n", subCode, subMsg);
|
||||
log.info("[MyCallbackRsp][OnFinish][29] :初始化失败!错误码: %s, 错误信息: %s%n", subCode, subMsg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package com.dpkj.modules.scanface.wx.dll;
|
||||
|
||||
import com.dpkj.modules.scanface.ali.dll.AliScanFaceDll;
|
||||
import com.sun.jna.Library;
|
||||
import com.sun.jna.Native;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
package com.dpkj.modules.scanface.wx.service.impl;
|
||||
|
||||
import com.dpkj.modules.scanface.ali.dll.AliScanFaceDll;
|
||||
import com.dpkj.modules.scanface.wx.config.WechatUrlConfig;
|
||||
import com.dpkj.modules.scanface.wx.config.WxMpProperties;
|
||||
import com.dpkj.modules.scanface.wx.dll.WxpayFaceSDKDll;
|
||||
|
|
|
@ -26,16 +26,22 @@ dpkj:
|
|||
#支付宝刷脸
|
||||
ali:
|
||||
face:
|
||||
#dll文件路径
|
||||
dll-path: C:/opt/ant-abcp/bpaas_api.dll
|
||||
#IOT 应用管理-appid
|
||||
app-id: 2021005138692337
|
||||
#IOT应用版本
|
||||
app-version: 1.0.0
|
||||
#签约商家的 PID,以 2088 开头,企业主体
|
||||
merchant-id: 2088641941653700
|
||||
#商家机具终端编号,每台设备保持唯一
|
||||
#商家机具终端编号,每台设备保持唯一【设备铭牌上的整机编码】
|
||||
device-num: P060003750
|
||||
#服务商的 PID
|
||||
partner-id: 2088641941653700
|
||||
# 刷脸支付服务-服务编码(serviceCode)
|
||||
service-code: BPaaSSmileVerifyNonInitV1
|
||||
# 核心入参 serviceId
|
||||
service-id: pay
|
||||
|
||||
# 自定义app参数
|
||||
app:
|
||||
|
|
|
@ -26,8 +26,10 @@ dpkj:
|
|||
#支付宝刷脸
|
||||
ali:
|
||||
face:
|
||||
#dll文件路径
|
||||
dll-path: C:/opt/ant-abcp/bpaas_api.dll
|
||||
#IOT 应用管理-appid
|
||||
app-id: 2021005138692337
|
||||
app-id: 2021005138656502
|
||||
#IOT应用版本
|
||||
app-version: 1.0.0
|
||||
#签约商家的 PID,以 2088 开头,企业主体
|
||||
|
@ -36,6 +38,8 @@ dpkj:
|
|||
device-num: P060003750
|
||||
#服务商的 PID
|
||||
partner-id: 2088641941653700
|
||||
# 核心入参 serviceId
|
||||
service-id: pay
|
||||
|
||||
# 自定义app参数
|
||||
app:
|
||||
|
|
Binary file not shown.
Loading…
Reference in New Issue