ScheduleUtil.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package com.xjrsoft.module.schedule.util;
  2. import cn.dev33.satoken.secure.SaSecureUtil;
  3. import lombok.extern.slf4j.Slf4j;
  4. import javax.crypto.Mac;
  5. import javax.crypto.spec.SecretKeySpec;
  6. import java.io.*;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9. import java.nio.charset.StandardCharsets;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. /**
  13. * @author dzx
  14. * @date 2024/1/9
  15. */
  16. @Slf4j
  17. public class ScheduleUtil {
  18. public static final String ALGORITHM = "HmacSHA256";
  19. public static final String apiUrl = "http://219.153.208.37:20000/api/v1/ScheduleFlowV2/OpenApi/";
  20. public static final String password = "@ak8To$xSHFoT6FoqsqYb3";
  21. public static final String secert = "UUXQ8G4W9IU";
  22. public static final String hostUrl = "http://219.153.208.37:20000";
  23. // private static JianyuekbConfig jianyuekbConfig;
  24. // public ScheduleUtil(JianyuekbConfig jianyuekbConfig){
  25. // this.jianyuekbConfig = jianyuekbConfig;
  26. // }
  27. private static String calculateHMac(String key, String data) throws Exception {
  28. Mac sha256_HMAC = Mac.getInstance(ALGORITHM);
  29. SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), ALGORITHM);
  30. sha256_HMAC.init(secret_key);
  31. return byteArrayToHex(sha256_HMAC.doFinal(data.getBytes(StandardCharsets.UTF_8)));
  32. }
  33. public static String byteArrayToHex(byte[] a) {
  34. StringBuilder sb = new StringBuilder(a.length * 2);
  35. for (byte b : a)
  36. sb.append(String.format("%02x", b));
  37. return sb.toString();
  38. }
  39. public static String createSign(Long timestamp) throws Exception {
  40. String md5Str = SaSecureUtil.md5(ScheduleUtil.password + timestamp);
  41. return calculateHMac(ScheduleUtil.secert, md5Str);
  42. }
  43. public static String doPost(String httpUrl, String param, String sign, Long timestamp) {
  44. HttpURLConnection connection = null;
  45. InputStream is = null;
  46. OutputStream os = null;
  47. BufferedReader br = null;
  48. String result = null;
  49. OutputStreamWriter writer = null;
  50. try {
  51. URL url = new URL(httpUrl);
  52. // 通过远程url连接对象打开连接
  53. connection = (HttpURLConnection) url.openConnection();
  54. connection.setRequestProperty("schoolId", secert);
  55. connection.setRequestProperty("sign", sign);
  56. connection.setRequestProperty("timestamp", String.format("%d", timestamp));
  57. // 设置连接请求方式
  58. connection.setRequestMethod("POST");
  59. // 设置连接主机服务器超时时间:15000毫秒
  60. connection.setConnectTimeout(15000);
  61. // 设置读取主机服务器返回数据超时时间:60000毫秒
  62. connection.setReadTimeout(60000);
  63. // 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
  64. connection.setDoOutput(true);
  65. // 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无
  66. connection.setDoInput(true);
  67. // 设置传入参数的格式:请求参数应该是 name1=value1&name2=value2 的形式。
  68. connection.setRequestProperty("Content-Type", "application/json");
  69. // 设置鉴权信息:Authorization: Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0
  70. // connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
  71. writer = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8);
  72. //body参数放这里
  73. writer.write(param);
  74. writer.flush();
  75. writer.close();
  76. if (connection.getResponseCode() == 200) {
  77. is = connection.getInputStream();
  78. // 对输入流对象进行包装:charset根据工作项目组的要求来设置
  79. br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
  80. StringBuffer sbf = new StringBuffer();
  81. String temp = null;
  82. // 循环遍历一行一行读取数据
  83. while ((temp = br.readLine()) != null) {
  84. sbf.append(temp);
  85. sbf.append("\r\n");
  86. }
  87. result = sbf.toString();
  88. } else {
  89. is = connection.getInputStream();
  90. // 对输入流对象进行包装:charset根据工作项目组的要求来设置
  91. br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
  92. StringBuffer sbf = new StringBuffer();
  93. String temp = null;
  94. // 循环遍历一行一行读取数据
  95. while ((temp = br.readLine()) != null) {
  96. sbf.append(temp);
  97. sbf.append("\r\n");
  98. }
  99. log.error(sbf.toString());
  100. }
  101. } catch (Exception e) {
  102. e.printStackTrace();
  103. } finally {
  104. // 关闭资源
  105. if (null != br) {
  106. try {
  107. br.close();
  108. } catch (IOException e) {
  109. e.printStackTrace();
  110. }
  111. }
  112. if (null != os) {
  113. try {
  114. os.close();
  115. } catch (IOException e) {
  116. e.printStackTrace();
  117. }
  118. }
  119. if (null != is) {
  120. try {
  121. is.close();
  122. } catch (IOException e) {
  123. e.printStackTrace();
  124. }
  125. }
  126. // 断开与远程地址url的连接
  127. connection.disconnect();
  128. }
  129. return result;
  130. }
  131. public static Map<Integer, String> getWeek() {
  132. Map<Integer, String> weekMap = new HashMap<>();
  133. weekMap.put(1, "周一");
  134. weekMap.put(2, "周二");
  135. weekMap.put(3, "周三");
  136. weekMap.put(4, "周四");
  137. weekMap.put(5, "周五");
  138. weekMap.put(6, "周六");
  139. weekMap.put(7, "周日");
  140. return weekMap;
  141. }
  142. public static Map<Integer, Integer> getTmePeriod() {
  143. Map<Integer, Integer> map = new HashMap<>();
  144. map.put(1, 0);
  145. map.put(2, 1);
  146. map.put(3, 2);
  147. map.put(4, 3);
  148. return map;
  149. }
  150. }