package me.zhengjie.base.util; import cfca.sadk.algorithm.common.PKIException; import cfca.sadk.util.Base64; import cfca.trustsign.common.vo.cs.*; import cfca.trustsign.common.vo.request.tx3.*; import cfca.trustsign.common.vo.response.ErrorResVO; import cfca.trustsign.common.vo.response.tx3.*; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import me.zhengjie.application.admin.service.DictDetailService; import me.zhengjie.sign.connector.HttpConnector; import me.zhengjie.sign.converter.JsonObjectMapper; import me.zhengjie.sign.util.PdfUtil; import me.zhengjie.sign.util.SecurityUtil; import me.zhengjie.sign.util.TimeUtil; import me.zhengjie.utils.SpringContextHolder; import org.springframework.stereotype.Component; import java.io.File; import java.io.InputStream; import java.util.List; import java.util.Map; @Component @Slf4j @RequiredArgsConstructor public class CFCACertUtil { private final HttpConnector httpConnector; private static final int PROXY_SIGN_ON = 1; private static final String PROJECT_CODE = "005"; private static final String AUTH_MODE = "公安部"; /** * 注册安心签企业用户 * * @param enterprise 企业信息 * @param enterpriseTransactor 企业经办人信息 * @return 安心签用户ID * @throws Exception */ public String registerCompany(EnterpriseVO enterprise, EnterpriseTransactorVO enterpriseTransactor) throws Exception { enterprise.setAuthenticationTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14)); enterprise.setAuthenticationMode(AUTH_MODE); enterprise.setIdentTypeCode("N"); enterpriseTransactor.setIdentTypeCode("0"); Tx3002ReqVO tx3002ReqVO = new Tx3002ReqVO(); tx3002ReqVO.setHead(HeadVO.builder().txTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14)).build()); tx3002ReqVO.setEnterprise(enterprise); tx3002ReqVO.setEnterpriseTransactor(enterpriseTransactor); String response = requestAPI(tx3002ReqVO, "3002"); if (!isSucess(response)) { ErrorResVO errorResVO = new ObjectMapper().readValue(response, ErrorResVO.class); System.out.println(errorResVO.getErrorCode() + ": " + errorResVO.getErrorMessage()); throw new Exception("安心签服务请求失败"); } Tx3002ResVO tx3002ResVO = new ObjectMapper().readValue(response, Tx3002ResVO.class); return tx3002ResVO.getEnterprise().getUserId(); } /** * 注册安心签个人用户 * * @throws Exception */ public String registerPerson(PersonVO person) throws Exception { person.setAuthenticationTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14)); person.setAuthenticationMode(AUTH_MODE); Tx3001ReqVO tx3001ReqVO = new Tx3001ReqVO(); tx3001ReqVO.setHead(HeadVO.builder().txTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14)).build()); tx3001ReqVO.setPerson(person); String response = requestAPI(tx3001ReqVO, "3001"); if (!isSucess(response)) { ErrorResVO errorResVO = new ObjectMapper().readValue(response, ErrorResVO.class); System.out.println(errorResVO.getErrorCode() + ": " + errorResVO.getErrorMessage()); throw new Exception("安心签服务请求失败"); } Tx3001ResVO tx3001ResVO = new ObjectMapper().readValue(response, Tx3001ResVO.class); return tx3001ResVO.getPerson().getUserId(); } /**TODO 暂时没有用到 * 查询证书信息 * * @param cert * @return * @throws Exception */ public CertVO queryCertificate(CertVO cert) throws Exception { Tx3311ReqVO tx3311ReqVO = new Tx3311ReqVO(); tx3311ReqVO.setHead(HeadVO.builder().txTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14)).build()); tx3311ReqVO.setCert(cert); String response = requestAPI(tx3311ReqVO, "3311"); if (!isSucess(response)) { ErrorResVO errorResVO = new ObjectMapper().readValue(response, ErrorResVO.class); System.out.println(errorResVO.getErrorCode() + ": " + errorResVO.getErrorMessage()); throw new Exception("安心签服务请求失败"); } Tx3311ResVO tx3311ResVO = new ObjectMapper().readValue(response, Tx3311ResVO.class); return tx3311ResVO.getCert(); } // 9. 修改印章3012 // 此接口用于客户平台修改某用户的印章图片。客户平台可以调用此接口修改某用户指定印章ID的印章图片。 public void updateSealWithContent(String userId, String sealId, String content) throws Exception { HeadVO head = new HeadVO(); head.setTxTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14)); SealVO sealVO = new SealVO(); sealVO.setSealId(sealId); sealVO.setImageData(content); SealUpdateVO sealUpdateVO = new SealUpdateVO(); sealUpdateVO.setUserId(userId); sealUpdateVO.setSeal(sealVO); Tx3012ReqVO tx3012ReqVO = new Tx3012ReqVO(); tx3012ReqVO.setHead(head); tx3012ReqVO.setSealUpdate(sealUpdateVO); String response = requestAPI(tx3012ReqVO, "3012"); if (!isSucess(response)) { ErrorResVO errorResVO = new ObjectMapper().readValue(response, ErrorResVO.class); System.out.println(errorResVO.getErrorCode() + ": " + errorResVO.getErrorMessage()); throw new Exception("安心签服务请求失败"); } } /** * 修改签章 * * @param userId * @param sealId * @param b * @throws Exception */ public void updateSeal(String userId, String sealId, byte[] b) throws Exception { // 这里将图片的路径下载文件 // byte[] b=DownloadUtils.downloadFile(filePath); String content = Base64.toBase64String(b); updateSealWithContent(userId, sealId, content); } /** * 为指定用户添加印章 * * @param userId * @param content * @return 返回印章ID * @throws Exception */ public String addSealWithContent(String userId, String content) throws Exception { HeadVO head = new HeadVO(); head.setTxTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14)); SealAddVO sealAddVO = new SealAddVO(); sealAddVO.setUserId(userId); SealVO sealVO = new SealVO(); sealVO.setImageData(content); sealAddVO.setSeal(sealVO); Tx3011ReqVO tx3011ReqVO = new Tx3011ReqVO(); tx3011ReqVO.setHead(head); tx3011ReqVO.setSealAdd(sealAddVO); String response = requestAPI(tx3011ReqVO, "3011"); if (!isSucess(response)) { ErrorResVO errorResVO = new ObjectMapper().readValue(response, ErrorResVO.class); System.out.println(errorResVO.getErrorCode() + ": " + errorResVO.getErrorMessage()); throw new Exception("安心签服务请求失败"); } Tx3011ResVO tx3011ResVO = new ObjectMapper().readValue(response, Tx3011ResVO.class); return tx3011ResVO.getSealAdd().getSeal().getSealId(); } /** * 为指定用户添加印章 * * @param userId * @param b * @return 返回印章ID * @throws Exception */ public String addSeal(String userId, byte[] b) throws Exception { String content = Base64.toBase64String(b); return addSealWithContent(userId, content); } /** * 对指定用户发送授权验证码 * * @param userId * @throws Exception */ public boolean sendAuthMessage(String userId) throws Exception { Tx3101ReqVO tx3101ReqVO = new Tx3101ReqVO(); HeadVO head = new HeadVO(); head.setTxTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14)); ProxySignVO proxySignVO = new ProxySignVO(); proxySignVO.setUserId(userId); proxySignVO.setProjectCode(PROJECT_CODE); tx3101ReqVO.setHead(head); tx3101ReqVO.setProxySign(proxySignVO); String response = requestAPI(tx3101ReqVO, "3101"); if (!isSucess(response)) { ErrorResVO errorResVO = new ObjectMapper().readValue(response, ErrorResVO.class); System.out.println(errorResVO.getErrorCode() + ": " + errorResVO.getErrorMessage()); if (!"60030401".equals(errorResVO.getErrorCode())) { throw new Exception("安心签服务请求失败"); } else { return true; } } return false; } /** * 确认授权验证码 * * @param userId * @param checkCode * @throws Exception */ public void confirmAuthMessage(String userId, String checkCode) throws Exception { Tx3102ReqVO tx3102ReqVO = new Tx3102ReqVO(); HeadVO head = new HeadVO(); head.setTxTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14)); ProxySignVO proxySignVO = new ProxySignVO(); proxySignVO.setUserId(userId); proxySignVO.setProjectCode(PROJECT_CODE); proxySignVO.setCheckCode(checkCode); tx3102ReqVO.setHead(head); tx3102ReqVO.setProxySign(proxySignVO); String response = requestAPI(tx3102ReqVO, "3102"); if (!isSucess(response)) { ErrorResVO errorResVO = new ObjectMapper().readValue(response, ErrorResVO.class); System.out.println(errorResVO.getErrorCode() + ": " + errorResVO.getErrorMessage()); throw new Exception("安心签服务请求失败"); } } /** * 上传签署合同 * * @param notaryUser 公证员签名信息 * @param notaryOffice 公证处签名信息 * @param file 签名文件 * @return 返回合同编号 * @throws Exception */ public String uploadSignContract(UploadSignInfoVO notaryUser, UploadSignInfoVO notaryOffice, InputStream file, String fileName) throws Exception { DictDetailService dictDetailService = SpringContextHolder.getBean(DictDetailService.class); Map map = dictDetailService.getValueByName("signed_position"); // signed 公证员签名 // 公证员签名 String signed = map.get("signed"); JSONObject json = JSONObject.parseObject(signed); String keyword = json.getString("keyword"); String offsetX = json.getString("offsetX"); String offsetY = json.getString("offsetY"); String width = json.getString("width"); String height = json.getString("height"); SignKeywordVO keywordNotaryUser = getSignKeywork(keyword, offsetX, offsetY, width, height); setSignInfoDefault(notaryUser, keywordNotaryUser); // 盖章 String stamp = map.get("stamp"); json = JSONObject.parseObject(stamp); keyword = json.getString("keyword"); offsetX = json.getString("offsetX"); offsetY = json.getString("offsetY"); width = json.getString("width"); height = json.getString("height"); SignKeywordVO keywordNotaryOffice = getSignKeywork(keyword, offsetX, offsetY, width, height); setSignInfoDefault(notaryOffice, keywordNotaryOffice); UploadSignInfoVO[] signInfos = { notaryUser, notaryOffice }; UploadContractVO uploadContract = new UploadContractVO(); uploadContract.setSignInfos(signInfos); uploadContract.setContractTypeCode("WT"); uploadContract.setContractName("赋强公证"); HeadVO head = new HeadVO(); head.setTxTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14)); Tx3203ReqVO tx3203ReqVO = new Tx3203ReqVO(); tx3203ReqVO.setHead(head); tx3203ReqVO.setUploadContract(uploadContract); String response = requestAPI(tx3203ReqVO, "3203", file, fileName); if (!isSucess(response)) { ErrorResVO errorResVO = new ObjectMapper().readValue(response, ErrorResVO.class); System.out.println(errorResVO.getErrorCode() + ": " + errorResVO.getErrorMessage()); throw new Exception("安心签服务请求失败"); } Tx3203ResVO tx3203ResVO = new ObjectMapper().readValue(response, Tx3203ResVO.class); return tx3203ResVO.getContract().getContractNo(); } // /** // * 上传签署合同 // * // * @param user // * @param file // * @return // * @throws Exception // */ // public static String uploadSignContract(UploadSignInfoVO user, InputStream file, String fileName) throws Exception { // // SignKeywordVO keywordUser = getSignKeywork(Constants.signed, "70", "0", "160", "60"); // setSignInfoDefault(user, keywordUser); // // UploadSignInfoVO[] signInfos = { user }; // UploadContractVO uploadContract = new UploadContractVO(); // uploadContract.setSignInfos(signInfos); // uploadContract.setContractTypeCode("WT"); // uploadContract.setContractName("赋强公证"); // // HeadVO head = new HeadVO(); // head.setTxTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14)); // // Tx3203ReqVO tx3203ReqVO = new Tx3203ReqVO(); // tx3203ReqVO.setHead(head); // tx3203ReqVO.setUploadContract(uploadContract); // // 读取文件的流,TODO // String response = requestAPI(tx3203ReqVO, "3203", file, fileName); // if (!isSucess(response)) { // ErrorResVO errorResVO = new ObjectMapper().readValue(response, ErrorResVO.class); // System.out.println(errorResVO.getErrorCode() + ": " + errorResVO.getErrorMessage()); // throw new Exception("安心签服务请求失败"); // } // Tx3203ResVO tx3203ResVO = new ObjectMapper().readValue(response, Tx3203ResVO.class); // return tx3203ResVO.getContract().getContractNo(); // } /** * 下载签名公证书 * * @param filePath * @param contractNo * @throws Exception */ public String download(String filePath, String contractNo) throws Exception { byte[] fileBtye = httpConnector .getFile("platId/" + httpConnector.getPlatID() + "/contractNo/" + contractNo + "/downloading"); String contentType; String suffix; if (PdfUtil.isPdf(fileBtye)) { contentType = "application/pdf"; suffix = ".pdf"; } else { contentType = "application/ofd"; suffix = ".ofd"; } String fullFilePath = filePath + contractNo + suffix; // 上传文件路径 log.info("上传文件的目录:" + fullFilePath); FileUploadUtil.uploadFile(fullFilePath, contentType, fileBtye); return fullFilePath; } /** * 发送安心签API请求 * * @param txReqVO * @param txCode * @return * @throws Exception */ private String requestAPI(Object txReqVO, String txCode) throws Exception { return requestAPI(txReqVO, txCode, null); } /** * 发送安心签API请求 * * @param txReqVO * @param txCode * @throws PKIException */ private String requestAPI(Object txReqVO, String txCode, File file) throws Exception { String req = new JsonObjectMapper().writeValueAsString(txReqVO); System.out.println("req:" + req); String signature = SecurityUtil.p7SignMessageDetach(httpConnector.getKeyStorePath(), httpConnector.getKeyStorePassword(), httpConnector.getAlias(), req); String res = ""; if ("3203".equals(txCode)) { res = httpConnector.post("platId/" + httpConnector.getPlatID() + "/txCode/" + txCode + "/transaction", req, signature, file); } else { res = httpConnector.post("platId/" + httpConnector.getPlatID() + "/txCode/" + txCode + "/transaction", req, signature); } System.out.println("res:" + res); return res; } /** * 发送安心签API请求 * * @param txReqVO * @param txCode * @throws PKIException */ private String requestAPI(Object txReqVO, String txCode, InputStream file, String fileName) throws Exception { String req = new JsonObjectMapper().writeValueAsString(txReqVO); System.out.println("req:" + req); String signature = SecurityUtil.p7SignMessageDetach(httpConnector.getKeyStorePath(), httpConnector.getKeyStorePassword(), httpConnector.getAlias(), req); String res = ""; if ("3203".equals(txCode)) { res = httpConnector.post("platId/" + httpConnector.getPlatID() + "/txCode/" + txCode + "/transaction", req, signature, file, fileName); } else { res = httpConnector.post("platId/" + httpConnector.getPlatID() + "/txCode/" + txCode + "/transaction", req, signature); } System.out.println("res:" + res); return res; } /** * 多个人签证,最多三个人 ** * @param notaryUserInfos 签名多个,包含了可能是借款人,抵押人,担保人 * @param notaryUser 公证员签名信息 * @param seal 盖章的位置 * @param file 文件的 * @param fileName 文件名称 * @return 返回合同编号 * @throws Exception */ public String uploadMultiSignContract(List notaryUserInfos, UploadSignInfoVO notaryUser, UploadSignInfoVO seal, InputStream file, String fileName) throws Exception { DictDetailService dictDetailService = SpringContextHolder.getBean(DictDetailService.class); Map map = dictDetailService.getValueByName("signed_position"); int i = 0; String signature = map.get("signature"); JSONObject json = JSONObject.parseObject(signature); int start = json.getIntValue("start"); int step = json.getIntValue("step"); String keyword = json.getString("keyword"); String offsetY = json.getString("offsetY"); String width = json.getString("width"); String height = json.getString("height"); for (UploadSignInfoVO user : notaryUserInfos) { String x = String.valueOf(start + i * step); i++; SignKeywordVO keywordNotaryUser = getSignKeywork(keyword, x, offsetY, width, height); setSignInfoDefault(user, keywordNotaryUser); } // 公证员签名 String signed = map.get("signed"); json = JSONObject.parseObject(signed); keyword = json.getString("keyword"); String offsetX = json.getString("offsetX"); offsetY = json.getString("offsetY"); width = json.getString("width"); height = json.getString("height"); // 公证员签名 if (notaryUser != null) { SignKeywordVO keywordNotaryUser = getSignKeywork(keyword, offsetX, offsetY, width, height); setSignInfoDefault(notaryUser, keywordNotaryUser); notaryUserInfos.add(notaryUser); } // 盖章 String stamp = map.get("stamp"); json = JSONObject.parseObject(stamp); keyword = json.getString("keyword"); offsetX = json.getString("offsetX"); offsetY = json.getString("offsetY"); width = json.getString("width"); height = json.getString("height"); if (seal != null) { SignKeywordVO keywordNotaryOffice = getSignKeywork(keyword, offsetX, offsetY, width, height); setSignInfoDefault(seal, keywordNotaryOffice); notaryUserInfos.add(seal); } UploadContractVO uploadContract = new UploadContractVO(); uploadContract.setSignInfos(notaryUserInfos.toArray(new UploadSignInfoVO[0])); uploadContract.setContractTypeCode("WT"); uploadContract.setContractName("赋强公证"); HeadVO head = new HeadVO(); head.setTxTime(TimeUtil.getCurrentTime(TimeUtil.FORMAT_14)); Tx3203ReqVO tx3203ReqVO = new Tx3203ReqVO(); tx3203ReqVO.setHead(head); tx3203ReqVO.setUploadContract(uploadContract); String response = requestAPI(tx3203ReqVO, "3203", file, fileName); if (!isSucess(response)) { ErrorResVO errorResVO = new ObjectMapper().readValue(response, ErrorResVO.class); System.out.println(errorResVO.getErrorCode() + ": " + errorResVO.getErrorMessage()); throw new Exception("安心签服务请求失败"); } Tx3203ResVO tx3203ResVO = new ObjectMapper().readValue(response, Tx3203ResVO.class); return tx3203ResVO.getContract().getContractNo(); } /** * 设置签名默认信息 * * @param signInfo * @param keyword */ public void setSignInfoDefault(UploadSignInfoVO signInfo, SignKeywordVO keyword) { signInfo.setIsProxySign(PROXY_SIGN_ON); signInfo.setProjectCode(PROJECT_CODE); signInfo.setIsCheckProjectCode(0); signInfo.setSignKeyword(keyword); } /** * 获得签名定位关键词对象 * * @param keyword * @param offsetX * @param offsetY * @param width * @param height * @return */ public SignKeywordVO getSignKeywork(String keyword, String offsetX, String offsetY, String width, String height) { SignKeywordVO signKeyword = new SignKeywordVO(); signKeyword.setKeyword(keyword); signKeyword.setOffsetCoordX(offsetX); signKeyword.setOffsetCoordY(offsetY); signKeyword.setImageWidth(width); signKeyword.setImageHeight(height); return signKeyword; } /** * 判断请求是否成功 * * @param response * @return */ private boolean isSucess(String response) { if (response.indexOf("\"head\"") < 0) { return false; } return true; } }