配置迁移到数据库表里

This commit is contained in:
2026-05-30 23:32:23 +08:00
parent bf685417e7
commit 8aa1a619b9
13 changed files with 423 additions and 145 deletions

View File

@@ -3,6 +3,7 @@ package com.rj.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rj.dto.hxr.HxrAdminOrderApiContext;
import com.rj.dto.hxr.HxrOrderRow;
import com.rj.dto.hxr.HxrOrderSelectResponse;
import com.rj.entity.LbDepartmentUser;
@@ -35,9 +36,6 @@ import java.util.stream.Collectors;
@Service
public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrderRow> implements ILbOrderRowService {
/** 与 hxr.admin.order-select-url 中 {@code limit} 一致,用于判断是否还有下一页 */
private static final int HXR_ORDER_PAGE_SIZE = 90;
/** 从第三方同步的订单明细默认 data_type */
private static final String SYNC_DATA_TYPE_DETAIL = "detail_data";
@@ -59,6 +57,9 @@ public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrder
@Autowired
private HxrAdminOrderSelectService hxrAdminOrderSelectService;
@Autowired
private ILbThirdIntegrationConfigService lbThirdIntegrationConfigService;
@Autowired
private ILbDepartmentUserService lbDepartmentUserService;
@@ -290,6 +291,18 @@ public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrder
String tid = tenantId.trim();
Optional<HxrAdminOrderApiContext> apiContextOpt =
lbThirdIntegrationConfigService.resolveAdminOrderApiContext(tid);
if (apiContextOpt.isEmpty()) {
result.put("success", false);
result.put("message",
"未找到该租户的第三方集成配置或配置未启用、URL/凭证不完整(请检查 lb_third_integration_config");
result.put("synced", 0);
return result;
}
HxrAdminOrderApiContext apiContext = apiContextOpt.get();
int orderPageLimit = apiContext.pageLimit() > 0 ? apiContext.pageLimit() : 90;
String previousTenantId = TenantContextHolder.getTenantId();
TenantContextHolder.setTenantId(tid);
try {
@@ -298,11 +311,12 @@ public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrder
int totalSynced = 0;
while (true) {
Optional<HxrOrderSelectResponse> opt =
fetchHxrOrderPage(buyTimeStart, buyTimeEnd, isResell, hxrOrderStatus, page);
fetchHxrOrderPage(buyTimeStart, buyTimeEnd, isResell, hxrOrderStatus, page, apiContext);
if (opt.isEmpty()) {
if (page == 1) {
result.put("success", false);
result.put("message", "未拉取到订单(请检查 hxr.admin cookie/phpsid、网络或后台返回");
result.put("message",
"未拉取到订单(请检查 lb_third_integration_config 中 cookie/phpsid、URL 或网络)");
result.put("synced", 0);
return result;
}
@@ -346,7 +360,7 @@ public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrder
}
totalSynced += upserted;
if (rows.size() < HXR_ORDER_PAGE_SIZE ) {
if (rows.size() < orderPageLimit) {
break;
}
page++;
@@ -389,17 +403,21 @@ public class LbOrderRowServiceImpl extends ServiceImpl<LbOrderRowMapper, LbOrder
String buyTimeEnd,
Integer isResell,
Integer hxrOrderStatus,
int page)
int page,
HxrAdminOrderApiContext apiContext)
throws Exception {
if (hxrOrderStatus != null) {
if (hxrOrderStatus == 0) {
return hxrAdminOrderSelectService.fetchUnpaidOrderSelect(buyTimeStart, buyTimeEnd, page);
return hxrAdminOrderSelectService.fetchUnpaidOrderSelect(
buyTimeStart, buyTimeEnd, page, apiContext);
}
if (hxrOrderStatus == 1) {
return hxrAdminOrderSelectService.fetchPaidOrderSelect(buyTimeStart, buyTimeEnd, page);
return hxrAdminOrderSelectService.fetchPaidOrderSelect(
buyTimeStart, buyTimeEnd, page, apiContext);
}
}
return hxrAdminOrderSelectService.fetchOrderSelect(buyTimeStart, buyTimeEnd, isResell, page);
return hxrAdminOrderSelectService.fetchOrderSelect(
buyTimeStart, buyTimeEnd, isResell, page, apiContext);
}
@Override

View File

@@ -5,8 +5,9 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rj.common.LbThirdIntegrationConstants;
import com.rj.common.PasswordUtil;
import com.rj.dto.LbThirdIntegrationCredentialUpdateRequest;
import com.rj.dto.hxr.HxrAdminOrderApiContext;
import com.rj.dto.hxr.HxrAdminUserApiContext;
import com.rj.dto.hxr.HxrGoodsApiContext;
import com.rj.entity.LbThirdIntegrationConfig;
import com.rj.mapper.LbThirdIntegrationConfigMapper;
@@ -15,7 +16,6 @@ import com.rj.util.LbThirdIntegrationConfigUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
@@ -57,10 +57,7 @@ public class LbThirdIntegrationConfigServiceImpl
entity.setCreateTime(now);
entity.setUpdateTime(now);
boolean credentialUpdated = applyCredentialEncryption(entity, null);
if (credentialUpdated && entity.getCredentialVersion() == null) {
entity.setCredentialVersion(1);
} else if (entity.getCredentialVersion() == null) {
if (entity.getCredentialVersion() == null) {
entity.setCredentialVersion(1);
}
@@ -108,7 +105,7 @@ public class LbThirdIntegrationConfigServiceImpl
}
}
boolean credentialUpdated = applyCredentialEncryption(entity, existing);
boolean credentialUpdated = applyCredentials(entity, existing);
if (credentialUpdated) {
int version = existing.getCredentialVersion() == null ? 1 : existing.getCredentialVersion();
entity.setCredentialVersion(version + 1);
@@ -324,8 +321,8 @@ public class LbThirdIntegrationConfigServiceImpl
String token = StringUtils.hasText(tokenOverride)
? tokenOverride.trim()
: decryptFromBytes(config.getGoodsApiTokenCipher());
String appStr = decryptFromBytes(config.getGoodsApiAppStrCipher());
: config.getGoodsApiToken();
String appStr = config.getGoodsApiAppStr();
if (!StringUtils.hasText(token) || !StringUtils.hasText(appStr)) {
return Optional.empty();
}
@@ -343,6 +340,64 @@ public class LbThirdIntegrationConfigServiceImpl
}
}
@Override
public Optional<HxrAdminUserApiContext> resolveAdminUserApiContext(String tenantId) {
if (!StringUtils.hasText(tenantId)) {
return Optional.empty();
}
LbThirdIntegrationConfig config = getByTenantIdOrNull(tenantId.trim());
if (config == null) {
return Optional.empty();
}
if (config.getEnabled() == null || config.getEnabled() != 1) {
return Optional.empty();
}
String cookieHeader = LbThirdIntegrationConfigUtil.resolveCookieHeader(config);
if (!StringUtils.hasText(cookieHeader)) {
return Optional.empty();
}
try {
return Optional.of(new HxrAdminUserApiContext(
LbThirdIntegrationConfigUtil.resolveUserSelectBaseUrl(config),
LbThirdIntegrationConfigUtil.resolveUserPageLimit(config),
cookieHeader.trim(),
LbThirdIntegrationConfigUtil.resolveUserReferer(config)));
} catch (IllegalStateException e) {
return Optional.empty();
}
}
@Override
public Optional<HxrAdminOrderApiContext> resolveAdminOrderApiContext(String tenantId) {
if (!StringUtils.hasText(tenantId)) {
return Optional.empty();
}
LbThirdIntegrationConfig config = getByTenantIdOrNull(tenantId.trim());
if (config == null) {
return Optional.empty();
}
if (config.getEnabled() == null || config.getEnabled() != 1) {
return Optional.empty();
}
String cookieHeader = LbThirdIntegrationConfigUtil.resolveCookieHeader(config);
if (!StringUtils.hasText(cookieHeader)) {
return Optional.empty();
}
try {
return Optional.of(new HxrAdminOrderApiContext(
LbThirdIntegrationConfigUtil.resolveOrderSelectBaseUrl(config),
LbThirdIntegrationConfigUtil.resolveOrderPageLimit(config),
cookieHeader.trim(),
LbThirdIntegrationConfigUtil.resolveOrderReferer(config)));
} catch (IllegalStateException e) {
return Optional.empty();
}
}
private LbThirdIntegrationConfig getByTenantIdOrNull(String tenantId) {
return this.getOne(new LambdaQueryWrapper<LbThirdIntegrationConfig>()
.eq(LbThirdIntegrationConfig::getTenantId, tenantId)
@@ -359,7 +414,7 @@ public class LbThirdIntegrationConfigServiceImpl
|| Boolean.TRUE.equals(request.getClearGoodsApiToken())
|| Boolean.TRUE.equals(request.getClearGoodsApiAppStr());
if (!hasUpdate && !hasClear) {
return "请至少提供一项凭证明文,或指定一项 clear* 清除操作";
return "请至少提供一项凭证,或指定一项 clear* 清除操作";
}
if (Boolean.TRUE.equals(request.getClearCookie()) && StringUtils.hasText(request.getCookie())) {
return "clearCookie 与 cookie 不能同时传";
@@ -380,27 +435,27 @@ public class LbThirdIntegrationConfigServiceImpl
LambdaUpdateWrapper<LbThirdIntegrationConfig> uw,
LbThirdIntegrationCredentialUpdateRequest request) {
if (Boolean.TRUE.equals(request.getClearCookie())) {
uw.set(LbThirdIntegrationConfig::getCookieCipher, null);
uw.set(LbThirdIntegrationConfig::getCookie, null);
} else if (StringUtils.hasText(request.getCookie())) {
uw.set(LbThirdIntegrationConfig::getCookieCipher, encryptToBytes(request.getCookie()));
uw.set(LbThirdIntegrationConfig::getCookie, request.getCookie().trim());
}
if (Boolean.TRUE.equals(request.getClearPhpsid())) {
uw.set(LbThirdIntegrationConfig::getPhpsidCipher, null);
uw.set(LbThirdIntegrationConfig::getPhpsid, null);
} else if (StringUtils.hasText(request.getPhpsid())) {
uw.set(LbThirdIntegrationConfig::getPhpsidCipher, encryptToBytes(request.getPhpsid()));
uw.set(LbThirdIntegrationConfig::getPhpsid, request.getPhpsid().trim());
}
if (Boolean.TRUE.equals(request.getClearGoodsApiToken())) {
uw.set(LbThirdIntegrationConfig::getGoodsApiTokenCipher, null);
uw.set(LbThirdIntegrationConfig::getGoodsApiToken, null);
} else if (StringUtils.hasText(request.getGoodsApiToken())) {
uw.set(LbThirdIntegrationConfig::getGoodsApiTokenCipher, encryptToBytes(request.getGoodsApiToken()));
uw.set(LbThirdIntegrationConfig::getGoodsApiToken, request.getGoodsApiToken().trim());
}
if (Boolean.TRUE.equals(request.getClearGoodsApiAppStr())) {
uw.set(LbThirdIntegrationConfig::getGoodsApiAppStrCipher, null);
uw.set(LbThirdIntegrationConfig::getGoodsApiAppStr, null);
} else if (StringUtils.hasText(request.getGoodsApiAppStr())) {
uw.set(LbThirdIntegrationConfig::getGoodsApiAppStrCipher, encryptToBytes(request.getGoodsApiAppStr()));
uw.set(LbThirdIntegrationConfig::getGoodsApiAppStr, request.getGoodsApiAppStr().trim());
}
}
@@ -410,20 +465,20 @@ public class LbThirdIntegrationConfigServiceImpl
data.put("tenantId", config.getTenantId());
data.put("providerCode", config.getProviderCode());
data.put("authType", config.getAuthType());
data.put("cookieConfigured", hasCipher(config.getCookieCipher()));
data.put("phpsidConfigured", hasCipher(config.getPhpsidCipher()));
data.put("goodsApiTokenConfigured", hasCipher(config.getGoodsApiTokenCipher()));
data.put("goodsApiAppStrConfigured", hasCipher(config.getGoodsApiAppStrCipher()));
data.put("cookieConfigured", isConfigured(config.getCookie()));
data.put("phpsidConfigured", isConfigured(config.getPhpsid()));
data.put("goodsApiTokenConfigured", isConfigured(config.getGoodsApiToken()));
data.put("goodsApiAppStrConfigured", isConfigured(config.getGoodsApiAppStr()));
data.put("credentialVersion", config.getCredentialVersion());
data.put("credentialExpireTime", config.getCredentialExpireTime());
data.put("lastVerifiedTime", config.getLastVerifiedTime());
data.put("lastVerifiedOk", config.getLastVerifiedOk());
data.put("updateTime", config.getUpdateTime());
if (includePlaintext) {
data.put("cookie", decryptFromBytes(config.getCookieCipher()));
data.put("phpsid", decryptFromBytes(config.getPhpsidCipher()));
data.put("goodsApiToken", decryptFromBytes(config.getGoodsApiTokenCipher()));
data.put("goodsApiAppStr", decryptFromBytes(config.getGoodsApiAppStrCipher()));
data.put("cookie", config.getCookie());
data.put("phpsid", config.getPhpsid());
data.put("goodsApiToken", config.getGoodsApiToken());
data.put("goodsApiAppStr", config.getGoodsApiAppStr());
}
return data;
}
@@ -496,85 +551,61 @@ public class LbThirdIntegrationConfigServiceImpl
}
/**
* 将明文凭证加密为 VARBINARY未传明文则保留已有密文更新场景
* 更新场景下保留未传凭证字段的已有值
*
* @return 是否有任一凭证字段被更新
*/
private boolean applyCredentialEncryption(LbThirdIntegrationConfig entity, LbThirdIntegrationConfig existing) {
private boolean applyCredentials(LbThirdIntegrationConfig entity, LbThirdIntegrationConfig existing) {
boolean updated = false;
if (StringUtils.hasText(entity.getCookiePlain())) {
entity.setCookieCipher(encryptToBytes(entity.getCookiePlain()));
if (StringUtils.hasText(entity.getCookie())) {
entity.setCookie(entity.getCookie().trim());
updated = true;
} else if (existing != null) {
entity.setCookieCipher(existing.getCookieCipher());
entity.setCookie(existing.getCookie());
}
if (StringUtils.hasText(entity.getPhpsidPlain())) {
entity.setPhpsidCipher(encryptToBytes(entity.getPhpsidPlain()));
if (StringUtils.hasText(entity.getPhpsid())) {
entity.setPhpsid(entity.getPhpsid().trim());
updated = true;
} else if (existing != null) {
entity.setPhpsidCipher(existing.getPhpsidCipher());
entity.setPhpsid(existing.getPhpsid());
}
if (StringUtils.hasText(entity.getGoodsApiTokenPlain())) {
entity.setGoodsApiTokenCipher(encryptToBytes(entity.getGoodsApiTokenPlain()));
if (StringUtils.hasText(entity.getGoodsApiToken())) {
entity.setGoodsApiToken(entity.getGoodsApiToken().trim());
updated = true;
} else if (existing != null) {
entity.setGoodsApiTokenCipher(existing.getGoodsApiTokenCipher());
entity.setGoodsApiToken(existing.getGoodsApiToken());
}
if (StringUtils.hasText(entity.getGoodsApiAppStrPlain())) {
entity.setGoodsApiAppStrCipher(encryptToBytes(entity.getGoodsApiAppStrPlain()));
if (StringUtils.hasText(entity.getGoodsApiAppStr())) {
entity.setGoodsApiAppStr(entity.getGoodsApiAppStr().trim());
updated = true;
} else if (existing != null) {
entity.setGoodsApiAppStrCipher(existing.getGoodsApiAppStrCipher());
entity.setGoodsApiAppStr(existing.getGoodsApiAppStr());
}
entity.setCookiePlain(null);
entity.setPhpsidPlain(null);
entity.setGoodsApiTokenPlain(null);
entity.setGoodsApiAppStrPlain(null);
return updated;
}
private static byte[] encryptToBytes(String plainText) {
String encrypted = PasswordUtil.encryptReversibleWithDefaultSalt(plainText);
return encrypted.getBytes(StandardCharsets.UTF_8);
}
/**
* 供业务层读取凭证明文(内部使用)
*/
public static String decryptFromBytes(byte[] cipherBytes) {
if (cipherBytes == null || cipherBytes.length == 0) {
return null;
}
String stored = new String(cipherBytes, StandardCharsets.UTF_8);
return PasswordUtil.decryptReversibleWithDefaultSalt(stored);
}
private LbThirdIntegrationConfig maskForResponse(LbThirdIntegrationConfig entity) {
if (entity == null) {
return null;
}
entity.setCookieConfigured(hasCipher(entity.getCookieCipher()));
entity.setPhpsidConfigured(hasCipher(entity.getPhpsidCipher()));
entity.setGoodsApiTokenConfigured(hasCipher(entity.getGoodsApiTokenCipher()));
entity.setGoodsApiAppStrConfigured(hasCipher(entity.getGoodsApiAppStrCipher()));
entity.setCookieConfigured(isConfigured(entity.getCookie()));
entity.setPhpsidConfigured(isConfigured(entity.getPhpsid()));
entity.setGoodsApiTokenConfigured(isConfigured(entity.getGoodsApiToken()));
entity.setGoodsApiAppStrConfigured(isConfigured(entity.getGoodsApiAppStr()));
entity.setCookieCipher(null);
entity.setPhpsidCipher(null);
entity.setGoodsApiTokenCipher(null);
entity.setGoodsApiAppStrCipher(null);
entity.setCookiePlain(null);
entity.setPhpsidPlain(null);
entity.setGoodsApiTokenPlain(null);
entity.setGoodsApiAppStrPlain(null);
entity.setCookie(null);
entity.setPhpsid(null);
entity.setGoodsApiToken(null);
entity.setGoodsApiAppStr(null);
return entity;
}
private static boolean hasCipher(byte[] cipher) {
return cipher != null && cipher.length > 0;
private static boolean isConfigured(String value) {
return StringUtils.hasText(value);
}
}

View File

@@ -3,10 +3,12 @@ package com.rj.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rj.dto.hxr.HxrAdminUserApiContext;
import com.rj.dto.hxr.HxrLbUserSelectResponse;
import com.rj.entity.LbUser;
import com.rj.mapper.LbUserMapper;
import com.rj.service.HxrAdminUserService;
import com.rj.service.ILbThirdIntegrationConfigService;
import com.rj.service.ILbUserService;
import com.rj.tenant.TenantContextHolder;
import lombok.extern.slf4j.Slf4j;
@@ -34,6 +36,9 @@ public class LbUserServiceImpl extends ServiceImpl<LbUserMapper, LbUser> impleme
@Autowired
private HxrAdminUserService hxrAdminUserService;
@Autowired
private ILbThirdIntegrationConfigService lbThirdIntegrationConfigService;
@Override
public Map<String, Object> add(LbUser entity) {
Map<String, Object> result = new HashMap<>();
@@ -313,6 +318,20 @@ public class LbUserServiceImpl extends ServiceImpl<LbUserMapper, LbUser> impleme
}
String tid = tenantId.trim();
Optional<HxrAdminUserApiContext> apiContextOpt =
lbThirdIntegrationConfigService.resolveAdminUserApiContext(tid);
if (apiContextOpt.isEmpty()) {
result.put("success", false);
result.put("message",
"未找到该租户的第三方集成配置或配置未启用、URL/凭证不完整(请检查 lb_third_integration_config");
result.put("synced", 0);
return result;
}
HxrAdminUserApiContext apiContext = apiContextOpt.get();
int userPageLimit = apiContext.pageLimit() > 0
? apiContext.pageLimit()
: HxrAdminUserService.USER_SELECT_PAGE_SIZE;
String previousTenantId = TenantContextHolder.getTenantId();
TenantContextHolder.setTenantId(tid);
try {
@@ -320,11 +339,13 @@ public class LbUserServiceImpl extends ServiceImpl<LbUserMapper, LbUser> impleme
int page = 1;
int totalSynced = 0;
while (true) {
Optional<HxrLbUserSelectResponse> opt = hxrAdminUserService.fetchUserSelectPage(page);
Optional<HxrLbUserSelectResponse> opt =
hxrAdminUserService.fetchUserSelectPage(page, apiContext);
if (opt.isEmpty()) {
if (page == 1) {
result.put("success", false);
result.put("message", "未拉取到用户(请检查 hxr.admin cookie/phpsid、网络或后台返回");
result.put("message",
"未拉取到用户(请检查 lb_third_integration_config 中 cookie/phpsid、URL 或网络)");
result.put("synced", 0);
return result;
}
@@ -370,7 +391,7 @@ public class LbUserServiceImpl extends ServiceImpl<LbUserMapper, LbUser> impleme
}
totalSynced += upserted;
if (rows.size() < HxrAdminUserService.USER_SELECT_PAGE_SIZE) {
if (rows.size() < userPageLimit) {
break;
}
page++;