ScheduleUtil.java 6.2 KB

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