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();
|
}
|
}
|