package com.xjrsoft.module.schedule.util; import cn.dev33.satoken.secure.SaSecureUtil; import lombok.extern.slf4j.Slf4j; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; /** * @author dzx * @date 2024/1/9 */ @Slf4j public class ScheduleUtil { public static final String ALGORITHM = "HmacSHA256"; public static final String apiUrl = "http://219.153.208.37:20000/api/v1/ScheduleFlowV2/OpenApi/"; public static final String password = "@ak8To$xSHFoT6FoqsqYb3"; public static final String secert = "UUXQ8G4W9IU"; public static final String hostUrl = "http://219.153.208.37:20000"; // private static JianyuekbConfig jianyuekbConfig; // public ScheduleUtil(JianyuekbConfig jianyuekbConfig){ // this.jianyuekbConfig = jianyuekbConfig; // } private static String calculateHMac(String key, String data) throws Exception { Mac sha256_HMAC = Mac.getInstance(ALGORITHM); SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), ALGORITHM); sha256_HMAC.init(secret_key); return byteArrayToHex(sha256_HMAC.doFinal(data.getBytes(StandardCharsets.UTF_8))); } public static String byteArrayToHex(byte[] a) { StringBuilder sb = new StringBuilder(a.length * 2); for (byte b : a) sb.append(String.format("%02x", b)); return sb.toString(); } public static String createSign(Long timestamp) throws Exception { String md5Str = SaSecureUtil.md5(ScheduleUtil.password + timestamp); return calculateHMac(ScheduleUtil.secert, md5Str); } public static String doPost(String httpUrl, String param, String sign, Long timestamp) { HttpURLConnection connection = null; InputStream is = null; OutputStream os = null; BufferedReader br = null; String result = null; OutputStreamWriter writer = null; try { URL url = new URL(httpUrl); // 通过远程url连接对象打开连接 connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("schoolId", secert); connection.setRequestProperty("sign", sign); connection.setRequestProperty("timestamp", String.format("%d", timestamp)); // 设置连接请求方式 connection.setRequestMethod("POST"); // 设置连接主机服务器超时时间:15000毫秒 connection.setConnectTimeout(15000); // 设置读取主机服务器返回数据超时时间:60000毫秒 connection.setReadTimeout(60000); // 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true connection.setDoOutput(true); // 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无 connection.setDoInput(true); // 设置传入参数的格式:请求参数应该是 name1=value1&name2=value2 的形式。 connection.setRequestProperty("Content-Type", "application/json"); // 设置鉴权信息:Authorization: Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0 // connection.setRequestProperty("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0"); writer = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8); //body参数放这里 writer.write(param); writer.flush(); writer.close(); if (connection.getResponseCode() == 200) { is = connection.getInputStream(); // 对输入流对象进行包装:charset根据工作项目组的要求来设置 br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)); StringBuffer sbf = new StringBuffer(); String temp = null; // 循环遍历一行一行读取数据 while ((temp = br.readLine()) != null) { sbf.append(temp); sbf.append("\r\n"); } result = sbf.toString(); } else { is = connection.getInputStream(); // 对输入流对象进行包装:charset根据工作项目组的要求来设置 br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)); StringBuffer sbf = new StringBuffer(); String temp = null; // 循环遍历一行一行读取数据 while ((temp = br.readLine()) != null) { sbf.append(temp); sbf.append("\r\n"); } log.error(sbf.toString()); } } catch (Exception e) { e.printStackTrace(); } finally { // 关闭资源 if (null != br) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != os) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != is) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } // 断开与远程地址url的连接 connection.disconnect(); } return result; } public static Map getWeek() { Map weekMap = new HashMap<>(); weekMap.put(1, "周一"); weekMap.put(2, "周二"); weekMap.put(3, "周三"); weekMap.put(4, "周四"); weekMap.put(5, "周五"); weekMap.put(6, "周六"); weekMap.put(7, "周日"); return weekMap; } public static Map getTmePeriod() { Map map = new HashMap<>(); map.put(1, 0); map.put(2, 1); map.put(3, 2); map.put(4, 3); return map; } }