From 4671fd9bc21e9338d2a9819fa81b808f6ca3acb9 Mon Sep 17 00:00:00 2001 From: anthonywj Date: Wed, 26 Jul 2023 14:40:58 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=A4=A9=E7=9C=BC=E6=9F=A5?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E6=8E=A5=E5=8F=A3,=E7=94=9F=E4=BA=A7?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E8=BF=94=E5=9B=9E=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 8 +- .../admin/config/OkHttpConfiguration.java | 93 +++++++++ .../device/ProductInfoController.java | 33 ++++ .../admin/res/udid/ManuInfoResponse.java | 22 +++ .../admin/service/tyapi/TyDlHttpClient.java | 41 ++++ .../com/glxp/udidl/admin/util/OkHttpCli.java | 184 ++++++++++++++++++ src/main/resources/application-dev.yml | 9 + src/main/resources/application-pro.yml | 9 +- 8 files changed, 396 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/glxp/udidl/admin/config/OkHttpConfiguration.java create mode 100644 src/main/java/com/glxp/udidl/admin/res/udid/ManuInfoResponse.java create mode 100644 src/main/java/com/glxp/udidl/admin/service/tyapi/TyDlHttpClient.java create mode 100644 src/main/java/com/glxp/udidl/admin/util/OkHttpCli.java diff --git a/pom.xml b/pom.xml index 4702528..e2b3d43 100644 --- a/pom.xml +++ b/pom.xml @@ -199,7 +199,11 @@ activation 1.1.1 - + + com.squareup.okhttp3 + okhttp + 3.10.0 + @@ -219,4 +223,4 @@ - \ No newline at end of file + diff --git a/src/main/java/com/glxp/udidl/admin/config/OkHttpConfiguration.java b/src/main/java/com/glxp/udidl/admin/config/OkHttpConfiguration.java new file mode 100644 index 0000000..57c4e74 --- /dev/null +++ b/src/main/java/com/glxp/udidl/admin/config/OkHttpConfiguration.java @@ -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); + } +} diff --git a/src/main/java/com/glxp/udidl/admin/controller/device/ProductInfoController.java b/src/main/java/com/glxp/udidl/admin/controller/device/ProductInfoController.java index 8b84bff..b72f2fa 100644 --- a/src/main/java/com/glxp/udidl/admin/controller/device/ProductInfoController.java +++ b/src/main/java/com/glxp/udidl/admin/controller/device/ProductInfoController.java @@ -13,6 +13,7 @@ import com.glxp.udidl.admin.req.SyncUdiRequest; import com.glxp.udidl.admin.req.UdiCompanyRequest; import com.glxp.udidl.admin.res.BaseResponse; import com.glxp.udidl.admin.res.PageSimpleResponse; +import com.glxp.udidl.admin.res.udid.ManuInfoResponse; import com.glxp.udidl.admin.res.udid.ProductInfoResponse; import com.glxp.udidl.admin.res.udid.VailUdiReponse; import com.glxp.udidl.admin.service.inout.ProductInfoService; @@ -427,4 +428,36 @@ public class ProductInfoController { } } + + + @ApiOperation(value = "获取生产标识信息", response = ProductInfoEntity.class) + @ApiImplicitParams({@ApiImplicitParam(name = "code", value = "UDI码", required = true)}) + @GetMapping("udidl/device/getManuInfo") + public BaseResponse getManuInfo(String code) { + + if (StrUtil.isEmpty(code)) { + return ResultVOUtils.error(500, "查询条件不能为空!"); + } + UdiEntity udiEntity = FilterUdiUtils.getUdi(code); + if (udiEntity == null) { + return ResultVOUtils.error(500, "UDI码格式错误!"); + } else { + if (StrUtil.isNotEmpty(udiEntity.getUdi())) { + ProductInfoFilterRequest productInfoFilterRequest = new ProductInfoFilterRequest(); + productInfoFilterRequest.setNameCode(udiEntity.getUdi()); + List productInfoEntityList = productInfoService.filterProductInfo(productInfoFilterRequest); + if (CollUtil.isEmpty(productInfoEntityList)) { + return ResultVOUtils.error(500, "未查询到该UDI码国家库产品信息!"); + } else { + ProductInfoEntity productInfoEntity = productInfoEntityList.get(0); + ManuInfoResponse manuInfoResponse = new ManuInfoResponse(); + BeanUtils.copyProperties(productInfoEntity, manuInfoResponse); + return ResultVOUtils.success(manuInfoResponse); + } + } else { + return ResultVOUtils.error(500, "UDI码格式错误!"); + } + } + + } } diff --git a/src/main/java/com/glxp/udidl/admin/res/udid/ManuInfoResponse.java b/src/main/java/com/glxp/udidl/admin/res/udid/ManuInfoResponse.java new file mode 100644 index 0000000..f813e4e --- /dev/null +++ b/src/main/java/com/glxp/udidl/admin/res/udid/ManuInfoResponse.java @@ -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; + +} diff --git a/src/main/java/com/glxp/udidl/admin/service/tyapi/TyDlHttpClient.java b/src/main/java/com/glxp/udidl/admin/service/tyapi/TyDlHttpClient.java new file mode 100644 index 0000000..83c59d5 --- /dev/null +++ b/src/main/java/com/glxp/udidl/admin/service/tyapi/TyDlHttpClient.java @@ -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 params = new HashMap<>(); + String response = okHttpCli.doGet(supplyUrl, params, headers); + BaseResponse baseResponse = JSONObject.parseObject(response, new TypeReference>() { + }); + if (baseResponse.getCode() == 20000) { + } else { + } + } + +} diff --git a/src/main/java/com/glxp/udidl/admin/util/OkHttpCli.java b/src/main/java/com/glxp/udidl/admin/util/OkHttpCli.java new file mode 100644 index 0000000..3810326 --- /dev/null +++ b/src/main/java/com/glxp/udidl/admin/util/OkHttpCli.java @@ -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 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 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 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 ""; + } + +} diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 8064ef9..15803a3 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -23,6 +23,13 @@ spring: max-idle: 10 min-idle: 5 max-wait: 5000ms +ok: + http: + connect-timeout: 3000 + read-timeout: 3000 + write-timeout: 3000 + max-idle-connections: 200 + keep-alive-duration: 300 servlet: multipart: @@ -40,6 +47,8 @@ config: downloadPath: E:/temp openAuth: false +TY_AUTHORIZATION: ac9d111c-3abd-4d8c-bf57-2df44952ab2e + # 阳光采购平台 udplat: host: http://pre-mcs.udplat.org/mcs-api diff --git a/src/main/resources/application-pro.yml b/src/main/resources/application-pro.yml index fab757b..f273f0b 100644 --- a/src/main/resources/application-pro.yml +++ b/src/main/resources/application-pro.yml @@ -17,6 +17,13 @@ spring: multipart: max-file-size: 100MB max-request-size: 1000MB +ok: + http: + connect-timeout: 3000 + read-timeout: 3000 + write-timeout: 3000 + max-idle-connections: 200 + keep-alive-duration: 300 logging: level: @@ -34,4 +41,4 @@ udplat: host: http://pre-mcs.udplat.org/mcs-api appId: 20004072 secretKey: hoGxLSEsSyysnS9 - userName: 福建片仔癀诊断技术有限公司 \ No newline at end of file + userName: 福建片仔癀诊断技术有限公司