添加天眼查相关接口,生产信息返回接口
parent
18872cebe1
commit
4671fd9bc2
@ -0,0 +1,93 @@
|
||||
package com.glxp.udidl.admin.config;
|
||||
|
||||
import okhttp3.ConnectionPool;
|
||||
import okhttp3.OkHttpClient;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.SSLSocketFactory;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Configuration
|
||||
public class OkHttpConfiguration {
|
||||
|
||||
@Value("${ok.http.connect-timeout}")
|
||||
private Integer connectTimeout;
|
||||
|
||||
@Value("${ok.http.read-timeout}")
|
||||
private Integer readTimeout;
|
||||
|
||||
@Value("${ok.http.write-timeout}")
|
||||
private Integer writeTimeout;
|
||||
|
||||
@Value("${ok.http.max-idle-connections}")
|
||||
private Integer maxIdleConnections;
|
||||
|
||||
@Value("${ok.http.keep-alive-duration}")
|
||||
private Long keepAliveDuration;
|
||||
|
||||
@Bean
|
||||
public OkHttpClient okHttpClient() {
|
||||
return new OkHttpClient.Builder()
|
||||
.sslSocketFactory(sslSocketFactory(), x509TrustManager())
|
||||
// 是否开启缓存
|
||||
.retryOnConnectionFailure(false)
|
||||
.connectionPool(pool())
|
||||
.connectTimeout(connectTimeout, TimeUnit.SECONDS)
|
||||
.readTimeout(readTimeout, TimeUnit.SECONDS)
|
||||
.writeTimeout(writeTimeout, TimeUnit.SECONDS)
|
||||
.hostnameVerifier((hostname, session) -> true)
|
||||
// 设置代理
|
||||
// .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8888)))
|
||||
// 拦截器
|
||||
// .addInterceptor()
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public X509TrustManager x509TrustManager() {
|
||||
return new X509TrustManager() {
|
||||
@Override
|
||||
public void checkClientTrusted(X509Certificate[] chain, String authType)
|
||||
throws CertificateException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkServerTrusted(X509Certificate[] chain, String authType)
|
||||
throws CertificateException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return new X509Certificate[0];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SSLSocketFactory sslSocketFactory() {
|
||||
try {
|
||||
// 信任任何链接
|
||||
SSLContext sslContext = SSLContext.getInstance("TLS");
|
||||
sslContext.init(null, new TrustManager[]{x509TrustManager()}, new SecureRandom());
|
||||
return sslContext.getSocketFactory();
|
||||
} catch (NoSuchAlgorithmException | KeyManagementException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ConnectionPool pool() {
|
||||
return new ConnectionPool(maxIdleConnections, keepAliveDuration, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.glxp.udidl.admin.res.udid;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ManuInfoResponse {
|
||||
private String nameCode;
|
||||
|
||||
@ApiModelProperty(value = "生产标识是否包含批号")
|
||||
private String scbssfbhph;
|
||||
|
||||
@ApiModelProperty(value = "生产标识是否包含序列号")
|
||||
private String scbssfbhxlh;
|
||||
|
||||
@ApiModelProperty(value = "生产标识是否包含生产日期")
|
||||
private String scbssfbhscrq;
|
||||
|
||||
@ApiModelProperty(value = "生产标识是否包含失效日期")
|
||||
private String scbssfbhsxrq;
|
||||
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package com.glxp.udidl.admin.service.tyapi;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.glxp.udidl.admin.res.BaseResponse;
|
||||
import com.glxp.udidl.admin.util.OkHttpCli;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Service
|
||||
public class TyDlHttpClient {
|
||||
|
||||
@Resource
|
||||
OkHttpCli okHttpCli;
|
||||
@Value("${TY_AUTHORIZATION}")
|
||||
String suthorization;
|
||||
|
||||
String supplyUrl = "http://open.api.tianyancha.com/services/open/m/supply/2.0";
|
||||
String baseInfo = "http://open.api.tianyancha.com/services/open/ic/baseinfoV2/2.0";
|
||||
String contactUrl = "http://open.api.tianyancha.com/services/open/ic/contact";
|
||||
String openSearchUrl = "http://open.api.tianyancha.com/services/open/search/2.0";
|
||||
|
||||
|
||||
String[] headers = {"Authorization", suthorization};
|
||||
|
||||
//获取供应商列表
|
||||
public void getSupplier() {
|
||||
Map<String, String> params = new HashMap<>();
|
||||
String response = okHttpCli.doGet(supplyUrl, params, headers);
|
||||
BaseResponse<String> baseResponse = JSONObject.parseObject(response, new TypeReference<BaseResponse<String>>() {
|
||||
});
|
||||
if (baseResponse.getCode() == 20000) {
|
||||
} else {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,184 @@
|
||||
package com.glxp.udidl.admin.util;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import okhttp3.*;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* @author Answer.AI.L
|
||||
* @date 2019-04-09
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class OkHttpCli {
|
||||
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
||||
private static final MediaType XML = MediaType.parse("application/xml; charset=utf-8");
|
||||
|
||||
@Resource
|
||||
private OkHttpClient okHttpClient;
|
||||
|
||||
/**
|
||||
* get 请求
|
||||
*
|
||||
* @param url 请求url地址
|
||||
* @return string
|
||||
*/
|
||||
public String doGet(String url) {
|
||||
return doGet(url, null, null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get 请求
|
||||
*
|
||||
* @param url 请求url地址
|
||||
* @param params 请求参数 map
|
||||
* @return string
|
||||
*/
|
||||
public String doGet(String url, Map<String, String> params) {
|
||||
return doGet(url, params, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* get 请求
|
||||
*
|
||||
* @param url 请求url地址
|
||||
* @param headers 请求头字段 {k1, v1 k2, v2, ...}
|
||||
* @return string
|
||||
*/
|
||||
public String doGet(String url, String[] headers) {
|
||||
return doGet(url, null, headers);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get 请求
|
||||
*
|
||||
* @param url 请求url地址
|
||||
* @param params 请求参数 map
|
||||
* @param headers 请求头字段 {k1, v1 k2, v2, ...}
|
||||
* @return string
|
||||
*/
|
||||
public String doGet(String url, Map<String, String> params, String[] headers) {
|
||||
StringBuilder sb = new StringBuilder(url);
|
||||
if (params != null && params.keySet().size() > 0) {
|
||||
boolean firstFlag = true;
|
||||
for (String key : params.keySet()) {
|
||||
if (firstFlag) {
|
||||
sb.append("?").append(key).append("=").append(params.get(key));
|
||||
firstFlag = false;
|
||||
} else {
|
||||
sb.append("&").append(key).append("=").append(params.get(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Request.Builder builder = new Request.Builder();
|
||||
if (headers != null && headers.length > 0) {
|
||||
if (headers.length % 2 == 0) {
|
||||
for (int i = 0; i < headers.length; i = i + 2) {
|
||||
builder.addHeader(headers[i], headers[i + 1]);
|
||||
}
|
||||
} else {
|
||||
log.warn("headers's length[{}] is error.", headers.length);
|
||||
}
|
||||
|
||||
}
|
||||
log.info(sb.toString());
|
||||
Request request = builder.url(sb.toString()).build();
|
||||
log.info("do get request and url[{}]", sb.toString());
|
||||
return execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* post 请求
|
||||
*
|
||||
* @param url 请求url地址
|
||||
* @param params 请求参数 map
|
||||
* @return string
|
||||
*/
|
||||
public String doPost(String url, Map<String, String> params) {
|
||||
FormBody.Builder builder = new FormBody.Builder();
|
||||
|
||||
if (params != null && params.keySet().size() > 0) {
|
||||
for (String key : params.keySet()) {
|
||||
builder.add(key, params.get(key));
|
||||
}
|
||||
}
|
||||
Request request = new Request.Builder().url(url).post(builder.build()).build();
|
||||
log.info("do post request and url[{}]", url);
|
||||
|
||||
return execute(request);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* post 请求, 请求数据为 json 的字符串
|
||||
*
|
||||
* @param url 请求url地址
|
||||
* @param json 请求数据, json 字符串
|
||||
* @return string
|
||||
*/
|
||||
public String doPostJson(String url, String json) {
|
||||
log.info("do post request and url[{}]", url);
|
||||
return exectePost(url, json, JSON);
|
||||
}
|
||||
|
||||
public String doPostJson(String url, String json, String... headers) {
|
||||
log.info("do post request and url[{}]", url);
|
||||
return exectePost(url, json, JSON, headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* post 请求, 请求数据为 xml 的字符串
|
||||
*
|
||||
* @param url 请求url地址
|
||||
* @param xml 请求数据, xml 字符串
|
||||
* @return string
|
||||
*/
|
||||
public String doPostXml(String url, String xml) {
|
||||
log.info("do post request and url[{}]", url);
|
||||
return exectePost(url, xml, XML);
|
||||
}
|
||||
|
||||
|
||||
private String exectePost(String url, String data, MediaType contentType) {
|
||||
RequestBody requestBody = RequestBody.create(contentType, data);
|
||||
Request request = new Request.Builder().url(url).post(requestBody).build();
|
||||
|
||||
return execute(request);
|
||||
}
|
||||
|
||||
private String exectePost(String url, String data, MediaType contentType, String... headParms) {
|
||||
RequestBody requestBody = RequestBody.create(contentType, data);
|
||||
|
||||
Request request = new Request.Builder().headers(Headers.of(headParms)).url(url).post(requestBody).build();
|
||||
|
||||
return execute(request);
|
||||
}
|
||||
|
||||
private String execute(Request request) {
|
||||
Response response = null;
|
||||
try {
|
||||
response = okHttpClient.newCall(request).execute();
|
||||
if (response.isSuccessful()) {
|
||||
return response.body().string();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(ExceptionUtils.getStackTrace(e));
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
if (response != null) {
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue