package com.syzb.business.service; import cn.hutool.core.date.DatePattern; import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.crypto.SecureUtil; import cn.hutool.http.HttpRequest; import cn.hutool.json.JSONUtil; import com.syzb.business.vo.*; import com.syzb.common.handler.BizException; import com.syzb.common.result.ResponseStatus; import com.syzb.common.util.logger.LoggerUtil; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; // business信息Service @Service public class BusinessApiService { @Value("${business.api.host}") private String host; @Value("${business.api.url.loginUrl}") private String loginUrl; @Value("${business.api.url.getUserUrl}") private String getUserUrl; @Value("${business.api.url.getModuleListUrl}") private String getModuleListUrl; @Value("${business.api.url.getOrderListUrl}") private String getOrderListUrl; @Value("${business.api.url.getModuleUserListUrl}") private String getModuleUserListUrl; @Value("${business.api.authorizationDeptId}") private String authorizationDeptId; @Value("${business.api.authorizationKey}") private String authorizationKey; @Value("${business.api.timeout}") private int timeout = 10000; private static final int SUCCESS_CODE = 200; private static final int TOKEN_ERROR_CODE = 401; public BusinessLoginVO login(String userName, String password) { Map params = new HashMap<>(); params.put("username", userName); params.put("password", sha512(password)); BusinessLoginRspVO vo = post(loginUrl, params, BusinessLoginRspVO.class); return vo.getData(); } public BusinessUserVO getUser(String token) { Map params = new HashMap<>(); params.put("token", token); BusinessUserRspVO vo = post(getUserUrl, params, BusinessUserRspVO.class); return vo.getData(); } public List getModuleList(String userId) { Map params = new HashMap<>(); params.put("userId", userId); BusinessModuleRspVO vo = post(getModuleListUrl, params, BusinessModuleRspVO.class); return vo.getData(); } public List getOrderList(LocalDateTime startTime, LocalDateTime endTime) { List result = new ArrayList<>(); String startTimeStr = LocalDateTimeUtil.format(startTime, DatePattern.NORM_DATETIME_FORMATTER); String endTimeStr = LocalDateTimeUtil.format(endTime, DatePattern.NORM_DATETIME_FORMATTER); Integer currentPage = 1; Integer pageSize = 100; while (true) { BusinessOrderExtVO extVO = getOrderList(startTimeStr, endTimeStr, currentPage, pageSize); if (extVO == null || extVO.getData() == null) { break; } result.addAll(extVO.getData()); if (extVO.getData().size() < pageSize || result.size() >= extVO.getTotal()) { break; } currentPage++; } return result; } private BusinessOrderExtVO getOrderList(String startTime, String endTime, Integer currentPage, Integer pageSize) { Map params = new HashMap<>(); params.put("currentPage", currentPage); params.put("pageSize", pageSize); params.put("updateStartTime", startTime); params.put("updateEndTime", endTime); BusinessOrderRspVO vo = post(getOrderListUrl, params, BusinessOrderRspVO.class); return vo.getData(); } public List getModuleUserList(Integer moduleId, LocalDateTime startTime, LocalDateTime endTime) { List result = new ArrayList<>(); String startTimeStr = LocalDateTimeUtil.format(startTime, DatePattern.NORM_DATETIME_FORMATTER); String endTimeStr = LocalDateTimeUtil.format(endTime, DatePattern.NORM_DATETIME_FORMATTER); Integer currentPage = 1; Integer pageSize = 100; while (true) { BusinessModuleUserExtVO extVO = getModuleUserList(moduleId, startTimeStr, endTimeStr, currentPage, pageSize); if (extVO == null || extVO.getData() == null) { break; } result.addAll(extVO.getData()); if (extVO.getData().size() < pageSize || result.size() >= extVO.getTotal()) { break; } currentPage++; } return result; } private BusinessModuleUserExtVO getModuleUserList(Integer moduleId, String startTime, String endTime, Integer currentPage, Integer pageSize) { Map params = new HashMap<>(); params.put("moduleId", moduleId); params.put("currentPage", currentPage); params.put("pageSize", pageSize); params.put("updateStartTime", startTime); params.put("updateEndTime", endTime); BusinessModuleUserRspVO vo = post(getModuleUserListUrl, params, BusinessModuleUserRspVO.class); return vo.getData(); } private T post(String url, Map params, Class type) { T vo; try { if (params == null) { params = new HashMap<>(); } String body = JSONUtil.toJsonStr(params); Map> header = buildHeader(); LoggerUtil.api.info("url:" + url + ",body:" + body, ",header:" + JSONUtil.toJsonStr(header)); String response = HttpRequest.post(host + url) .body(body, "application/json") .header(header, true) .timeout(timeout)//超时,毫秒 .execute().body(); LoggerUtil.api.info("response:" + response); vo = JSONUtil.toBean(response, type); LoggerUtil.api.info("vo:" + JSONUtil.toJsonStr(vo)); if (vo.getCode() == null) { throw new BizException(ResponseStatus.OUTSYS_ERROR, "协议异常:" + vo.getMessage()); } else if (vo.getCode() == SUCCESS_CODE) { return vo; } else if (vo.getCode() == TOKEN_ERROR_CODE) { throw new BizException(ResponseStatus.SESSION_EXPIRY); } else { throw new BizException(ResponseStatus.OUTSYS_ERROR, vo.getCode() + ":" + vo.getMessage()); } } catch (BizException e) { throw e; } catch (Exception e) { throw new BizException(ResponseStatus.OUTSYS_ERROR, e); } } private String encodeSign(String time) { return SecureUtil.md5(authorizationKey + authorizationDeptId + time); } private static String sha512(String str) { try { // 获取SHA-512算法的MessageDigest实例 MessageDigest digest = MessageDigest.getInstance("SHA-512"); // 将输入字符串转换为字节数组并计算哈希值 byte[] hashBytes = digest.digest(str.getBytes(StandardCharsets.UTF_8)); // 将字节数组转换为十六进制字符串 StringBuilder hexString = new StringBuilder(); for (byte b : hashBytes) { String hex = Integer.toHexString(0xff & b); if (hex.length() == 1) hexString.append('0'); hexString.append(hex); } return hexString.toString(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } private Map> buildHeader() { Map> header = new HashMap<>(); String time = String.valueOf(System.currentTimeMillis()); header.put("Authorization-Dept-Id", List.of(authorizationDeptId)); header.put("Authorization-Sign-Time", List.of(time)); header.put("Authorization-Sign", List.of(encodeSign(time))); return header; } // @PostConstruct // public void test() { // BusinessLoginVO loginVO = login("sz545138", "Abc@123"); // String token = loginVO.getToken(); //// String token = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI1NDUxMzgiLCJpYXQiOjE3Mzk5NTcxMjYsImV4cCI6MTczOTk2MDcyNn0.02FnOeW7RnIeI7sqwTTw0H9NPj03iFRAkr9PPXwpaDMgRTK2v7krDqAYa6dSIkuGx8wnKWPp-kcA4Ajr_MLjdw"; // BusinessUserVO userVO = getUser(token); // System.out.println(JSONUtil.toJsonStr(userVO)); // List moduleList = getModuleList(userVO.getUserId().toString()); // System.out.println(JSONUtil.toJsonStr(moduleList)); // List orderList = getOrderList(LocalDate.now().withDayOfYear(1).atStartOfDay(), LocalDateTime.now()); // System.out.println(JSONUtil.toJsonStr(orderList)); // List moduleUserList = getModuleUserList(1000, LocalDate.now().withDayOfYear(1).atStartOfDay(), LocalDateTime.now()); // System.out.println(JSONUtil.toJsonStr(moduleUserList)); // } }