对接接口方法:1.获取token2.更新案件状态3.上传文件4.推送案件附件信息

This commit is contained in:
wangqiong
2024-03-28 18:54:08 +08:00
parent b0f71c6e3a
commit 08f4aca27c
11 changed files with 781 additions and 11 deletions
@@ -0,0 +1,77 @@
package com.ruoyi.system.service;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.wisdomarbitrate.domain.entity.casestatus.MsCaseStatus;
import com.ruoyi.wisdomarbitrate.domain.vo.mscase.MsCaseFileInfo;
import com.ruoyi.wisdomarbitrate.domain.vo.mscase.MsCaseStatusInfo;
import org.apache.ibatis.annotations.Case;
import org.apache.poi.hmef.Attachment;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
public interface BeiMingInterface {
/**
* 1.获取北明接口令牌token
*
* @param userName
* @param password
* @param times
* @return
*/
String getApiToken(String userName, String password, Long times);
/**
* 2.提交案件状态信息
*
* @param token 令牌
* @param abutmentId 第三方平台案件唯一标识
* @param msCaseStatusInfo 案件状态信息
* @return
*/
JSONObject submitCaseStatusInfo(String token, String abutmentId, String syncSource, MsCaseStatusInfo msCaseStatusInfo);
/**
* 3.上传附件
*
* @param file
* @return
*/
JSONObject uploadFile(File file, String token, String syncSource);
/**
* 4.同步附件信息
*
* @param action
* @param caseNo
* @param msCaseFileInfo
* @return
*/
JSONObject syncAttachmentInfo(String token, String syncSource, String action, String caseNo, MsCaseFileInfo msCaseFileInfo);
/**
* 推送案件状态信息(调解系统推送案件状态时调用)
*
* @param username 用户名
* @param password 密码
* @param caseNo 案件编号
* @param statusCode 案件状态编码
* @param caseClosureExplanation 案件状态描述(或结案信息)
* @return
*/
JSONObject pushCaseStatusInfo(String username, String password, String caseNo, String statusCode, String caseClosureExplanation);
/**
* 推送案件附件信息(调解系统推送案件附件信息时调用)
*
* @param username 用户名
* @param password 密码
* @param file 文件
* @param abutmentId 调解系统文件Id
* @param syncSource 用户名
* @param caseNo 案件编号
* @return
*/
JSONObject pushAttachmentInfo(String username, String password, File file, String abutmentId, String syncSource, String caseNo);
}
@@ -0,0 +1,316 @@
package com.ruoyi.system.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.ruoyi.common.enums.AttachmentOperateTypeEnum;
import com.ruoyi.system.service.BeiMingInterface;
import com.ruoyi.wisdomarbitrate.domain.vo.mscase.MsCaseFileInfo;
import com.ruoyi.wisdomarbitrate.domain.vo.mscase.MsCaseStatusInfo;
import com.ruoyi.wisdomarbitrate.utils.CommonInputStreamResource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import java.io.File;
import java.io.FileInputStream;
import static com.ruoyi.common.utils.EncryptUtils.sm4Decrypt;
import static com.ruoyi.common.utils.EncryptUtils.sm4Encrypt;
/**
* @author ym
*/
@Slf4j
@Service
public class BeiMingInterfaceService implements BeiMingInterface {
@Autowired
RestTemplate restTemplate;
/**
* 接口地址
*/
@Value("${beimingapihost}")
public String apihost;
/**
* 接口路径前缀
*/
@Value("${beimingapiprefix}")
public String apiprefix;
@Value("${beimingprivatekey}")
public String privateKey;
/**
* 1.获取北明接口令牌token
*
* @param userName
* @param password
* @param times
*/
@Override
public String getApiToken(String userName, String password, Long times) {
JSONObject result = new JSONObject();
try {
//设置请求头
HttpHeaders httpHeaders = new HttpHeaders();
//传递请求体时必须设置传递参数的格式,为Content-Type : application/json
httpHeaders.add("Content-Type", "application/json;charset=UTF-8");
httpHeaders.add("syncSource", userName);
// 2.请求头 & 请求体
HttpEntity<JSON> fromEntity = new HttpEntity(httpHeaders);
JSONObject body = new JSONObject();
// body.put("account", userName);
// body.put("password", password);
// body.put("timestamp", times);
//对整体请求进行加密
ObjectNode param = new ObjectMapper().createObjectNode();
param.put("account", userName);
param.put("password", password);
param.put("timestamp", times);
String encryptString = sm4Encrypt(param.toString(), privateKey);
body.put("encryptString", encryptString);
System.out.println("encryptString:" + encryptString);
fromEntity = new HttpEntity(body, httpHeaders);
String url = apihost + apiprefix + "/getToken";
result = restTemplate.postForObject(url, fromEntity, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
return result.toString();
}
/**
* 解析加密后端token
*
* @param token
* @return
*/
public String analysisResultToken(String token) {
String resultToken = null;
if (token != null && !token.isEmpty()) {
JSONObject jsonObject = JSON.parseObject(token);
String tokenString = jsonObject.getString("data");
System.out.println("data信息:" + tokenString);
String tokenstr = sm4Decrypt(tokenString, privateKey);
System.out.println("解密后的字符串:" + tokenstr);
if (tokenstr != null && !tokenstr.isEmpty()) {
JSONObject tokenObject = JSON.parseObject(tokenstr);
resultToken = tokenObject.getString("token");
}
}
return resultToken;
}
/**
* 2.推送案件状态信息
*
* @param token 令牌
* @param abutmentId 第三方平台案件唯一标识
* @param msCaseStatusInfo 案件状态信息
* @param syncSource 同步来源(账户名)
* @return
*/
@Override
public JSONObject submitCaseStatusInfo(String token, String abutmentId, String syncSource, MsCaseStatusInfo msCaseStatusInfo) {
JSONObject result = new JSONObject();
try {
//设置请求头
HttpHeaders httpHeaders = new HttpHeaders();
//传递请求体时必须设置传递参数的格式,为Content-Type : application/json
httpHeaders.add("Content-Type", "application/json;charset=UTF-8");
httpHeaders.add("token", token);
httpHeaders.add("syncSource", syncSource);
//对整体请求进行加密
ObjectNode param = new ObjectMapper().createObjectNode();
param.put("abutmentId", abutmentId);
param.put("caseNo", msCaseStatusInfo.getCaseNo());
param.put("statusCode", msCaseStatusInfo.getStatusCode());
param.put("caseClosureExplanation", msCaseStatusInfo.getCaseClosureExplanation());
String encryptString = sm4Encrypt(param.toString(), privateKey);
// 2.请求头 & 请求体
HttpEntity<JSON> fromEntity = new HttpEntity(httpHeaders);
JSONObject body = new JSONObject();
// body.put("abutmentId", Encrypt("abutmentId",abutmentId));
// body.put("caseNo", Encrypt("caseNo", msCaseStatusInfo.getCaseNo()));
// body.put("statusCode", Encrypt("statusCode",msCaseStatusInfo.getStatusCode()));
// body.put("caseClosureExplanation", Encrypt("caseClosureExplanation", msCaseStatusInfo.getCaseClosureExplanation()));
//对整体请求进行加密
body.put("encryptString", encryptString);
fromEntity = new HttpEntity(body, httpHeaders);
String url = apihost + apiprefix + "/caseMediation/status";
result = restTemplate.postForObject(url, fromEntity, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
return result;
}
/**
* 3.上传附件
*
* @param file
* @return
*/
@Override
public JSONObject uploadFile(File file, String token, String syncSource) {
System.out.println("文件:" + file.getName());
System.out.println("文件:" + file.toString());
JSONObject result = new JSONObject();
try {
//设置请求头
HttpHeaders httpHeaders = new HttpHeaders();
//传递请求体时必须设置传递参数的格式,为Content-Type : application/json
httpHeaders.add("Content-Type", "multipart/form-data");
httpHeaders.add("token", token);
httpHeaders.add("syncSource", syncSource);
// 构建请求体
MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>();
CommonInputStreamResource commonInputStreamResource = null;
try {
FileInputStream fileInputStream = new FileInputStream(file);
commonInputStreamResource = new CommonInputStreamResource(fileInputStream, file.length(), file.getName());
} catch (Exception e) {
log.error("文件输入流转换错误", e);
}
requestBody.add("file", commonInputStreamResource);
HttpEntity<MultiValueMap> fromEntity = new HttpEntity<MultiValueMap>(requestBody, httpHeaders);
String url = apihost + apiprefix + "/uploadFile";
result = restTemplate.postForObject(url, fromEntity, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
return result;
}
/**
* 4.同步附件信息
*
* @param token
* @param syncSource 用户名
* @param action 附件操作类型
* @param caseNo 案件编号
* @param msCaseFileInfo 案件附件信息
* @return
*/
@Override
public JSONObject syncAttachmentInfo(String token, String syncSource, String action, String caseNo, MsCaseFileInfo msCaseFileInfo) {
JSONObject result = new JSONObject();
try {
//设置请求头
HttpHeaders httpHeaders = new HttpHeaders();
//传递请求体时必须设置传递参数的格式,为Content-Type : application/json
httpHeaders.add("Content-Type", "application/json;charset=UTF-8");
httpHeaders.add("token", token);
httpHeaders.add("syncSource", syncSource);
//对整体请求进行加密
ObjectNode param = new ObjectMapper().createObjectNode();
param.put("caseNo", caseNo);
param.put("action", action);
param.put("abutmentId", msCaseFileInfo.getAbutmentId());
param.put("abutmentCaseId", msCaseFileInfo.getAbutmentCaseId());
param.put("documentSubject", msCaseFileInfo.getDocumentSubject());
param.put("documentType", msCaseFileInfo.getDocumentType());
param.put("fileName", msCaseFileInfo.getFileName());
param.put("fileId", msCaseFileInfo.getFileId());
if (msCaseFileInfo.getOwnerType() != null) {
param.put("ownerType", msCaseFileInfo.getOwnerType());
}
if (msCaseFileInfo.getOwnerId() != null) {
param.put("ownerId", msCaseFileInfo.getOwnerId());
}
if (msCaseFileInfo.getOwnerName() != null) {
param.put("ownerName", msCaseFileInfo.getOwnerName());
}
String encryptString = sm4Encrypt(param.toString(), privateKey);
// 2.请求体
HttpEntity<JSON> fromEntity = new HttpEntity(httpHeaders);
JSONObject body = new JSONObject();
//对整体请求进行加密
body.put("encryptString", encryptString);
fromEntity = new HttpEntity(body, httpHeaders);
String url = apihost + apiprefix + "/caseMediation/attachment/accept";
result = restTemplate.postForObject(url, fromEntity, JSONObject.class);
} catch (RestClientException e) {
e.printStackTrace();
}
return result;
}
/**
* 推送案件状态信息
*/
@Override
public JSONObject pushCaseStatusInfo(String username, String password, String caseNo, String statusCode, String caseClosureExplanation) {
JSONObject result = new JSONObject();
String token = getApiToken(username, password, System.currentTimeMillis());
token = analysisResultToken(token);
if (token != null && !token.isEmpty()) {
MsCaseStatusInfo info = MsCaseStatusInfo.builder().caseNo(caseNo).statusCode(statusCode).caseClosureExplanation(caseClosureExplanation).build();
result = submitCaseStatusInfo(token, caseNo, username, info);
System.out.println("jieguo:" + result.toString());
if (result != null) {
String data = result.getString("data");
String datastr = sm4Decrypt(data, privateKey);
System.out.println("最终解密后的字符串:" + datastr);
}
}
return result;
}
/**
* 推送案件附件信息
*/
@Override
public JSONObject pushAttachmentInfo(String username, String password, File file, String abutmentId, String syncSource, String caseNo) {
JSONObject result = new JSONObject();
//1.获取token
String token = getApiToken(username, password, System.currentTimeMillis());
token = analysisResultToken(token);
if (token != null && !token.isEmpty()) {
//2.上传文件
JSONObject fileResult = uploadFile(file, token, syncSource);
if (fileResult != null) {
String data = fileResult.getString("data");
String datastr = sm4Decrypt(data, privateKey);
System.out.println("最终解密后的字符串:" + datastr);
if (datastr != null) {
JSONObject parse = JSON.parseObject(datastr);
if (parse != null) {
String fileId = parse.getString("fileId");
System.out.println("fileId====:" + fileId);
if (fileId != null) {
//3.同步附件更新信息
MsCaseFileInfo fileInfo = MsCaseFileInfo.builder().fileId(fileId).fileName(file.getName()).abutmentId(abutmentId).abutmentCaseId(caseNo).documentSubject("EVEDENT").documentType("EVEDENT_METERIAL").build();
result = syncAttachmentInfo(token, username, AttachmentOperateTypeEnum.ADD.getCode(), caseNo, fileInfo);
result = syncAttachmentInfo(token, username, AttachmentOperateTypeEnum.UPD.getCode(), caseNo, fileInfo);
result = syncAttachmentInfo(token, username, AttachmentOperateTypeEnum.DEL.getCode(), caseNo, fileInfo);
}
}
}
}
}
return result;
}
/**
* 对字段值进行加密
*
* @return
*/
private String Encrypt(String filed, String filedValue) {
ObjectNode param = new ObjectMapper().createObjectNode();
param.put(filed, filedValue);
String encryptString = sm4Encrypt(param.toString(), privateKey);
return encryptString;
}
}
@@ -0,0 +1,54 @@
package com.ruoyi.wisdomarbitrate.domain.vo.mscase;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 北明接口案件附件信息入参
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class MsCaseFileInfo {
/**
* 第三方文件唯一标识
*/
private String abutmentId;
/**
* 第三方平台案件唯一标识
*/
private String abutmentCaseId;
/**
* 第一级文件类型
*/
private String documentSubject;
/**
* 第二级文件类型
*/
private String documentType;
/**
* 所属人类型(可不传)
*/
private String ownerType;
/**
* 文件所属人第三方平台对应的人员唯一标识(可不传)
*/
private String ownerId;
/**
* 文件所属人名称(可不传)
*/
private String ownerName;
/**
* 文件名称
*/
private String fileName;
/**
* 一站式平台返回的文件ID
*/
private String fileId;
}
@@ -0,0 +1,35 @@
package com.ruoyi.wisdomarbitrate.domain.vo.mscase;
import lombok.*;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* 北明接口案件状态入参
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class MsCaseStatusInfo {
/**
* 案号
* 案件状态编码
* 结案说明
*/
private String caseNo;
/**
* 案件状态编码
*/
private String statusCode;
/**
* 结案说明
*/
private String caseClosureExplanation;
}
@@ -0,0 +1,42 @@
package com.ruoyi.wisdomarbitrate.utils;
import org.springframework.core.io.InputStreamResource;
import java.io.InputStream;
public class CommonInputStreamResource extends InputStreamResource {
private long length;
private String fileName;
public CommonInputStreamResource(InputStream inputStream, long length, String fileName) {
super(inputStream);
this.length = length;
this.fileName = fileName;
}
/**
* 覆写父类方法
* 如果不重写这个方法,并且文件有一定大小,那么服务端会出现异常
* {@code The multi-part request contained parameter data (excluding uploaded files) that exceeded}
*/
@Override
public String getFilename() {
return fileName;
}
/**
* 覆写父类 contentLength 方法
* 因为 {@link org.springframework.core.io.AbstractResource#contentLength()}方法会重新读取一遍文件,
* 而上传文件时,restTemplate 会通过这个方法获取大小。然后当真正需要读取内容的时候,发现已经读完,会报如下错误。
*/
@Override
public long contentLength() {
long estimate = length;
return estimate == 0 ? 1 : estimate;
}
public void setLength(long length) {
this.length = length;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}