package com.upchina.business.service; import cn.hutool.crypto.SecureUtil; import cn.hutool.http.HttpRequest; import cn.hutool.json.JSONUtil; import com.upchina.business.vo.*; import com.upchina.common.handler.BizException; import com.upchina.common.result.ResponseStatus; import com.upchina.common.util.logger.LoggerUtil; import org.springframework.stereotype.Service; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.List; import java.util.Map; // business信息Service @Service public class BusinessApiService { private String host = "https://webdzapi.ceniu.sztg.com"; private String loginUrl = "/common/loginByUserName"; private String getUserUrl = "/common/getUserByToken"; private String getUserModuleListUrl = "/user/getUserModuleList"; private String authorizationDeptId = "16"; private String authorizationKey = "56e670eea5ff3a1aebbc02820e908ceb56e670eea5ff3a1aebbc02820e908ceb"; // private String authorizationDeptId = "33"; // // private String authorizationKey = "90804bd6089d950e8804c6d7cad7558a90804bd6089d950e8804c6d7cad7558a"; private static final int timeout = 10000; private static final int SUCCESS_CODE = 200; private static final int TOKEN_ERROR_CODE = 401; public BusinessLoginVO loginByUserName(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 getUserByToken(String token) { Map params = new HashMap<>(); params.put("token", token); BusinessUserRspVO vo = post(getUserUrl, params, BusinessUserRspVO.class); return vo.getData(); } public List getUserModuleList(String userId) { Map params = new HashMap<>(); params.put("userId", userId); BusinessModuleRspVO vo = post(getUserModuleListUrl, params, BusinessModuleRspVO.class); return vo.getData(); } public static void main(String[] args) { BusinessApiService service = new BusinessApiService(); BusinessLoginVO loginVO = service.loginByUserName("sz545138", "Abc@123"); String token = loginVO.getToken(); BusinessUserVO userVO = service.getUserByToken(token); System.out.println(JSONUtil.toJsonStr(userVO)); service.getUserModuleList(userVO.getUserId().toString()); } 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; } }