Files
Arbitrate-Backend/ruoyi-system/src/main/java/com/ruoyi/bestsign/utils/BestsignOpenApiClient.java
T

197 lines
6.9 KiB
Java
Raw Normal View History

2023-10-05 12:25:52 +08:00
package com.ruoyi.bestsign.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
2023-10-05 20:03:21 +08:00
import javax.validation.constraints.NotNull;
2023-10-05 12:25:52 +08:00
import java.io.IOException;
/**
* 上上签混合云SDK客户端
*/
@Component
@Data
@Slf4j
public class BestsignOpenApiClient {
/**
* 开发者id
*/
@Value("${ssq.developerId}")
private String developerId;
/**
* 开发者私钥
*/
2023-10-05 20:36:22 +08:00
@Value("${ssq.privateKey}")
private String privateKey;
2023-10-05 12:25:52 +08:00
/**
* Host地址
*/
@Value("${ssq.serverHost}")
private String serverHost;
2023-10-05 20:03:21 +08:00
/**
* 签名参数
*/
2023-10-05 12:25:52 +08:00
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 地址
2023-10-05 20:36:22 +08:00
* @param method 地址
2023-10-05 12:25:52 +08:00
* @return 异步申请任务单号
* @throws IOException
*/
2023-10-05 20:03:21 +08:00
public JSONObject userPersonalReg(String account, String name, String mail,
2023-10-05 20:36:22 +08:00
String mobile, String identity, String identityType,
String contactMail, String contactMobile, String province,
String city, String address, @NotNull String method) throws Exception {
2023-10-05 20:03:21 +08:00
//body参数
2023-10-05 12:25:52 +08:00
JSONObject requestBody = new JSONObject();
2023-10-05 20:03:21 +08:00
//用户帐号
requestBody.put("account", account);
//用户名称
requestBody.put("name", name);
//用户类型
requestBody.put("userType", "1");
//用户邮箱
2023-10-05 20:36:22 +08:00
requestBody.put("mail", mail);
2023-10-05 20:03:21 +08:00
//用户手机号
requestBody.put("mobile", mobile);
//用户证件信息对象
2023-10-05 12:25:52 +08:00
JSONObject credential = new JSONObject();
2023-10-05 20:03:21 +08:00
//用户证件号
2023-10-05 12:25:52 +08:00
credential.put("identity", identity);
2023-10-05 20:03:21 +08:00
//用户证件类型
2023-10-05 12:25:52 +08:00
credential.put("identityType", identityType);
requestBody.put("credential", credential);
2023-10-05 20:03:21 +08:00
2023-10-05 12:25:52 +08:00
//是否申请证书
2023-10-05 20:03:21 +08:00
requestBody.put("applyCert", "1");
2023-10-05 12:25:52 +08:00
// 生成一个时间戳参数
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);
2023-10-05 20:03:21 +08:00
System.out.println(JSON.toJSONString(userObj));
return userObj;
2023-10-05 12:25:52 +08:00
// 返回errno为0,表示成功,其他表示失败
2023-10-05 20:03:21 +08:00
// 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"));
// }
2023-10-05 12:25:52 +08:00
}
2023-10-05 20:19:57 +08:00
2023-10-05 12:25:52 +08:00
/**
* 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;
}
2023-10-05 20:19:57 +08:00
2023-10-05 20:36:22 +08:00
public String selectApplyStatus(String account, String taskId) throws Exception {
2023-10-05 20:19:57 +08:00
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参数
2023-10-05 20:36:22 +08:00
String fullUrlParams = String.format(urlSignParams, this.developerId,
2023-10-05 20:19:57 +08:00
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 {
2023-10-05 20:36:22 +08:00
log.error("查询申请状态异常:" + responseObj.toJSONString());
2023-10-05 20:19:57 +08:00
}
return responseObj.toJSONString();
}
2023-10-05 12:25:52 +08:00
}