From 17e6f9cfcf1ead7896750cd3ff4679ac28c16f1e Mon Sep 17 00:00:00 2001 From: 199901012 Date: Thu, 1 Apr 2021 00:17:54 +0800 Subject: [PATCH] =?UTF-8?q?'=E6=8F=90=E4=BA=A4diam'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hai-cweb/src/test/common/RegionTest.java | 43 ++++++--- .../com/hai/schedule/SynchronizeCNPC.java | 91 +++++++++++++++++++ .../java/com/hai/common/utils/HttpsUtils.java | 38 ++++++++ 3 files changed, 159 insertions(+), 13 deletions(-) create mode 100644 hai-schedule/src/main/java/com/hai/schedule/SynchronizeCNPC.java diff --git a/hai-cweb/src/test/common/RegionTest.java b/hai-cweb/src/test/common/RegionTest.java index 5bcf357b..e24a80f0 100644 --- a/hai-cweb/src/test/common/RegionTest.java +++ b/hai-cweb/src/test/common/RegionTest.java @@ -76,20 +76,37 @@ public class RegionTest { public void test(){ try { - Map map = new HashMap<>(); - map.put("appId", "js3w7K4W0SglB6bNPVjs3w7K4W0SglB6bNPV"); - map.put("appSecret", "SoIXquKSKT8s7sa6ySjfyBXxKTC4Jhqx7fiE1qT0XWrrnkScurMGsLdGrz5ThLo5"); - Long date = new Date().getTime(); - String signature = "mUGcV7CxPYRuzIWXd72bVknKYCXuT7d1tRxQUdVR9iAcxxTdmWC8lTo0rL5wDZwLmTwQnpCqeHz3MtAIVBrm99WmtPv93pRdgWcIkkgPt18pAFyavYrPt80Yh8iyFr8s"; - String sha256 = encodeBySHA256(signature + JSON.toJSONString(map) + date); - - - Map map2 = new HashMap<>(); - map2.put("signature", signature); - map2.put("body", JSON.toJSONString(map)); - map2.put("ts", date); - JSONObject jsonObject = HttpsUtils.doPost("https://app.zshcqsy.com/api-provider/api/open/merchant/token", JSON.toJSONString(map2)); + Map tokenMap = new HashMap<>(); + tokenMap.put("appId", "jsVpwDoHZfR2rrpjhA"); + tokenMap.put("appSecret", "R7yJjinikdPhOlTrVgmdS7cblWMMOf99zEJkfPgaTIoDtKGlX51JKueLV725a6S4"); + JSONObject jsonObject = HttpsUtils.doPost("https://app.zshcqsy.com/api-provider/api/open/merchant/token", JSON.toJSONString(tokenMap)); System.out.println(jsonObject.toJSONString()); + if(jsonObject != null && jsonObject.getBoolean("success") == true) { + JSONObject data = jsonObject.getJSONObject("data"); + String token = data.getString("token"); + + Map bodyMap = new HashMap<>(); + bodyMap.put("appId", "jsVpwDoHZfR2rrpjhA"); + bodyMap.put("pageNo", 1); + bodyMap.put("pageSize", 10); + bodyMap.put("startTime", "2021-3-31 00:00:00"); + bodyMap.put("endTime", "2021-3-31 23:00:00"); + bodyMap.put("customerCode", "48464274"); + + String signature = "hAFO76ZqXmIqosNw3PTAP4wxGJTN7MPawSU0QFDzzNioLjKMEaVFDlNKRIQX6n0DgyoVMy170T0b13P8uUTAQsbk9UNRPFO5i0fWp1hHOhNdaaQ2BdC0WXQz5QQ5epyK"; + + Long date = new Date().getTime(); + String sha256 = encodeBySHA256(signature + JSON.toJSONString(bodyMap) + date); + + Map headerMap = new HashMap<>(); + headerMap.put("sign", sha256); + headerMap.put("token", token); + headerMap.put("ts", date); + //map2.put("sign", sha256); + HttpsUtils.doPost("https://app.zshcqsy.com/api-provider/sapapi/open/coupon/customerRedeemcodeList", bodyMap, headerMap); + //System.out.println(jsonObject.toJSONString()); + } + }catch (Exception e){ e.printStackTrace(); diff --git a/hai-schedule/src/main/java/com/hai/schedule/SynchronizeCNPC.java b/hai-schedule/src/main/java/com/hai/schedule/SynchronizeCNPC.java new file mode 100644 index 00000000..2ddd87e9 --- /dev/null +++ b/hai-schedule/src/main/java/com/hai/schedule/SynchronizeCNPC.java @@ -0,0 +1,91 @@ +package com.hai.schedule; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.hai.common.utils.HttpsUtils; +import com.hai.entity.HighOrder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.Scheduled; + +import java.io.UnsupportedEncodingException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @Auther: 胡锐 + * @Description: 同步中石化接口 + * @Date: 2021/3/31 23:49 + */ +@Configuration +public class SynchronizeCNPC { + + private static Logger log = LoggerFactory.getLogger(SynchronizeCNPC.class); + + private static final String[] HEX_DIGITS = {"0" ,"1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}; + + @Scheduled(cron="0 0/1 * * * ?") //每1分钟执行一次 + public void certification() { + Map tokenMap = new HashMap<>(); + tokenMap.put("appId", "jsVpwDoHZfR2rrpjhA"); + tokenMap.put("appSecret", "R7yJjinikdPhOlTrVgmdS7cblWMMOf99zEJkfPgaTIoDtKGlX51JKueLV725a6S4"); + JSONObject jsonObject = HttpsUtils.doPost("https://app.zshcqsy.com/api-provider/api/open/merchant/token", JSON.toJSONString(tokenMap)); + System.out.println(jsonObject.toJSONString()); + if (jsonObject != null && jsonObject.getBoolean("success") == true) { + JSONObject data = jsonObject.getJSONObject("data"); + String token = data.getString("token"); + + Map bodyMap = new HashMap<>(); + bodyMap.put("appId", "jsVpwDoHZfR2rrpjhA"); + bodyMap.put("pageNo", 1); + bodyMap.put("pageSize", 10); + bodyMap.put("startTime", "2021-3-31 00:00:00"); + bodyMap.put("endTime", "2021-3-31 23:00:00"); + bodyMap.put("customerCode", "48464274"); + + String signature = "hAFO76ZqXmIqosNw3PTAP4wxGJTN7MPawSU0QFDzzNioLjKMEaVFDlNKRIQX6n0DgyoVMy170T0b13P8uUTAQsbk9UNRPFO5i0fWp1hHOhNdaaQ2BdC0WXQz5QQ5epyK"; + + Long date = new Date().getTime(); + String sha256 = encodeBySHA256(signature + JSON.toJSONString(bodyMap) + date); + + Map headerMap = new HashMap<>(); + headerMap.put("sign", sha256); + headerMap.put("token", token); + headerMap.put("ts", date); + log.info(HttpsUtils.doPost("https://app.zshcqsy.com/api-provider/sapapi/open/coupon/customerRedeemcodeList", bodyMap, headerMap).toJSONString()); + } + } + + public String encodeBySHA256(String str) { + try{ + MessageDigest messageDigest = MessageDigest.getInstance("SHA-256"); + messageDigest.reset(); + messageDigest.update(str.getBytes("UTF-8")); + return getFormattedText(messageDigest.digest()); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } catch (Exception e) { + throw new RuntimeException(e); + } + return ""; + } + + private String getFormattedText(byte[] bytes) { + int len = bytes.length; + StringBuilder buf = new StringBuilder(len * 2); + // 把密文转换成十六进制的字符串形式 + for (int j=0;j> 4) & 0x0f]); + buf.append(HEX_DIGITS[bytes[j] & 0x0f]); + } + return buf.toString(); + } + +} diff --git a/hai-service/src/main/java/com/hai/common/utils/HttpsUtils.java b/hai-service/src/main/java/com/hai/common/utils/HttpsUtils.java index 89e0861e..8c442de3 100644 --- a/hai-service/src/main/java/com/hai/common/utils/HttpsUtils.java +++ b/hai-service/src/main/java/com/hai/common/utils/HttpsUtils.java @@ -221,6 +221,44 @@ public class HttpsUtils { return null; } + public static JSONObject doPost(String apiUrl, Map body, Map header) { + CloseableHttpClient httpClient = null; + if (apiUrl.startsWith("https")) { + httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()) + .setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build(); + } else { + httpClient = HttpClients.createDefault(); + } + String httpStr = null; + CloseableHttpResponse response = null; + + try { + HttpPost httpPost = new HttpPost(apiUrl); + httpPost.setConfig(requestConfig); + for (Map.Entry entry : header.entrySet()) { + httpPost.setHeader(entry.getKey(), entry.getValue().toString()); + } + StringEntity stringEntity = new StringEntity(JSON.toJSONString(body), "UTF-8");// 解决中文乱码问题 + stringEntity.setContentEncoding("UTF-8"); + stringEntity.setContentType("application/json"); + httpPost.setEntity(stringEntity); + response = httpClient.execute(httpPost); + HttpEntity entity = response.getEntity(); + httpStr = EntityUtils.toString(entity, "UTF-8"); + return JSON.parseObject(httpStr); + } catch (Exception e) { + log.error(e.getMessage(),e); + } finally { + if (response != null) { + try { + EntityUtils.consume(response.getEntity()); + } catch (IOException e) { + log.error(e.getMessage(),e); + } + } + } + return null; + } /** * 发送 POST 请求,JSON形式 *