java-mall/client/src/main/java/com/small/client/Utils/CommonUtil.java
2025-07-19 14:24:13 +08:00

144 lines
4.2 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2025. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.small.client.Utils;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.apache.tomcat.util.codec.binary.Base64;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
@Slf4j
public class CommonUtil {
private final static String apiUrl = "http://4ei8850868ux.vicp.fun";
public static JSONObject sendPostRequestToSiXun(String urlPath, JSONObject params) {
String resp = HttpUtil.post(apiUrl + urlPath, params.toString());
if (StrUtil.isBlank(resp)) {
return null;
}
JSONObject respObj = JSONUtil.parseObj(resp);
return respObj;
}
/**
* 根据总条数和分页大小,求页数
*
* @param total
* @param pageSize
* @return
*/
public static Integer getPagesCount(Integer total, Integer pageSize) {
if (total == null || pageSize == null || pageSize <= 0 || total <= 0) {
return 0;
}
int pagesCount = 0;
pagesCount = total / pageSize;
if (total % pageSize > 0) {
// 有余数
pagesCount++;
} else {
if (pagesCount == 0) {
pagesCount = 1;
}
}
return pagesCount;
}
/**
* 接口是否成功执行返回
*
* @param jsonObject
* @return
*/
public static Boolean isSuccess(JSONObject jsonObject) {
if (jsonObject == null) {
return false;
}
return jsonObject.get("code") != null && jsonObject.getStr("code").equals("0");
}
/**
* 接口是否成功执行返回
*
* @param jsonObject
* @return
*/
public static Boolean hasSuccessData(JSONObject jsonObject) {
if (jsonObject == null) {
return false;
}
return jsonObject.get("code") != null && jsonObject.getStr("code").equals("0") && jsonObject.get("data") != null;
}
/**
* 通过json节点表达式获取节点json字符串驼峰命名改成下划线命名
*
* @param jsonObject
* @param expression json 节点表达式比如: data.list, msg, code
* @return
*/
public static String toUnderlineJson(JSONObject jsonObject, String expression) {
return StrUtil.toUnderlineCase(jsonObject.getByPath(expression, String.class));
}
/**
* 生成 md 摘要通用签名(参考了顺丰同城的做法)
* 参考https://openic.sf-express.com/#/quickstart
*
* @param postData
* @param appId
* @param appKey
* @return
*/
public static String generateOpenSign(String postData, String appId, String appKey) {
try {
if (StrUtil.isBlank(postData) || StrUtil.isBlank(appId) || StrUtil.isBlank(appKey)) {
log.error("生成签名时缺少必要参数!");
return Strings.EMPTY;
}
String sb = postData + "&" + appId + "&" + appKey;
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] md5 = md.digest(sb.getBytes(StandardCharsets.UTF_8));
int i;
StringBuffer buf = new StringBuffer();
for (byte b : md5) {
i = b;
if (i < 0) {
i += 256;
}
if (i < 16) {
buf.append("0");
}
buf.append(Integer.toHexString(i));
}
return Base64.encodeBase64String(buf.toString().getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
log.error(e.getMessage());
return Strings.EMPTY;
}
}
}