298 lines
10 KiB
Java
298 lines
10 KiB
Java
package com.rj.util;
|
||
|
||
import com.rj.common.LbThirdIntegrationConstants;
|
||
import com.rj.dto.hxr.BuyerOrderListEndpoint;
|
||
import com.rj.entity.LbThirdIntegrationConfig;
|
||
import org.springframework.util.StringUtils;
|
||
|
||
import java.util.HashMap;
|
||
import java.util.Map;
|
||
|
||
/**
|
||
* 从 {@link LbThirdIntegrationConfig} 解析第三方 API 地址与请求头。
|
||
*/
|
||
public final class LbThirdIntegrationConfigUtil {
|
||
|
||
private LbThirdIntegrationConfigUtil() {
|
||
}
|
||
|
||
public static String resolveGoodsApiBaseUrl(LbThirdIntegrationConfig config) {
|
||
String base = trimTrailingSlash(requireText(config.getAdminBaseUrl(), "adminBaseUrl"));
|
||
String path = config.getGoodsApiPath();
|
||
if (!StringUtils.hasText(path)) {
|
||
path = LbThirdIntegrationConstants.DEFAULT_GOODS_API_PATH;
|
||
}
|
||
path = path.trim();
|
||
if (!path.startsWith("/")) {
|
||
path = "/" + path;
|
||
}
|
||
return base + path;
|
||
}
|
||
|
||
public static String resolveBuyApiUrl(LbThirdIntegrationConfig config) {
|
||
String base = trimTrailingSlash(requireText(config.getAdminBaseUrl(), "adminBaseUrl"));
|
||
String path = config.getBuyApiPath();
|
||
if (!StringUtils.hasText(path)) {
|
||
path = LbThirdIntegrationConstants.DEFAULT_BUY_API_PATH;
|
||
}
|
||
path = path.trim();
|
||
if (!path.startsWith("/")) {
|
||
path = "/" + path;
|
||
}
|
||
return base + path;
|
||
}
|
||
|
||
public static String resolveGoodsApiOrigin(LbThirdIntegrationConfig config) {
|
||
if (StringUtils.hasText(config.getGoodsApiOrigin())) {
|
||
return config.getGoodsApiOrigin().trim();
|
||
}
|
||
return trimTrailingSlash(requireText(config.getWebBaseUrl(), "webBaseUrl"));
|
||
}
|
||
|
||
public static String resolveGoodsApiReferer(LbThirdIntegrationConfig config) {
|
||
if (StringUtils.hasText(config.getGoodsApiReferer())) {
|
||
return config.getGoodsApiReferer().trim();
|
||
}
|
||
return trimTrailingSlash(requireText(config.getWebBaseUrl(), "webBaseUrl")) + "/";
|
||
}
|
||
|
||
public static int resolveGoodsPageLimit(LbThirdIntegrationConfig config) {
|
||
Integer limit = config.getGoodsPageLimit();
|
||
if (limit == null || limit < 1) {
|
||
return LbThirdIntegrationConstants.DEFAULT_GOODS_PAGE_LIMIT;
|
||
}
|
||
return limit;
|
||
}
|
||
|
||
public static String resolveFansApiBaseUrl(LbThirdIntegrationConfig config) {
|
||
String base = trimTrailingSlash(requireText(config.getAdminBaseUrl(), "adminBaseUrl"));
|
||
String path = config.getFansApiPath();
|
||
if (!StringUtils.hasText(path)) {
|
||
path = LbThirdIntegrationConstants.DEFAULT_FANS_API_PATH;
|
||
}
|
||
path = path.trim();
|
||
if (!path.startsWith("/")) {
|
||
path = "/" + path;
|
||
}
|
||
return base + path;
|
||
}
|
||
|
||
public static String resolveLoginApiUrl(LbThirdIntegrationConfig config) {
|
||
String base = trimTrailingSlash(requireText(config.getAdminBaseUrl(), "adminBaseUrl"));
|
||
String path = config.getLoginApiPath();
|
||
if (!StringUtils.hasText(path)) {
|
||
path = LbThirdIntegrationConstants.DEFAULT_LOGIN_API_PATH;
|
||
}
|
||
path = path.trim();
|
||
if (!path.startsWith("/")) {
|
||
path = "/" + path;
|
||
}
|
||
return base + path;
|
||
}
|
||
|
||
public static int resolveFansPageLimit(LbThirdIntegrationConfig config) {
|
||
Integer limit = config.getFansPageLimit();
|
||
if (limit == null || limit < 1) {
|
||
return LbThirdIntegrationConstants.DEFAULT_FANS_PAGE_LIMIT;
|
||
}
|
||
return limit;
|
||
}
|
||
|
||
public static String resolveUserSelectBaseUrl(LbThirdIntegrationConfig config) {
|
||
String base = trimTrailingSlash(requireText(config.getAdminBaseUrl(), "adminBaseUrl"));
|
||
String path = config.getUserSelectPath();
|
||
if (!StringUtils.hasText(path)) {
|
||
path = LbThirdIntegrationConstants.DEFAULT_USER_SELECT_PATH;
|
||
}
|
||
path = path.trim();
|
||
if (!path.startsWith("/")) {
|
||
path = "/" + path;
|
||
}
|
||
return base + path;
|
||
}
|
||
|
||
public static String resolveUserUpdateUrl(LbThirdIntegrationConfig config) {
|
||
String base = trimTrailingSlash(requireText(config.getAdminBaseUrl(), "adminBaseUrl"));
|
||
String path = config.getUserUpdatePath();
|
||
if (!StringUtils.hasText(path)) {
|
||
path = LbThirdIntegrationConstants.DEFAULT_USER_UPDATE_PATH;
|
||
}
|
||
path = path.trim();
|
||
if (!path.startsWith("/")) {
|
||
path = "/" + path;
|
||
}
|
||
return base + path;
|
||
}
|
||
|
||
public static String resolveUserReferer(LbThirdIntegrationConfig config) {
|
||
if (StringUtils.hasText(config.getUserReferer())) {
|
||
return config.getUserReferer().trim();
|
||
}
|
||
return trimTrailingSlash(requireText(config.getAdminBaseUrl(), "adminBaseUrl"))
|
||
+ "/app/admin/user/index";
|
||
}
|
||
|
||
public static int resolveUserPageLimit(LbThirdIntegrationConfig config) {
|
||
Integer limit = config.getUserPageLimit();
|
||
if (limit == null || limit < 1) {
|
||
return LbThirdIntegrationConstants.DEFAULT_USER_PAGE_LIMIT;
|
||
}
|
||
return limit;
|
||
}
|
||
|
||
public static String resolveOrderSelectBaseUrl(LbThirdIntegrationConfig config) {
|
||
String base = trimTrailingSlash(requireText(config.getAdminBaseUrl(), "adminBaseUrl"));
|
||
String path = config.getOrderSelectPath();
|
||
if (!StringUtils.hasText(path)) {
|
||
path = LbThirdIntegrationConstants.DEFAULT_ORDER_SELECT_PATH;
|
||
}
|
||
path = path.trim();
|
||
if (!path.startsWith("/")) {
|
||
path = "/" + path;
|
||
}
|
||
return base + path;
|
||
}
|
||
|
||
public static String resolveOrderReferer(LbThirdIntegrationConfig config) {
|
||
if (StringUtils.hasText(config.getOrderReferer())) {
|
||
return config.getOrderReferer().trim();
|
||
}
|
||
return trimTrailingSlash(requireText(config.getAdminBaseUrl(), "adminBaseUrl"))
|
||
+ "/app/admin/order/index";
|
||
}
|
||
|
||
public static int resolveOrderPageLimit(LbThirdIntegrationConfig config) {
|
||
Integer limit = config.getOrderPageLimit();
|
||
if (limit == null || limit < 1) {
|
||
return LbThirdIntegrationConstants.DEFAULT_ORDER_PAGE_LIMIT;
|
||
}
|
||
return limit;
|
||
}
|
||
|
||
public static String resolveBuyerOrderListBaseUrl(LbThirdIntegrationConfig config) {
|
||
return resolveBuyerOrderListEndpoint(config).baseUrl();
|
||
}
|
||
|
||
public static int resolveBuyerOrderListLimit(LbThirdIntegrationConfig config) {
|
||
return resolveBuyerOrderListEndpoint(config).pageLimit();
|
||
}
|
||
|
||
public static int resolveBuyerOrderListCate(LbThirdIntegrationConfig config) {
|
||
return resolveBuyerOrderListEndpoint(config).cate();
|
||
}
|
||
|
||
public static int resolveBuyerOrderListType(LbThirdIntegrationConfig config) {
|
||
return resolveBuyerOrderListEndpoint(config).type();
|
||
}
|
||
|
||
/**
|
||
* 解析买方订单列表 API:支持 {@code buyer_order_list_path} 内嵌 {@code ?cate=&type=} 查询参数。
|
||
*/
|
||
public static BuyerOrderListEndpoint resolveBuyerOrderListEndpoint(LbThirdIntegrationConfig config) {
|
||
String adminBase = trimTrailingSlash(requireText(config.getAdminBaseUrl(), "adminBaseUrl"));
|
||
String rawPath = config.getBuyerOrderListPath();
|
||
if (!StringUtils.hasText(rawPath)) {
|
||
rawPath = LbThirdIntegrationConstants.DEFAULT_BUYER_ORDER_LIST_PATH;
|
||
}
|
||
rawPath = rawPath.trim();
|
||
|
||
String pathOnly = rawPath;
|
||
int cate = LbThirdIntegrationConstants.DEFAULT_BUYER_ORDER_LIST_CATE;
|
||
int type = LbThirdIntegrationConstants.DEFAULT_BUYER_ORDER_LIST_TYPE;
|
||
|
||
int queryIdx = rawPath.indexOf('?');
|
||
if (queryIdx >= 0) {
|
||
pathOnly = rawPath.substring(0, queryIdx);
|
||
Map<String, String> queryParams = parseQueryString(rawPath.substring(queryIdx + 1));
|
||
cate = parsePositiveInt(queryParams.get("cate"), cate);
|
||
type = parsePositiveInt(queryParams.get("type"), type);
|
||
}
|
||
|
||
if (!pathOnly.startsWith("/")) {
|
||
pathOnly = "/" + pathOnly;
|
||
}
|
||
|
||
return new BuyerOrderListEndpoint(
|
||
adminBase + pathOnly,
|
||
cate,
|
||
type,
|
||
resolveBuyerOrderListLimitRaw(config));
|
||
}
|
||
|
||
private static int resolveBuyerOrderListLimitRaw(LbThirdIntegrationConfig config) {
|
||
Integer limit = config.getBuyerOrderListLimit();
|
||
if (limit == null || limit < 1) {
|
||
return LbThirdIntegrationConstants.DEFAULT_BUYER_ORDER_LIST_LIMIT;
|
||
}
|
||
return limit;
|
||
}
|
||
|
||
private static Map<String, String> parseQueryString(String query) {
|
||
Map<String, String> params = new HashMap<>();
|
||
if (query == null || query.isBlank()) {
|
||
return params;
|
||
}
|
||
for (String pair : query.split("&")) {
|
||
if (pair.isBlank()) {
|
||
continue;
|
||
}
|
||
int eq = pair.indexOf('=');
|
||
if (eq <= 0) {
|
||
continue;
|
||
}
|
||
params.put(pair.substring(0, eq).trim(), pair.substring(eq + 1).trim());
|
||
}
|
||
return params;
|
||
}
|
||
|
||
private static int parsePositiveInt(String text, int defaultValue) {
|
||
if (text == null || text.isBlank()) {
|
||
return defaultValue;
|
||
}
|
||
try {
|
||
int value = Integer.parseInt(text.trim());
|
||
return value > 0 ? value : defaultValue;
|
||
} catch (NumberFormatException e) {
|
||
return defaultValue;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 组装 Cookie 请求头:优先完整 cookie,否则 {@code PHPSID=} + phpsid。
|
||
*/
|
||
public static String resolveCookieHeader(LbThirdIntegrationConfig config) {
|
||
String c = firstNonBlank(config.getCookie());
|
||
if (c != null) {
|
||
return c;
|
||
}
|
||
String id = firstNonBlank(config.getPhpsid());
|
||
if (id != null) {
|
||
return "PHPSID=" + id;
|
||
}
|
||
return "";
|
||
}
|
||
|
||
private static String firstNonBlank(String s) {
|
||
if (s == null) {
|
||
return null;
|
||
}
|
||
String t = s.trim();
|
||
return t.isEmpty() ? null : t;
|
||
}
|
||
|
||
private static String requireText(String value, String fieldName) {
|
||
if (!StringUtils.hasText(value)) {
|
||
throw new IllegalStateException(fieldName + " 未配置");
|
||
}
|
||
return value.trim();
|
||
}
|
||
|
||
private static String trimTrailingSlash(String url) {
|
||
String trimmed = url.trim();
|
||
while (trimmed.endsWith("/")) {
|
||
trimmed = trimmed.substring(0, trimmed.length() - 1);
|
||
}
|
||
return trimmed;
|
||
}
|
||
}
|