@@ -0,0 +1,293 @@
package com.sdm.common.service.lyric ;
import com.alibaba.fastjson2.JSONObject ;
import com.sdm.common.common.SdmResponse ;
import com.sdm.common.utils.HttpUtil ;
import org.apache.http.client.HttpClient ;
import org.apache.http.client.config.RequestConfig ;
import org.apache.http.client.methods.CloseableHttpResponse ;
import org.apache.http.client.methods.HttpPost ;
import org.apache.http.conn.ssl.NoopHostnameVerifier ;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory ;
import org.apache.http.entity.ContentType ;
import org.apache.http.entity.mime.MultipartEntityBuilder ;
import org.apache.http.entity.mime.content.FileBody ;
import org.apache.http.entity.mime.content.StringBody ;
import org.apache.http.impl.client.CloseableHttpClient ;
import org.apache.http.impl.client.HttpClientBuilder ;
import org.apache.http.impl.client.HttpClients ;
import org.apache.http.ssl.SSLContextBuilder ;
import org.apache.http.util.EntityUtils ;
import org.springframework.stereotype.Service ;
import org.springframework.util.DigestUtils ;
import javax.net.ssl.SSLContext ;
import java.io.File ;
import java.nio.charset.StandardCharsets ;
import java.security.KeyManagementException ;
import java.security.KeyStoreException ;
import java.security.NoSuchAlgorithmException ;
import java.util.HashMap ;
import java.util.Map ;
@Service
public class LyricIntegrateService {
//测试开发环境
private final String appKey = " e9eb516aa02a43a29e227a0d901ec5f1 " ;
private final String appSecret = " 9fac43db08634aaf8a9fe5fb9468de9d " ;
//海葵云url
private final String HK_CLOUD_URL = " https://v15.lyh.haikuicloud.com " ;
//海葵云获取用户token url后缀
private final String HK_USER_TOKEN_URL_SUFFIX = " /merchant/openapi/user/login/jobNo " ;
//海葵云获取单个用户信息url后缀
private final String HK_SIMPLE_USER_URL_SUFFIX = " /merchant/api/user/getSimpleUserInfo " ;
//海葵云上传文件url后缀
private final String HK_UPLOAD_FILE_URL_SUFFIX = " /haikui-oa/autoCreateFlow/uploadFile " ;
//EP系统URL
private final String EP_URL = " https://ep-url.dev.haikuicloud.com " ;
//推送代办状态url后缀
private final String QUERY_TODO_STATUS_SUFFIX = " /todoApi/todo/emulation/dm/status " ;
//查询待办结果url后缀
private final String QUERY_TODO_RESULT_SUFFIX = " /todoApi/todo/emulation/dm/result " ;
//查询待办附加url后缀
private final String QUERY_TOD_ATTACHMENT_SUFFIX = " /todoApi/todo/emulation/dm/attachments " ;
//生产环境
/**
private final String EP_URL = "https://ep-url.lyh.haikuicloud.com;
*/
/**
* 生成海葵云token签名
*
* @param appKey
* @param appSecret
* @param jobNo
* @return
*/
private String calculateSignHkcloud ( String appKey , String appSecret , String jobNo ) {
JSONObject jsonObject = new JSONObject ( ) ;
jsonObject . put ( " appKey " , appKey ) ;
jsonObject . put ( " appSecret " , appSecret ) ;
jsonObject . put ( " jobNo " , jobNo ) ;
String addSalStr = JSONObject . toJSONString ( jsonObject ) ;
return DigestUtils . md5DigestAsHex ( addSalStr . getBytes ( StandardCharsets . UTF_8 ) ) ;
}
/**
* 获取根据工号获取用户token
*
* @param appKey
* @param appSecret
* @param jobNo
* @param tokenUrl
* @return
*/
public String getHKCloudToken ( String appKey , String appSecret , String jobNo , String tokenUrl ) {
String sign = calculateSignHkcloud ( appKey , appSecret , jobNo ) ;
String token = null ;
JSONObject jsonObject = new JSONObject ( ) ;
jsonObject . put ( " jobNo " , jobNo ) ;
jsonObject . put ( " appKey " , appKey ) ;
jsonObject . put ( " sign " , sign ) ;
try {
String result = HttpUtil . httpPost ( tokenUrl , null , jsonObject . toJSONString ( ) ) ;
JSONObject resultJson = JSONObject . parseObject ( result ) ;
String code = resultJson . getString ( " code " ) ;
if ( code . equals ( " 0000 " ) ) {
JSONObject dataObject = resultJson . getJSONObject ( " data " ) ;
if ( dataObject ! = null ) {
token = dataObject . getString ( " token " ) ;
}
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
return token ;
}
/**
* 获取当前用户的
*
* @param jobNo
* @return
*/
public SdmResponse getHKCloudSimpleUserInfo ( String jobNo ) {
SdmResponse response = SdmResponse . success ( ) ;
String tokenUrl = HK_CLOUD_URL + HK_USER_TOKEN_URL_SUFFIX ;
String token = getHKCloudToken ( appKey , appSecret , jobNo , tokenUrl ) ;
if ( token = = null | | token . isEmpty ( ) ) {
response = SdmResponse . failed ( " 获取海葵云token失败 " ) ;
} else {
String simpleUserUrl = HK_CLOUD_URL + HK_SIMPLE_USER_URL_SUFFIX ;
Map < String , String > headers = new HashMap < String , String > ( ) ;
headers . put ( " authorization " , token ) ;
try {
String result = HttpUtil . httpPost ( simpleUserUrl , headers , " " ) ;
JSONObject resultJson = JSONObject . parseObject ( result ) ;
String code = resultJson . getString ( " code " ) ;
if ( code . equals ( " 0000 " ) ) {
JSONObject dataObject = resultJson . getJSONObject ( " data " ) ;
if ( dataObject ! = null ) {
response . setData ( dataObject ) ;
}
} else {
response = SdmResponse . failed ( " 获取海葵云单个用户信息失败 " ) ;
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
response = SdmResponse . failed ( " 获取海葵云单个用户信息失败 " ) ;
}
}
return response ;
}
/**
* 构建信任所有证书的CloseableHttpClient
* @return
*/
private static CloseableHttpClient createHttpsClient ( ) {
try {
// 1. 构建SSL上下文( 信任所有证书)
SSLContext sslContext = SSLContextBuilder . create ( )
. loadTrustMaterial ( null , ( chain , authType ) - > true ) // 信任所有证书
. build ( ) ;
// 2. 创建SSL连接工厂( 忽略主机名验证)
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory (
sslContext ,
NoopHostnameVerifier . INSTANCE // 信任所有主机名
) ;
// 3. 配置超时参数
RequestConfig requestConfig = RequestConfig . custom ( )
. setConnectTimeout ( 30000 ) // 连接超时30秒
. setSocketTimeout ( 300000 ) // 读取超时5分钟
. setConnectionRequestTimeout ( 5000 ) // 连接池超时5秒
. build ( ) ;
// 4. 构建HttpClient
return HttpClients . custom ( )
. setSSLSocketFactory ( sslSocketFactory )
. setDefaultRequestConfig ( requestConfig )
. build ( ) ;
} catch ( NoSuchAlgorithmException | KeyManagementException | KeyStoreException e ) {
throw new RuntimeException ( " 创建HTTPS客户端失败 " , e ) ;
}
}
/**
* 上传仿真报告
*
* @param jobNo
* @param hkUploadFileReq
* @return
*/
public SdmResponse uploadHkFile ( String jobNo , HkUploadFileReq hkUploadFileReq ) {
SdmResponse response = SdmResponse . failed ( " 文件上传失败 " ) ;
String token = getHKCloudToken ( appKey , appSecret , jobNo , HK_CLOUD_URL ) ;
if ( token = = null | | token . isEmpty ( ) )
{
response = SdmResponse . failed ( " 获取token失败 " ) ;
}
else
{
String url = HK_CLOUD_URL + HK_UPLOAD_FILE_URL_SUFFIX ;
HttpPost httpPost = new HttpPost ( url ) ;
MultipartEntityBuilder builder = MultipartEntityBuilder . create ( )
. setCharset ( StandardCharsets . UTF_8 ) ; // 解决中文乱码
// 添加普通参数
builder . addPart ( " filePower " , new StringBody ( hkUploadFileReq . filePower ,
ContentType . TEXT_PLAIN . withCharset ( StandardCharsets . UTF_8 ) ) ) ;
builder . addPart ( " waterMarkFlag " , new StringBody ( String . valueOf ( hkUploadFileReq . waterMarkFlag ) ,
ContentType . TEXT_PLAIN . withCharset ( StandardCharsets . UTF_8 ) ) ) ;
builder . addPart ( " waterMarkContent " , new StringBody ( hkUploadFileReq . waterMarkContent ,
ContentType . TEXT_PLAIN . withCharset ( StandardCharsets . UTF_8 ) ) ) ;
builder . addPart ( " sysId " , new StringBody ( String . valueOf ( hkUploadFileReq . sysId ) ,
ContentType . TEXT_PLAIN . withCharset ( StandardCharsets . UTF_8 ) ) ) ;
builder . addPart ( " formId " , new StringBody ( String . valueOf ( hkUploadFileReq . formId ) ,
ContentType . TEXT_PLAIN . withCharset ( StandardCharsets . UTF_8 ) ) ) ;
builder . addPart ( " ComponentInstId " , new StringBody ( String . valueOf ( hkUploadFileReq . componentInstId ) ,
ContentType . TEXT_PLAIN . withCharset ( StandardCharsets . UTF_8 ) ) ) ;
builder . addPart ( " tableName " , new StringBody ( hkUploadFileReq . tableName ,
ContentType . TEXT_PLAIN . withCharset ( StandardCharsets . UTF_8 ) ) ) ;
builder . addPart ( " columnName " , new StringBody ( hkUploadFileReq . columnName ,
ContentType . TEXT_PLAIN . withCharset ( StandardCharsets . UTF_8 ) ) ) ;
builder . addPart ( " xmh " , new StringBody ( hkUploadFileReq . xmh ,
ContentType . TEXT_PLAIN . withCharset ( StandardCharsets . UTF_8 ) ) ) ;
builder . addPart ( " gwh " , new StringBody ( hkUploadFileReq . gwh ,
ContentType . TEXT_PLAIN . withCharset ( StandardCharsets . UTF_8 ) ) ) ;
// 追加多个文件( 同一字段名files)
for ( String filePath : hkUploadFileReq . files ) {
File file = new File ( filePath ) ;
if ( file . exists ( ) )
builder . addPart ( " files " , new FileBody ( file , ContentType . DEFAULT_BINARY , file . getName ( ) ) ) ;
}
// 构建HttpPost请求
httpPost . setEntity ( builder . build ( ) ) ;
try
{
CloseableHttpClient httpclient = createHttpsClient ( ) ;
CloseableHttpResponse fileResponse = httpclient . execute ( httpPost ) ;
String responseContent = EntityUtils . toString ( fileResponse . getEntity ( ) , StandardCharsets . UTF_8 ) ;
if ( responseContent ! = null & & ! responseContent . isEmpty ( ) )
{
JSONObject responseJson = JSONObject . parseObject ( responseContent ) ;
if ( responseJson . containsKey ( " code " ) )
{
String code = responseJson . getString ( " code " ) ;
if ( code ! = null & & code . equals ( " 0000 " ) )
{
response = SdmResponse . success ( ) ;
}
}
}
}
catch ( Exception e )
{
e . printStackTrace ( ) ;
response = SdmResponse . failed ( " 上传文件异常 " ) ;
}
}
return response ;
}
/**
* 推送代办状态
* @param todoId
* @param reason
* @param progress
* @param progressDescription
* @param userNo
* @param statusType doing(启动), turnDown(驳回), pause(暂停), restart(重启), updateProgress(提交进度), finished(完成), closed(关闭)
* @return
*/
public SdmResponse pushTodoStatus ( int todoId , String reason , int progress , String progressDescription , String userNo , String statusType )
{
return null ;
}
public SdmResponse getTodoAttachments ( )
{
return null ;
}
public SdmResponse getTodoList ( )
{
return null ;
}
}