package com.ruoyi.bestsign.utils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.ruoyi.bestsign.domain.ArbitrSignatuVO; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import javax.validation.constraints.NotNull; import java.io.IOException; /** * 上上签混合云SDK客户端 */ @Component @Data @Slf4j public class BestsignOpenApiClient { /** * 开发者id */ @Value("${ssq.developerId}") private String developerId; /** * 开发者私钥 */ @Value("${ssq.privateKey}") private String privateKey; /** * Host地址 */ @Value("${ssq.serverHost}") private String serverHost; /** * 签名参数 */ private static String urlSignParams = "?developerId=%s&rtick=%s&signType=rsa&sign=%s"; // public BestsignOpenApiClient(String developerId, String privateKey, // String serverHost) { // this.developerId = developerIds; // this.privateKey = privateKey; // this.serverHost = serverHost; // } /** * POST方法示例 * 个人用户注册 * * @param account 用户账号 * @param name 姓名 * @param mail 用来接收通知邮件的电子邮箱 * @param mobile 用来接收通知短信的手机号码 * @param identity 证件号码 * @param identityType 枚举值:0-身份证,目前仅支持身份证 * @param contactMail 电子邮箱 * @param contactMobile 手机号码 * @param province 省份 * @param city 城市 * @param address 地址 * @param method 地址 * @return 异步申请任务单号 * @throws IOException */ public JSONObject userPersonalReg(String account, String name, String mail, String mobile, String identity, String identityType, String contactMail, String contactMobile, String province, String city, String address, @NotNull String method) throws Exception { //body参数 JSONObject requestBody = new JSONObject(); //用户帐号 requestBody.put("account", account); //用户名称 requestBody.put("name", name); //用户类型 requestBody.put("userType", "1"); //用户邮箱 requestBody.put("mail", mail); //用户手机号 requestBody.put("mobile", mobile); //用户证件信息对象 JSONObject credential = new JSONObject(); //用户证件号 credential.put("identity", identity); //用户证件类型 credential.put("identityType", identityType); requestBody.put("credential", credential); //是否申请证书 requestBody.put("applyCert", "1"); // 生成一个时间戳参数 String rtick = RSAUtils.getRtick(); // 计算参数签名 String paramsSign = RSAUtils.calcRsaSign(this.developerId, this.privateKey, this.serverHost, method, rtick, null, requestBody.toJSONString()); // 签名参数追加为url参数 String fullUrlParams = String.format(urlSignParams, this.developerId, rtick, paramsSign); // 发送POST请求 String responseBody = HttpClientSender.sendHttpPost(this.serverHost, method, fullUrlParams, requestBody.toJSONString()); System.out.println(responseBody); // 返回结果解析 JSONObject userObj = JSON.parseObject(responseBody); System.out.println(JSON.toJSONString(userObj)); return userObj; // 返回errno为0,表示成功,其他表示失败 // if (userObj.getIntValue("errno") == 0) { // JSONObject data = userObj.getJSONObject("data"); // if (data != null) { // //对返回data进行处理 // String taskId = data.getString("taskId"); // return taskId; // } // return null; // } else { // //接口返回异常 // System.out.println(userObj.getIntValue("errno")); // System.out.println(userObj.getString("errmsg")); // throw new Exception(userObj.getIntValue("errno") + ":" // + userObj.getString("errmsg")); // } } /** * GET方法示例 * 下载合同PDF文件 * * @param contractId 合同编号 * @return * @throws Exception */ public byte[] contractDownload(String contractId) throws Exception { String host = this.serverHost; String method = "/storage/contract/download/"; // 组装url参数 String urlParams = "contractId=" + contractId; // 生成一个时间戳参数 String rtick = RSAUtils.getRtick(); // 计算参数签名 String paramsSign = RSAUtils.calcRsaSign(this.developerId, this.privateKey, host, method, rtick, urlParams, null); // 签名参数追加为url参数 urlParams = String.format(urlSignParams, this.developerId, rtick, paramsSign) + "&" + urlParams; // 发送请求 byte[] responseBody = HttpClientSender.sendHttpGet(host, method, urlParams); // 返回结果解析 return responseBody; } public String selectApplyStatus(String account, String taskId) throws Exception { String methodInvoke = "/user/async/applyCert/status/"; JSONObject requestParam = new JSONObject(); //用户账号 requestParam.put("account", account); //任务单号 requestParam.put("taskId", taskId); String timestamsParam = RSAUtils.getRtick(); // 计算参数签名 String paramsSign = RSAUtils.calcRsaSign(this.developerId, this.privateKey, this.serverHost, methodInvoke, timestamsParam, null, requestParam.toJSONString()); // 签名参数追加为url参数 String fullUrlParams = String.format(urlSignParams, this.developerId, timestamsParam, paramsSign); String responseResult = HttpClientSender.sendHttpPost(this.serverHost, methodInvoke, fullUrlParams, requestParam.toJSONString()); JSONObject responseObj = JSON.parseObject(responseResult); // 返回errno为0,表示成功,其他表示失败 if (responseObj.getIntValue("errno") == 0) { JSONObject data = responseObj.getJSONObject("data"); if (data != null) { String message = data.getString("message"); String status = data.getString("status"); return status; } } else { log.error("查询申请状态异常:" + responseObj.toJSONString()); } return responseObj.toJSONString(); } /** * 上传文件 * * @param arbitrSignatuVO * @return * @throws Exception */ public String uploadContact(ArbitrSignatuVO arbitrSignatuVO) throws Exception { String methodInvoke = "/storage/contract/upload/"; JSONObject requestParam = new JSONObject(); requestParam.put("account", arbitrSignatuVO.getAccount()); requestParam.put("fmd5", arbitrSignatuVO.getFileMd5()); requestParam.put("fdata", arbitrSignatuVO.getFileData()); requestParam.put("fpages", arbitrSignatuVO.getFilePages()); requestParam.put("fname", arbitrSignatuVO.getFileName()); requestParam.put("ftype", arbitrSignatuVO.getFileType()); requestParam.put("title", arbitrSignatuVO.getContractTitle()); requestParam.put("expireTime", arbitrSignatuVO.getPeriodValidity()); String timestamsParam = RSAUtils.getRtick(); // 计算参数签名 String paramsSign = RSAUtils.calcRsaSign(this.developerId, this.privateKey, this.serverHost, methodInvoke, timestamsParam, null, requestParam.toJSONString()); // 签名参数追加为url参数 String fullUrlParams = String.format(urlSignParams, this.developerId, timestamsParam, paramsSign); String responseResult = HttpClientSender.sendHttpPost(this.serverHost, methodInvoke, fullUrlParams, requestParam.toJSONString()); JSONObject responseObj = JSON.parseObject(responseResult); // 返回errno为0,表示成功,其他表示失败 if (responseObj.getIntValue("errno") == 0) { JSONObject data = responseObj.getJSONObject("data"); if (data != null) { String contractId = data.getString("contractId"); return contractId; } } else { log.error("上传文件异常:" + responseObj.toJSONString()); } return responseObj.toJSONString(); } /** * 获取签署链接 * * @param arbitrSignatuVO * @return * @throws Exception */ public String getSignLink(ArbitrSignatuVO arbitrSignatuVO) throws Exception { String methodInvoke = "/contract/send/"; JSONObject requestParam = new JSONObject(); requestParam.put("contractId", arbitrSignatuVO.getContractNum()); requestParam.put("signer", arbitrSignatuVO.getSignerAccout()); JSONArray signatureArray = new JSONArray(); JSONObject signatuItem = new JSONObject(); signatuItem.put("pageNum", arbitrSignatuVO.getPageNumSigner()); //x坐标 signatuItem.put("x", arbitrSignatuVO.getPageNumSignerX()); //y坐标 signatuItem.put("y", arbitrSignatuVO.getPageNumSignerY()); signatureArray.add(signatuItem); requestParam.put("signaturePositions", signatureArray); String timestamsParam = RSAUtils.getRtick(); // 计算参数签名 String paramsSign = RSAUtils.calcRsaSign(this.developerId, this.privateKey, this.serverHost, methodInvoke, timestamsParam, null, requestParam.toJSONString()); // 签名参数追加为url参数 String fullUrlParams = String.format(urlSignParams, this.developerId, timestamsParam, paramsSign); String responseResult = HttpClientSender.sendHttpPost(this.serverHost, methodInvoke, fullUrlParams, requestParam.toJSONString()); JSONObject responseObj = JSON.parseObject(responseResult); // 返回errno为0,表示成功,其他表示失败 if (responseObj.getIntValue("errno") == 0) { JSONObject data = responseObj.getJSONObject("data"); if (data != null) { String signLink = data.getString("url"); return signLink; } } else { log.error("获取签署链接异常:" + responseObj.toJSONString()); } return responseObj.toJSONString(); } /** * 完成签署 * * @param arbitrSignatuVO * @return * @throws Exception */ public String finishSign(ArbitrSignatuVO arbitrSignatuVO) throws Exception { String methodInvoke = "/storage/contract/lock/"; JSONObject requestParam = new JSONObject(); requestParam.put("contractId", arbitrSignatuVO.getContractNum()); String timestamsParam = RSAUtils.getRtick(); // 计算参数签名 String paramsSign = RSAUtils.calcRsaSign(this.developerId, this.privateKey, this.serverHost, methodInvoke, timestamsParam, null, requestParam.toJSONString()); // 签名参数追加为url参数 String fullUrlParams = String.format(urlSignParams, this.developerId, timestamsParam, paramsSign); String responseResult = HttpClientSender.sendHttpPost(this.serverHost, methodInvoke, fullUrlParams, requestParam.toJSONString()); JSONObject responseObj = JSON.parseObject(responseResult); // 返回errno为0,表示成功,其他表示失败 if (responseObj.getIntValue("errno") == 0) { return "完成签名"; } else { log.error("完成签署异常:" + responseObj.toJSONString()); } return responseObj.toJSONString(); } }