This commit is contained in:
lu7213 committed 2026-09-27 19:18:44 -04:00
1 parent 0df6716cc3
commit 009955d560
25 files changed
+417 -63

No files matched your search

@@ -0,0 +1,61 @@
package com.ruoyi.system.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.AppApiKeysPlus;
import com.ruoyi.system.domain.UsdtOrdersPlus;
import com.ruoyi.system.service.IAppApiKeysService;
import com.ruoyi.system.service.IUsdtOrdersService;
import com.ruoyi.system.service.PaymentSignatureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.concurrent.TimeUnit;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.net.URI;
/** 对外支付 API,统一使用 JSON 和签名校验。 */
@RestController
@RequestMapping("/api/v1/order")
public class PaymentApiController {
@Autowired private IAppApiKeysService keys;
@Autowired private IUsdtOrdersService orders;
@Autowired private StringRedisTemplate redis;
@PostMapping("/create-transaction")
public AjaxResult create(@RequestBody Map<String, Object> body) {
String signature = String.valueOf(body.get("signature"));
Long appId = parseLong(body.get("app_id"));
AppApiKeysPlus key = activeKey(appId);
if (key == null || !PaymentSignatureService.sign(body, key.getApiSecret()).equalsIgnoreCase(signature)) return AjaxResult.error("签名无效");
String orderId = String.valueOf(body.get("order_id"));
if (orderId.length() == 0 || orderId.length() > 32 || body.get("amount") == null || body.get("wallet_user_name") == null) return AjaxResult.error("缺少必要参数");
if (!validUrl(body.get("notify_url")) || (body.get("redirect_url") != null && !validUrl(body.get("redirect_url")))) return AjaxResult.error("回调地址无效");
UsdtOrdersPlus order = new UsdtOrdersPlus();
order.setTradeId(UUID.randomUUID().toString().replace("-", "")); order.setOrderId(orderId);
order.setToken(String.valueOf(body.get("payment_address")));
order.setAmount(new BigDecimal(String.valueOf(body.get("amount")))); order.setActualAmount(order.getAmount());
String lockKey = "payment:slot:" + appId + ":" + order.getToken() + ":" + order.getActualAmount().toPlainString();
if (!Boolean.TRUE.equals(redis.opsForValue().setIfAbsent(lockKey, order.getTradeId(), 30, TimeUnit.MINUTES))) return AjaxResult.error("该金额已有待支付订单");
order.setStatus(1L); order.setCallbackConfirm(2L); order.setAppId(appId); order.setUserId(key.getUserId());
order.setNotifyUrl(String.valueOf(body.get("notify_url"))); order.setRedirectUrl(body.get("redirect_url") == null ? null : String.valueOf(body.get("redirect_url")));
orders.save(order);
Map<String, Object> result = new HashMap<String, Object>(); result.put("order_id", orderId); result.put("trade_id", order.getTradeId()); result.put("payment_amount", order.getActualAmount()); result.put("payment_address", order.getToken());
return AjaxResult.success(result);
}
@PostMapping("/query")
public AjaxResult query(@RequestBody Map<String, Object> body) {
Long appId = parseLong(body.get("app_id")); AppApiKeysPlus key = activeKey(appId); String signature = String.valueOf(body.get("signature"));
if (key == null || !PaymentSignatureService.sign(body, key.getApiSecret()).equalsIgnoreCase(signature)) return AjaxResult.error("签名无效");
UsdtOrdersPlus order = orders.getOne(new LambdaQueryWrapper<UsdtOrdersPlus>().eq(UsdtOrdersPlus::getAppId, appId).eq(UsdtOrdersPlus::getOrderId, String.valueOf(body.get("order_id"))));
if (order == null) return AjaxResult.error("订单不存在");
Map<String, Object> result = new HashMap<String, Object>(); result.put("order_id", order.getOrderId()); result.put("trade_id", order.getTradeId()); result.put("status", order.getStatus()); result.put("transaction_id", order.getBlockTransactionId()); return AjaxResult.success(result);
}
private AppApiKeysPlus activeKey(Long appId) { return appId == null ? null : keys.getOne(new LambdaQueryWrapper<AppApiKeysPlus>().eq(AppApiKeysPlus::getAppId, appId).eq(AppApiKeysPlus::getStatus, 1)); }
private Long parseLong(Object value) { try { return value == null ? null : Long.valueOf(String.valueOf(value)); } catch (Exception ex) { return null; } }
private boolean validUrl(Object value) { try { if (value == null) return false; URI uri = new URI(String.valueOf(value)); return ("https".equalsIgnoreCase(uri.getScheme()) || "http".equalsIgnoreCase(uri.getScheme())) && uri.getHost() != null; } catch (Exception ex) { return false; } }
}
@@ -7,8 +7,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.extern.slf4j.Slf4j;
@RestController
@Slf4j
@RequestMapping("/system/test")
public class TestPluginController {
@Autowired
@@ -17,7 +19,8 @@ public class TestPluginController {
@GetMapping(value = "/testPlugin")
public AjaxResult testPlugin() {
IPluginTest pluginTest = pluginBeanFactory.getBean("pluginDemo", "1.0.0", IPluginTest.class);
System.out.println(pluginTest.funcExec("dd"));
return AjaxResult.success(pluginTest.funcExec("dd"));
String result = pluginTest.funcExec("dd");
log.debug("plugin test completed, resultLength={}", result == null ? 0 : result.length());
return AjaxResult.success(result);
}
}
@@ -28,6 +28,18 @@ import java.util.List;
public class UsdtAggregationTaskController extends BaseController {
@Autowired
private IUsdtAggregationTaskService usdtAggregationTaskService;
/** 触发归集任务;实际明细执行由后台任务处理,避免阻塞 HTTP 请求。 */
@PreAuthorize("@ss.hasPermi('system:usdtTask:execute')")
@PostMapping("/{id}/execute")
public AjaxResult execute(@PathVariable("id") Long id) {
UsdtAggregationTask task = usdtAggregationTaskService.selectUsdtAggregationTaskById(id, false);
if (task == null) return AjaxResult.error("任务不存在");
if (task.getStatus() != null && task.getStatus() == 1) return AjaxResult.error("任务正在执行");
task.setStatus(1);
task.setExeTime(new java.util.Date());
usdtAggregationTaskService.updateUsdtAggregationTask(task, false);
return AjaxResult.success("任务已进入执行队列");
}
@PreAuthorize("@ss.hasPermi('system:usdtTask:getByIds')")
@PostMapping("/getByIds")
@@ -8,6 +8,7 @@ import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.select.Ids;
import com.ruoyi.common.select.SelectItem;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.WalletAddress;
import com.ruoyi.system.domain.WalletAddressPlus;
import com.ruoyi.system.service.IWalletAddressService;
@@ -16,6 +17,13 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.math.BigDecimal;
import org.tron.trident.core.ApiWrapper;
import org.tron.common.crypto.ECKey;
import org.tron.trident.utils.Base58Check;
import java.math.BigInteger;
import com.ruoyi.system.service.ITronService;
import com.ruoyi.system.domain.vo.WalletOperationRequest;
/**
* token管理Controller
@@ -28,6 +36,77 @@ import java.util.List;
public class WalletAddressController extends BaseController {
@Autowired
private IWalletAddressService walletAddressService;
@Autowired
private ITronService tronService;
/** 查询链上账户快照。私钥只从服务端读取,响应永远不包含 tokenKey。 */
@PreAuthorize("@ss.hasPermi('system:address:query')")
@GetMapping("/{id}/chain")
public AjaxResult chain(@PathVariable("id") Long id) {
WalletAddressPlus wallet = walletAddressService.getById(id);
if (wallet == null) {
return AjaxResult.error("钱包不存在");
}
if (wallet.getUserId() != null && !SecurityUtils.isAdmin(getUserId()) && !wallet.getUserId().equals(getUserId())) {
return AjaxResult.error("无权访问该钱包");
}
if (wallet.getTokenKey() == null || wallet.getTokenKey().trim().isEmpty()) {
return AjaxResult.error("钱包未配置受控私钥");
}
ApiWrapper api = null;
try {
api = tronService.buildApiWrapper(wallet.getTokenKey());
wallet.setTrxBalance(tronService.getTRxBalance(api, wallet.getToken()));
wallet.setUsdtBalance(tronService.getUsdtBalance(api, wallet.getToken()));
com.ruoyi.system.domain.vo.EnergyAndBandwidthVo resource = tronService.energyAndBandwidth(wallet.getToken());
wallet.setEnergyNum(resource.getEnergy().max(BigDecimal.ZERO).longValue());
wallet.setBandwidthNum(resource.getBandwidth().max(BigDecimal.ZERO).longValue());
wallet.setTokenKey(null);
return AjaxResult.success(wallet);
} catch (Exception ex) {
return AjaxResult.error("链上查询失败,请稍后重试");
} finally {
if (api != null) api.close();
}
}
@PreAuthorize("@ss.hasPermi('system:address:operate')")
@PostMapping("/operate/{type}")
public AjaxResult operate(@PathVariable("type") String type, @RequestBody WalletOperationRequest request) {
if (request == null || request.getWalletId() == null) return AjaxResult.error("钱包不能为空");
WalletAddressPlus wallet = walletAddressService.getById(request.getWalletId());
if (wallet == null || (wallet.getUserId() != null && !SecurityUtils.isAdmin(getUserId()) && !wallet.getUserId().equals(getUserId()))) return AjaxResult.error("无权访问该钱包");
if (wallet.getTokenKey() == null || wallet.getTokenKey().trim().isEmpty()) return AjaxResult.error("钱包未配置受控私钥");
try {
String derived = Base58Check.bytesToBase58(ECKey.fromPrivate(new BigInteger(wallet.getTokenKey().trim(), 16)).getAddress());
if (!derived.equals(wallet.getToken())) return AjaxResult.error("钱包私钥与地址不匹配");
} catch (Exception ex) { return AjaxResult.error("钱包私钥格式无效"); }
if (type == null || !type.matches("(?i)trx|usdt|stake|unstake|delegate|undelegate|cancel-unstake")) return AjaxResult.error("不支持的操作类型");
if (!"cancel-unstake".equalsIgnoreCase(type) && (request.getAmount() == null || request.getAmount().signum() <= 0)) return AjaxResult.error("金额必须大于 0");
if (("trx".equalsIgnoreCase(type) || "usdt".equalsIgnoreCase(type) || "delegate".equalsIgnoreCase(type) || "undelegate".equalsIgnoreCase(type)) && (request.getToAddress() == null || request.getToAddress().trim().isEmpty())) return AjaxResult.error("目标地址不能为空");
if (!Boolean.TRUE.equals(request.getBroadcast())) return AjaxResult.success("预演模式:已通过参数校验,未广播交易");
ApiWrapper api = null;
try {
api = tronService.buildApiWrapper(wallet.getTokenKey());
String tx;
BigDecimal amount = request.getAmount();
if ("trx".equalsIgnoreCase(type)) tx = tronService.trxTrans(api, wallet.getToken(), request.getToAddress(), amount);
else if ("usdt".equalsIgnoreCase(type)) tx = tronService.usdtTrans(api, wallet.getToken(), request.getToAddress(), amount.toPlainString());
else if ("stake".equalsIgnoreCase(type)) tx = String.valueOf(tronService.stakeTrxBalance(api, wallet.getToken(), amount, resource(request)));
else if ("unstake".equalsIgnoreCase(type)) tx = String.valueOf(tronService.unstakeTrxBalance(api, wallet.getToken(), amount, resource(request)));
else if ("delegate".equalsIgnoreCase(type)) tx = tronService.delegateResourceV2(api, wallet.getToken(), request.getToAddress(), amount, resource(request));
else if ("undelegate".equalsIgnoreCase(type)) tx = tronService.unDelegateResourceV2(api, wallet.getToken(), request.getToAddress(), amount, resource(request));
else if ("cancel-unstake".equalsIgnoreCase(type)) tx = String.valueOf(tronService.cancleUnstakeTrxBalance(api, wallet.getToken()));
else return AjaxResult.error("不支持的操作类型");
return AjaxResult.success(tx);
} catch (Exception ex) {
return AjaxResult.error("链上操作失败,请检查余额、资源和地址");
} finally { if (api != null) api.close(); }
}
private int resource(WalletOperationRequest request) {
return request.getResourceCode() == null ? 1 : request.getResourceCode();
}
@PreAuthorize("@ss.hasPermi('system:address:getByIds')" )
@PostMapping("/getByIds" )