AdvisorServer/src/main/java/com/upchina/business/service/BusinessApiService.java

124 lines
4.7 KiB
Java
Raw Normal View History

2025-02-15 17:29:47 +08:00
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 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;
public BusinessLoginVO loginByUserName(String userName, String password) {
Map<String, Object> 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<String, Object> params = new HashMap<>();
params.put("token", token);
BusinessUserRspVO vo = post(getUserUrl, params, BusinessUserRspVO.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));
}
private <T extends BusinessVO> T post(String url, Map<String, Object> params, Class<T> type) {
T vo;
try {
if (params == null) {
params = new HashMap<>();
}
String body = JSONUtil.toJsonStr(params);
Map<String, List<String>> 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 && vo.getCode() != SUCCESS_CODE) {
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);
}
return vo;
}
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<String, List<String>> buildHeader() {
Map<String, List<String>> 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;
}
}