Crunchy
2025-03-15 314ccc3fbb5b4cb1be4a77bc107a0ca1a2b2f0a3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.ruoyi.web.controller.tide;
 
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.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 {
    private final static String appId = "19472937-3039-4bf1-8334-13dc20e076e5";
 
    private final static String appSecret = "b57bbad60724f810e7bb7e1e7b8791b63eba50e50faef6fa819ade822fac9e4a";
 
    // 内网地址
    private final static String ip = "http://58.56.84.138: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);
            // 为x-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, appSecret.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", appId);
        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);
            // 为x-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, appSecret.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", appId);
        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 + "=======MES心跳执行成功!");
    }
 
    // 获取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", appId);
        json.put("appSecret", appSecret);
        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", appId);
        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();
    }
}