From 09394848ce262aff81897503d32ec334fd3f2b6f Mon Sep 17 00:00:00 2001
From: zouyu <2723363702@qq.com>
Date: 星期二, 20 一月 2026 15:59:30 +0800
Subject: [PATCH] Merge branch 'dev_tide' into dev_tide_zlglxt_xinlan
---
src/main/java/com/ruoyi/tide/utils/TideUtils.java | 191 +++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 191 insertions(+), 0 deletions(-)
diff --git a/src/main/java/com/ruoyi/tide/utils/TideUtils.java b/src/main/java/com/ruoyi/tide/utils/TideUtils.java
new file mode 100644
index 0000000..10f84ab
--- /dev/null
+++ b/src/main/java/com/ruoyi/tide/utils/TideUtils.java
@@ -0,0 +1,191 @@
+package com.ruoyi.tide.utils;
+
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.RandomUtil;
+import cn.hutool.crypto.digest.HMac;
+import cn.hutool.crypto.digest.HmacAlgorithm;
+import cn.hutool.http.HttpRequest;
+import com.alibaba.fastjson2.JSONObject;
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
+import lombok.Data;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+import java.util.HashMap;
+
+@Data
+@Component
+@Slf4j
+public class TideUtils {
+
+ public static String APP_ID;
+ public static String APP_SECRET;
+
+
+ @Value("${inspur.appId}")
+ private String appId;
+
+ @Value("${inspur.appSecret}")
+ private String appSecret;
+
+ @Value("${inspur.appId}")
+ public void setAppId(String appId) {
+ TideUtils.APP_ID = appId;
+ }
+
+ @Value("${inspur.appSecret}")
+ public void setAppSecret(String appSecret) {
+ TideUtils.APP_SECRET = appSecret;
+ }
+
+ // 鍐呯綉鍦板潃
+ private final static String ip = "http://10.136.0.8:8083";
+
+ // MD5鍔犲瘑骞惰浆鎹负16杩涘埗
+ public static String md5Encryption(String input) {
+ try {
+ MessageDigest md = MessageDigest.getInstance("MD5");
+ byte[] digest = md.digest(input.getBytes());
+ StringBuilder sb = new StringBuilder();
+ for (byte b : digest) {
+ sb.append(String.format("%02x", b & 0xff));
+ }
+ return sb.toString();
+ } catch (NoSuchAlgorithmException e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ // get璇锋眰
+ public static HashMap<String, String> getGetHeader(String parameter) {
+ // 鏃堕棿鎴�
+ String xTime = String.valueOf(DateUtil.current());
+ // 鑾峰彇闅忔満16浣嶅瓧绗︿覆
+ String xRandom = RandomUtil.randomString(16);
+ String xSignSplicingTogether;
+ // 鏈夊弬鏁�
+ if (ObjectUtils.isNotEmpty(parameter)) {
+ // 鍔犲瘑寰楀嚭x-body
+ String xBody = md5Encryption(parameter);
+ // 涓簒-sign鍔犲瘑鍋氭嫾鎺�
+ xSignSplicingTogether = "x-body=" + xBody + "&x-random=" + xRandom + "&x-time=" + xTime;
+ // 鏃犲弬鏁�
+ } else {
+ xSignSplicingTogether = "x-random=" + xRandom + "&x-time=" + xTime;
+ }
+ // 閰嶇疆鍔犲瘑鏂瑰紡涓庡瘑閽�
+ HMac hMac = new HMac(HmacAlgorithm.HmacSHA256, APP_SECRET.getBytes());
+ // 寰楀嚭x-sign
+ String xSign = hMac.digestHex(xSignSplicingTogether);
+ HashMap<String, String> result = new HashMap<>();
+ result.put("x-time", xTime);
+ result.put("x-random", xRandom);
+ result.put("x-sign", xSign);
+ result.put("appKey", APP_ID);
+ return result;
+ }
+
+ // post璇锋眰
+ public static HashMap<String, String> getPostHeader(String parameter) {
+ // 鏃堕棿鎴�
+ String xTime = String.valueOf(DateUtil.current());
+ // 鑾峰彇闅忔満16浣嶅瓧绗︿覆
+ String xRandom = RandomUtil.randomString(16);
+ String xSignSplicingTogether;
+ // 鏈夊弬鏁�
+ if (ObjectUtils.isNotEmpty(parameter)) {
+ // 鍔犲瘑寰楀嚭x-body
+ String xBody = md5Encryption(parameter);
+ // 涓簒-sign鍔犲瘑鍋氭嫾鎺�
+ xSignSplicingTogether = "x-body=" + xBody + "&x-random=" + xRandom + "&x-time=" + xTime;
+ // 鏃犲弬鏁�
+ } else {
+ xSignSplicingTogether = "x-random=" + xRandom + "&x-time=" + xTime;
+ }
+ // x-sign 鍔犲瘑
+ HMac hMac = new HMac(HmacAlgorithm.HmacSHA256, APP_SECRET.getBytes());
+ String xSign = hMac.digestHex(xSignSplicingTogether);
+ HashMap<String, String> result = new HashMap<>();
+ result.put("x-time", xTime);
+ result.put("x-random", xRandom);
+ result.put("x-sign", xSign);
+ result.put("appKey", APP_ID);
+ return result;
+ }
+
+ /**
+ * 浜斿垎閽熶竴娆$殑蹇冭烦
+ */
+ @Scheduled(cron = "0 0/5 * * * ?")
+ public static void heartbeat(){
+ HashMap<String, String> header = getGetHeader(null);
+ String url = ip + "/cpn/api/extra/v1/application/heartbeat";
+ String body = HttpRequest.get(url).headerMap(header, false).execute().body();
+ System.out.println(body + "搴旂敤蹇冭烦鎵ц鎴愬姛锛�");
+ }
+
+ // 鑾峰彇token
+ public static String getToken(String code) {
+ String url = ip + "/cpn/extral/applicationCode/appAuthCheck";
+ JSONObject json = new JSONObject();
+ json.put("code", code);
+ json.put("appID", APP_ID);
+ json.put("appSecret", APP_SECRET);
+ HashMap<String, String> header = getPostHeader(json.toString());
+ String body = HttpRequest.post(url)
+ .headerMap(header, false)
+ .body(json.toString())
+ .execute().body();
+ JSONObject jsonObject = JSONObject.parseObject(body);
+ return jsonObject.get("data").toString();
+ }
+
+ // 鑾峰彇鐢ㄦ埛淇℃伅
+ public static JSONObject getUserInfo(String token) {
+ String url = ip + "/cpn/api/extral/applicationCode/getUserInfoByToken";
+ JSONObject json = new JSONObject();
+ json.put("appID", APP_ID);
+ HashMap<String, String> header = getPostHeader(json.toString());
+ header.put("Authorization", token);
+ String body = HttpRequest.post(url)
+ .headerMap(header, false)
+ .body(json.toString())
+ .execute().body();
+ JSONObject jsonObject = JSONObject.parseObject(body);
+ return JSONObject.parseObject(jsonObject.get("data").toString());
+ }
+
+ public static JSONObject getResult(Integer code, String msg, Object data) {
+ JSONObject json = new JSONObject();
+ json.put("code", code);
+ json.put("msg", msg);
+ json.put("data", data);
+ String xRandom = RandomUtil.randomString(16);
+ json.put("traceId", xRandom);
+ return json;
+ }
+
+ public static String getRandomString(int length) {
+ SecureRandom random = new SecureRandom();
+ String uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ String lowercaseLetters = "abcdefghijklmnopqrstuvwxyz";
+ String numbers = "0123456789";
+ String specialCharacters = "!@$%^&*()_+-=.";
+ String characterSet = uppercaseLetters + lowercaseLetters + numbers + specialCharacters;
+ StringBuilder passwordBuilder = new StringBuilder();
+
+ for (int i = 0; i < length; i++) {
+ int randomIndex = random.nextInt(characterSet.length());
+ char randomChar = characterSet.charAt(randomIndex);
+ passwordBuilder.append(randomChar);
+ }
+
+ return passwordBuilder.toString();
+ }
+}
\ No newline at end of file
--
Gitblit v1.9.3