赞
踩
注意如果同时对接多个服务,每个服务的Client类是不同包下的
不要一个想到一个Client共用,单独创建,如果在同一个类下创建Client对象返回
注意Client所在的包,否则服务接不通
1. 阿里云官网
https://www.aliyun.com/
2. 进入官网直接登陆,没有账号注册一个即可
3.登陆完毕创建AccessKey
1.导入sdk依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.24</version>
</dependency>
2.发送短信代码
package com.aliyun.sample; import com.aliyun.tea.*; public class Sample { // com.aliyun.dysmsapi20170525.的类前缀可在项目中删除只保留类名 // 阿里云提供的实例是担心你倒错包所以给到全名 // 根据项目规范,将AccessKey和AccessKey Secret配置到配置文件中读取出来 // 根据自身规范对下面进行微改即可使用 // 手机效验次数等其他逻辑在发送短信代码之前完成即可 /** * 使用AK&SK初始化账号Client * @param accessKeyId * @param accessKeySecret * @return Client * @throws Exception */ public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception { com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config() // 必填,您的 AccessKey ID .setAccessKeyId("你的accessKeyId") // 必填,您的 AccessKey Secret .setAccessKeySecret("你的accessKeySecret"); // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi config.endpoint = "dysmsapi.aliyuncs.com"; return new com.aliyun.dysmsapi20170525.Client(config); } public static void main(String[] args_) throws Exception { java.util.List<String> args = java.util.Arrays.asList(args_); // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html com.aliyun.dysmsapi20170525.Client client = Sample.createClient(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")); com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest() // 此处签名使用阿里云测试则固定使用该值 // 自己申请后改成自己的配置 .setSignName("阿里云短信测试") // 模版code值,使用阿里云测试则固定使用该值 // 申请签名通过再去申请模版code,改成自己的code值 .setTemplateCode("SMS_154950909") // 手机号码 .setPhoneNumbers("13222222222") // 发送短信内容,转换成JSON字符串 // 例如: Map<String,Object> msg= new HashMap<>(); // msg.put("code":"1234"); // String sendMsg = JSON.toJSONString(msg); // setTemplateParam(sendMsg ) .setTemplateParam("{\"code\":\"1234\"}"); // 此处可配置连接超时时间等信息,根据需要自己配置即可 com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions(); try { // 复制代码运行请自行打印 API 的返回值 client.sendSmsWithOptions(sendSmsRequest, runtime); } catch (TeaException error) { // 如有需要,请打印 error com.aliyun.teautil.Common.assertAsString(error.message); } catch (Exception _error) { TeaException error = new TeaException(_error.getMessage(), _error); // 如有需要,请打印 error com.aliyun.teautil.Common.assertAsString(error.message); } } }
<!--自定义kv模版sdk依赖-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>documentautoml20221229</artifactId>
<version>1.0.11</version>
</dependency>
<!--ocrsdk依赖-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>ocr_api20210707</artifactId>
<version>1.2.0</version>
</dependency>
package com.cloud.agrobiz.sdk.aliyun.orc; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.aliyun.documentautoml20221229.models.PredictTemplateModelRequest; import com.aliyun.documentautoml20221229.models.PredictTemplateModelResponse; import com.aliyun.ocr_api20210707.Client; import com.aliyun.ocr_api20210707.models.RecognizeBusinessLicenseRequest; import com.aliyun.ocr_api20210707.models.RecognizeBusinessLicenseResponse; import com.aliyun.ocr_api20210707.models.RecognizeIdcardRequest; import com.aliyun.ocr_api20210707.models.RecognizeIdcardResponse; import com.aliyun.teaopenapi.models.Config; import com.aliyun.teautil.models.RuntimeOptions; import com.higsoft.core.sdk.exception.UnifyHandleError; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; /** * 阿里云ORC * @author chenwei * @version v1.0 **/ public class AliyunOrc { // 因为项目规范使用该方式封装,可根据自身项目规范从新提取封装 // 复制即可使用,通过图片保存路径url地址识别 private final static Logger log = LoggerFactory.getLogger(AliyunOrc.class); /** * 使用AK&SK初始化账号Client * @param accessKeyId * @param accessKeySecret * @return Client * @throws Exception */ public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception { Config config = new Config() // 必填,您的 AccessKey ID .setAccessKeyId(accessKeyId) // 必填,您的 AccessKey Secret .setAccessKeySecret(accessKeySecret); // Endpoint 请参考 https://api.aliyun.com/product/ocr-api config.endpoint = "ocr-api.cn-hangzhou.aliyuncs.com"; return new Client(config); } /** * 使用AK&SK初始化账号自定义AKClient * @param accessKeyId * @param accessKeySecret * @return Client * @throws Exception */ public static com.aliyun.documentautoml20221229.Client createAkClient(String accessKeyId, String accessKeySecret) throws Exception { Config config = new Config() // 必填,您的 AccessKey ID .setAccessKeyId(accessKeyId) // 必填,您的 AccessKey Secret .setAccessKeySecret(accessKeySecret); // Endpoint 请参考 https://api.aliyun.com/product/documentAutoml config.endpoint = "documentautoml.cn-beijing.aliyuncs.com"; return new com.aliyun.documentautoml20221229.Client(config); } /** * 通过营业执照url地址识别内容 * @author chenwei * @param url 营业执照url地址 * @date 2023/10/10 12:15 * @return Map<String, Object> */ public static Object orcBusinessLicenseByUrl(String accessKeyId, String accessKeySecre, String url) { try { Client client = createClient(accessKeyId, accessKeySecre); RecognizeBusinessLicenseRequest request = new RecognizeBusinessLicenseRequest().setUrl(url); RuntimeOptions runtime = new RuntimeOptions(); RecognizeBusinessLicenseResponse response = client.recognizeBusinessLicenseWithOptions(request, runtime); log.info(JSON.toJSONString(response)); if(StringUtils.isNotBlank(response.getBody().code)){ log.error("识别失败:{}", response.getBody()); throw new UnifyHandleError(response.getBody().getMessage()); } JSONObject jsonData = JSON.parseObject(response.getBody().getData()); Object data = jsonData.get("data"); log.info("识别到的数据:{}", data.toString()); return data; } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage()); throw new UnifyHandleError("识别失败,请稍后再试或联系管理员"); } } /** * 通过身份证url地址检测识别内容 * 开启图案检测以及身份证质量检测功能 * @author chenwei * @param url 身份证url地址 * @date 2023/10/10 12:19 * @return Map<String, Object> */ public static Object checkOrcCardByUrl(String accessKeyId, String accessKeySecre, String url) { try { Client client = createClient(accessKeyId, accessKeySecre); RecognizeIdcardRequest request = new RecognizeIdcardRequest() // 开启图案检测功能 .setOutputFigure(true) // 开启身份证质量检测功能 .setOutputQualityInfo(true) .setUrl(url); RuntimeOptions runtime = new RuntimeOptions(); RecognizeIdcardResponse response = client.recognizeIdcardWithOptions(request, runtime); log.info(JSON.toJSONString(response)); if(StringUtils.isNotBlank(response.getBody().code)){ log.error("识别失败:{}", response.getBody()); throw new UnifyHandleError(response.getBody().getMessage()); } JSONObject jsonData = JSON.parseObject(response.getBody().getData()); // 获取正面照 为空则获取背面照 Object faceData = JSON.parseObject(jsonData.get("data").toString()).get("face"); if(Objects.isNull(faceData)){ faceData = JSON.parseObject(jsonData.get("data").toString()).get("back").toString(); } Object cardInfo = JSON.parseObject(faceData.toString()).get("data"); log.info("识别到的数据:{}", cardInfo.toString()); return cardInfo; } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage()); throw new UnifyHandleError("识别失败,请稍后再试或联系管理员"); } } /** * 识别自定义模版内容,通过文件URL识别 * @author chenwei * @param taskId 配置自定义模版中任务id * @param url 文件url地址 * @date 2023/10/10 12:25 * @return Map<String, Object> */ public static List<Map<String, Object>> orcAkByUrl(String accessKeyId, String accessKeySecre, Long taskId, String url) { try { com.aliyun.documentautoml20221229.Client akClient = createAkClient(accessKeyId, accessKeySecre); PredictTemplateModelRequest request = new PredictTemplateModelRequest() .setTaskId(taskId) .setContent(url); // 根据需要自己超时以及重试机制 否则使用默认 RuntimeOptions runtime = new RuntimeOptions(); PredictTemplateModelResponse response = akClient.predictTemplateModelWithOptions(request, runtime); if(! StringUtils.equals(response.getBody().code, "200")){ log.error("识别失败:{}", response.getBody()); throw new UnifyHandleError(response.getBody().getMessage()); } List<Map<String, Object>> responseData = (List<Map<String, Object>>) response.getBody().getData().get("data"); List<Map<String, Object>> resultData = responseData.stream().map(item -> { Map<String, Object> fieldMap = new HashMap<>(); fieldMap.put("fieldName", item.get("fieldName")); fieldMap.put("fieldWordRaw", item.get("fieldWordRaw")); return fieldMap; }).collect(Collectors.toList()); log.info("识别解析后的数据:{}", resultData); return resultData; } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage()); throw new UnifyHandleError("识别失败,请稍后再试或联系管理员"); } } }
目前项目只用到身份证,营业执照和自定义模版,其他的可自行查看文档
开通步骤和之前一样,直接回到首页搜索实人认证,实人认证服务需要阿里云账号已经进行了企业认证才能开通,公司账号原因此处不做卡通演示,根据需求选择对应套餐即可
获取CertifyId文档地址
https://help.aliyun.com/zh/id-verification/developer-reference/initfaceverify-2?spm=a2c4g.11186623.0.i28
前端对接文档地址(uniapp)
https://help.aliyun.com/zh/id-verification/developer-reference/uniapp-integration-2?spm=a2c4g.11186623.0.i0
<!--阿里云实人认证-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>cloudauth20190307</artifactId>
<version>2.0.8</version>
</dependency>
package com.cloud.agrobiz.sdk.aliyun.authen; import com.alibaba.fastjson.JSON; import com.aliyun.cloudauth20190307.Client; import com.aliyun.cloudauth20190307.models.InitFaceVerifyRequest; import com.aliyun.cloudauth20190307.models.InitFaceVerifyResponse; import com.aliyun.tea.TeaException; import com.aliyun.teaopenapi.models.Config; import com.cloud.agrobiz.sdk.constant.AliyunAuthenConst; import com.cloud.agrobiz.sdk.dto.RealPersonAuthenDto; import com.cloud.agrobiz.sdk.utils.HigUUID; import com.higsoft.core.sdk.exception.UnifyHandleError; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.HashMap; import java.util.Map; /** * 阿里云实人认证 * @author chenwei * @version v1.0 **/ public class AliyunRealPersonAuthen { // 该代码复制可直接使用,自身项目规范问题使用该方式封装,可根据自身项目改成自己所需 private final static Logger log = LoggerFactory.getLogger(AliyunRealPersonAuthen.class); /** * 获取客户端对象 * @author chenwei * @param accessKeyId * @param accessKeySecret * @date 2023/10/11 10:05 * @return Client */ public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception { Config config = new Config(); // 您的AccessKey ID config.accessKeyId = accessKeyId; // 您的AccessKey Secret config.accessKeySecret = accessKeySecret; return new Client(config); } /** * 获取 CertifyId * @author chenwei * @param accessKeyId 阿里云账号keyId * @param accessKeySecret 阿里云账号秘钥 * @param realPersonAuthenDto 前端传入参数字符串 RealPersonAuthenDto * @date 2023/10/11 14:34 * @return Map<String, Object> */ public static Map<String, Object> getCertifyId(String accessKeyId, String accessKeySecret, String realPersonAuthenDto) { // 获取基本参数数据并转为对象 RealPersonAuthenDto authenDto = JSON.parseObject(realPersonAuthenDto, RealPersonAuthenDto.class); // 获取前端传入的mateInfo数据并转为对象 String mateInfoStr = JSON.parseObject(realPersonAuthenDto).get("mateInfo").toString(); // 用户的真实姓名。 String certName = authenDto.getCertName(); // 用户的证件号码。 String certNo = authenDto.getCertNo(); // MetaInfo环境参数,需要通过客户端SDK获取。 // metaInfo = "{\"zimVer\":\"3.0.0\",\"appVersion\":\"1\",\"bioMetaInfo\":\"4.1.0:11501568,0\",\"appName\":\"com.aliyun.antcloudauth\",\"deviceType\":\"ios\",\"osVersion\":\"iOS10.3.2\",\"apdidToken\":\"\",\"deviceModel\":\"iPhone9,1\"}"; try { Client client = createClient(accessKeyId, accessKeySecret); InitFaceVerifyRequest requestInitFaceVerify = new InitFaceVerifyRequest(); requestInitFaceVerify.sceneId = AliyunAuthenConst.SCENE_ID; // 客户服务端自定义的业务唯一标识,用于后续定位排查问题使用。值最长为32位长度的字母数字组合,请确保唯一。 requestInitFaceVerify.outerOrderNo = HigUUID.UUID32(); requestInitFaceVerify.productCode = AliyunAuthenConst.PRODUCT_CODE; requestInitFaceVerify.model = AliyunAuthenConst.MODEL; requestInitFaceVerify.certType = AliyunAuthenConst.CERT_TYPE; requestInitFaceVerify.certName = certName; requestInitFaceVerify.certNo = certNo; requestInitFaceVerify.metaInfo = mateInfoStr; // 自定义的用户ID,请保持唯一。 requestInitFaceVerify.userId = HigUUID.UUID16(); InitFaceVerifyResponse responseInitFaceVerify = client.initFaceVerify(requestInitFaceVerify); if(! StringUtils.equals(responseInitFaceVerify.body.code, "200")){ throw new UnifyHandleError(responseInitFaceVerify.body.message); } String certifyId = responseInitFaceVerify.body.resultObject.certifyId; Map<String, Object> resultMap = new HashMap<>(); resultMap.put("certifyId", certifyId); return resultMap; } catch (TeaException error) { log.error(error.message); throw new UnifyHandleError(error.message); } catch (Exception _error) { TeaException error = new TeaException(_error.getMessage(), _error); log.error(error.message); throw new UnifyHandleError(error.message); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。