|
@@ -0,0 +1,180 @@
|
|
|
+package com.ematou.wxbase.service.impl;
|
|
|
+
|
|
|
+import com.ematou.wxbase.common.constant.TokenType;
|
|
|
+import com.ematou.wxbase.common.web.R;
|
|
|
+import com.ematou.wxbase.entity.EidMerch;
|
|
|
+import com.ematou.wxbase.entity.MerchApp;
|
|
|
+import com.ematou.wxbase.entity.MerchInfo;
|
|
|
+import com.ematou.wxbase.entity.OperationRecord;
|
|
|
+import com.ematou.wxbase.entity.dto.EidTokenRequestDTO;
|
|
|
+import com.ematou.wxbase.exception.Assert;
|
|
|
+import com.ematou.wxbase.exception.ServiceException;
|
|
|
+import com.ematou.wxbase.mapper.EidMerchMapper;
|
|
|
+import com.ematou.wxbase.mapper.MerchAppMapper;
|
|
|
+import com.ematou.wxbase.mapper.MerchInfoMapper;
|
|
|
+import com.ematou.wxbase.service.EidService;
|
|
|
+import com.ematou.wxbase.service.OperationRecordService;
|
|
|
+import com.tencentcloudapi.common.Credential;
|
|
|
+import com.tencentcloudapi.common.exception.TencentCloudSDKException;
|
|
|
+import com.tencentcloudapi.common.profile.ClientProfile;
|
|
|
+import com.tencentcloudapi.common.profile.HttpProfile;
|
|
|
+import com.tencentcloudapi.faceid.v20180301.FaceidClient;
|
|
|
+import com.tencentcloudapi.faceid.v20180301.models.GetEidTokenRequest;
|
|
|
+import com.tencentcloudapi.faceid.v20180301.models.GetEidTokenResponse;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.List;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+/**
|
|
|
+ * E证通业务实现类
|
|
|
+ *
|
|
|
+ * @author frankeleyn
|
|
|
+ * @email lvjian@qhdswl.com
|
|
|
+ * @date 2023/2/18 16:56
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class EidServiceImpl implements EidService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private MerchInfoMapper merchInfoMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private MerchAppMapper merchAppMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private EidMerchMapper eidMerchMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private OperationRecordService recordService;
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取E证通 Token
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public R<String> getEidToken(EidTokenRequestDTO requestDTO) {
|
|
|
+ // 校验参数
|
|
|
+ validateParam(requestDTO);
|
|
|
+ // 校验商户
|
|
|
+ validateMerch(requestDTO.getMerchSn());
|
|
|
+ // 校验商户应用
|
|
|
+ validateMerchApp(requestDTO.getMerchSn(), requestDTO.getAppCode());
|
|
|
+ // 查询E证通商户配置
|
|
|
+ EidMerch eidMerch = eidMerchMapper.selectByMerchSn(requestDTO.getMerchSn());
|
|
|
+ Assert.notNull(eidMerch, "商户E证通配置不存在!");
|
|
|
+ // 请求E证通 token
|
|
|
+ String eidToken = reqEidToken(eidMerch);
|
|
|
+ // 插入操作记录
|
|
|
+ insertOptRecord(requestDTO);
|
|
|
+ // TODO: 插入E证通 token 记录
|
|
|
+
|
|
|
+
|
|
|
+ return R.success(null);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取E证通结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public R<?> getEidResult() {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验参数
|
|
|
+ *
|
|
|
+ * @param requestDTO
|
|
|
+ */
|
|
|
+ private void validateParam(EidTokenRequestDTO requestDTO) {
|
|
|
+ Assert.notEmpty(requestDTO.getMerchSn(), "商户号不能为空!");
|
|
|
+ Assert.notEmpty(requestDTO.getAppCode(), "应用编码不能为空!");
|
|
|
+ Assert.notEmpty(requestDTO.getUserName(), "用户名不能为空!");
|
|
|
+ Assert.notEmpty(requestDTO.getIdCard(), "用户身份证不能为空!");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验商户应用
|
|
|
+ *
|
|
|
+ * @param merchSn
|
|
|
+ * @param appCode
|
|
|
+ */
|
|
|
+ private void validateMerchApp(String merchSn, String appCode) {
|
|
|
+ MerchApp query = new MerchApp();
|
|
|
+ query.setMerchSn(merchSn);
|
|
|
+ query.setAppCode(appCode);
|
|
|
+ List<MerchApp> merchApps = merchAppMapper.selectByCondition(query);
|
|
|
+ Assert.notTrue(merchApps.size() < 1, "商户应用不存在!");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验商户
|
|
|
+ *
|
|
|
+ * @param merchSn
|
|
|
+ */
|
|
|
+ private void validateMerch(String merchSn) {
|
|
|
+ MerchInfo query = new MerchInfo();
|
|
|
+ query.setMerchSn(merchSn);
|
|
|
+ MerchInfo merchInfo = merchInfoMapper.selectOne(query);
|
|
|
+ Assert.notNull(merchInfo, "商户信息不存在!");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取E证通 token
|
|
|
+ * @param eidMerch
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String reqEidToken(EidMerch eidMerch) {
|
|
|
+ try {
|
|
|
+ // 创建访问凭据
|
|
|
+ Credential cred = new Credential(eidMerch.getSecretId(), eidMerch.getSecretKey());
|
|
|
+ // 实例化一个http选项,可选的,没有特殊需求可以跳过
|
|
|
+ HttpProfile httpProfile = new HttpProfile();
|
|
|
+ httpProfile.setEndpoint("faceid.tencentcloudapi.com");
|
|
|
+ // 实例化一个client选项,可选的,没有特殊需求可以跳过
|
|
|
+ ClientProfile clientProfile = new ClientProfile();
|
|
|
+ clientProfile.setHttpProfile(httpProfile);
|
|
|
+ // 实例化要请求产品的client对象,clientProfile是可选的
|
|
|
+ FaceidClient client = new FaceidClient(cred, "", clientProfile);
|
|
|
+ // 实例化一个请求对象,每个接口都会对应一个request对象
|
|
|
+ GetEidTokenRequest req = new GetEidTokenRequest();
|
|
|
+
|
|
|
+ // 返回的resp是一个GetEidTokenResponse的实例,与请求对象对应
|
|
|
+ GetEidTokenResponse resp = client.GetEidToken(req);
|
|
|
+ // 输出json格式的字符串回包
|
|
|
+ System.out.println(GetEidTokenResponse.toJsonString(resp));
|
|
|
+ // 获取 EidToken
|
|
|
+ String eidToken = resp.getEidToken();
|
|
|
+ eidToken = UUID.randomUUID().toString().replace("-", "");
|
|
|
+ // 返回 token
|
|
|
+ return eidToken;
|
|
|
+ } catch (TencentCloudSDKException e) {
|
|
|
+ log.error("腾讯云 SDK 人脸核验异常.", e);
|
|
|
+ throw new ServiceException("人脸核验异常!");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 插入操作记录
|
|
|
+ *
|
|
|
+ * @param requestDTO
|
|
|
+ */
|
|
|
+ private void insertOptRecord(EidTokenRequestDTO requestDTO) {
|
|
|
+ OperationRecord operationRecord =
|
|
|
+ OperationRecord.builder()
|
|
|
+ .appCode(requestDTO.getAppCode())
|
|
|
+ .merchSn(requestDTO.getMerchSn())
|
|
|
+ .tokenType(TokenType.EID_TOKEN.getItem())
|
|
|
+ .build();
|
|
|
+ // 插入操作记录
|
|
|
+ recordService.insertRecord(operationRecord);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|