| 
									
										
										
										
											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";
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-19 16:05:27 +08:00
										 |  |  |     private String getUserModuleListUrl = "/user/getUserModuleList";
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-15 17:29:47 +08:00
										 |  |  |     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;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-19 16:05:27 +08:00
										 |  |  |     private static final int TOKEN_ERROR_CODE = 401;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-15 17:29:47 +08:00
										 |  |  |     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();
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-19 16:05:27 +08:00
										 |  |  |     public List<BusinessModuleVO> getUserModuleList(String userId) {
 | 
					
						
							|  |  |  |         Map<String, Object> params = new HashMap<>();
 | 
					
						
							|  |  |  |         params.put("userId", userId);
 | 
					
						
							|  |  |  |         BusinessModuleRspVO vo = post(getUserModuleListUrl, params, BusinessModuleRspVO.class);
 | 
					
						
							|  |  |  |         return vo.getData();
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-15 17:29:47 +08:00
										 |  |  |     public static void main(String[] args) {
 | 
					
						
							|  |  |  |         BusinessApiService service = new BusinessApiService();
 | 
					
						
							| 
									
										
										
										
											2025-02-19 17:53:25 +08:00
										 |  |  | //        BusinessLoginVO loginVO = service.loginByUserName("sz545138", "Abc@123");
 | 
					
						
							|  |  |  | //        String token = loginVO.getToken();
 | 
					
						
							|  |  |  |         String token = "eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI1NDUxMzgiLCJpYXQiOjE3Mzk5NTcxMjYsImV4cCI6MTczOTk2MDcyNn0.02FnOeW7RnIeI7sqwTTw0H9NPj03iFRAkr9PPXwpaDMgRTK2v7krDqAYa6dSIkuGx8wnKWPp-kcA4Ajr_MLjdw";
 | 
					
						
							| 
									
										
										
										
											2025-02-15 17:29:47 +08:00
										 |  |  |         BusinessUserVO userVO = service.getUserByToken(token);
 | 
					
						
							|  |  |  |         System.out.println(JSONUtil.toJsonStr(userVO));
 | 
					
						
							| 
									
										
										
										
											2025-02-19 16:05:27 +08:00
										 |  |  |         service.getUserModuleList(userVO.getUserId().toString());
 | 
					
						
							| 
									
										
										
										
											2025-02-15 17:29:47 +08:00
										 |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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));
 | 
					
						
							| 
									
										
										
										
											2025-02-19 16:05:27 +08:00
										 |  |  |             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 {
 | 
					
						
							| 
									
										
										
										
											2025-02-15 17:29:47 +08:00
										 |  |  |                 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<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;
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }
 |