新版仿ok交易所-后端
1
zj
2025-10-30 14c1946eae86a86f8d1edee6cf3bdaf7572fc966
trading-order-admin/src/main/java/com/yami/trading/api/controller/exchange/ApiChannelBlockchainController.java
@@ -9,6 +9,8 @@
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yami.trading.bean.vo.RechargeAddressVo;
import com.yami.trading.common.util.RedisUtil;
import com.yami.trading.security.common.enums.CryptoCurrencyEnum;
import com.yami.trading.security.common.util.LocalKeyStorageAESUtil;
import com.yami.trading.api.UD.*;
@@ -33,6 +35,7 @@
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
@@ -69,6 +72,8 @@
    private SysUserService sysUserService;
    @Autowired
    RechargeBlockchainOrderService rechargeBlockchainOrderService;
    @Autowired
    protected UserService partyService;
    private final String gateway =  "https://sig11.udun.io";
    private final String merchantKey =  "7fd79e4ed59e7c31e1fa2e13d64f7e6c";
@@ -127,195 +132,68 @@
    @GetMapping(action + "getBlockchainName.action")
    public Object getBlockchainName(HttpServletRequest request) throws Exception {
        String coin = request.getParameter("coin");
        List<ChannelBlockchain> data = new ArrayList<ChannelBlockchain>();
        Map<String, String> loadedMap = null;
        String filePath = "/etc/asdk/data.json";
        File file = new File(filePath);
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
            loadedMap = (Map<String, String>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        String partyId = SecurityUtils.getUser().getUserId();
        if(null != partyId && loadedMap.get("userId").indexOf(partyId)>=0 && null!=loadedMap && "1".equals(loadedMap.get("type"))){
            if(coin.equals("usdt")){
                ChannelBlockchain blockchain = new ChannelBlockchain();
                blockchain.setBlockchain_name("TRC20");
                blockchain.setAddress(loadedMap.get("usdtTrc"));
                blockchain.setCoin(coin);
                blockchain.setAuto(false);
                blockchain.setImg(null);
                data.add(blockchain);
        return Result.succeed(getStringListMap(coin.toUpperCase(),partyId));
    }
                ChannelBlockchain blockchain1 = new ChannelBlockchain();
                blockchain1.setBlockchain_name("ERC20");
                blockchain1.setAddress(loadedMap.get("usdtErc"));
                blockchain1.setCoin(coin);
                blockchain1.setAuto(false);
                blockchain1.setImg(null);
                data.add(blockchain1);
            }else if(coin.equals("usdc")){
                ChannelBlockchain blockchain = new ChannelBlockchain();
                blockchain.setBlockchain_name("TRC20");
                blockchain.setAddress(loadedMap.get("usdcTrc"));
                blockchain.setCoin(coin);
                blockchain.setAuto(false);
                blockchain.setImg(null);
                data.add(blockchain);
    @NotNull
    private List<ChannelBlockchain> getStringListMap(String coin,String partyId ) {
                ChannelBlockchain blockchain1 = new ChannelBlockchain();
                blockchain1.setBlockchain_name("ERC20");
                blockchain1.setAddress(loadedMap.get("usdcErc"));
                blockchain1.setCoin(coin);
                blockchain1.setAuto(false);
                blockchain1.setImg(null);
                data.add(blockchain1);
            }else  if(coin.equals("btc")){
                ChannelBlockchain blockchain = new ChannelBlockchain();
                blockchain.setBlockchain_name("BTC");
                blockchain.setAddress(loadedMap.get("btc"));
                blockchain.setCoin(coin);
                blockchain.setAuto(false);
                blockchain.setImg(null);
                data.add(blockchain);
            }else if(coin.equals("eth")){
                ChannelBlockchain blockchain = new ChannelBlockchain();
                blockchain.setBlockchain_name("ETH");
                blockchain.setAddress(loadedMap.get("eth"));
                blockchain.setCoin(coin);
                blockchain.setAuto(false);
                blockchain.setImg(null);
                data.add(blockchain);
        //获取商户支持币种
        List<Coin> coinList = listSupportCoin(merchantId,false);
        List<ChannelBlockchain> blockchainList = new ArrayList<>();
        List<Coin> filteredCoins = coinList.stream()
                .filter(c -> coin.equals(c.getSymbol()))
                .collect(Collectors.toList());
        //创建地址
        filteredCoins.forEach(f->{
            if(f.getName().equals("BTC")){
                String partyBTC = Optional.ofNullable(RedisUtil.get(partyId + "_BTC"))
                        .map(Object::toString)
                        .orElse("");
                if(!StringUtils.isNotEmpty(partyBTC)){
                    Address address3 = createAddress(f.getMainCoinType(),null,null,null);
                    blockchainList.add(createElement("2c948a8280828d5f018084ff569b0002", "btc", "/qr/1be19b34fbb9406c8044c1a9b08a87ca.png", address3.getAddress(),"BTC"));
                    RedisUtil.set(partyId+ "_BTC",address3.getAddress());
                }else{
                    blockchainList.add(createElement("2c948a8280828d5f018084ff569b0002", "btc", "/qr/1be19b34fbb9406c8044c1a9b08a87ca.png", partyBTC,"BTC"));
                }
            }else if(f.getName().equals("ETH")){
                String partyETH = Optional.ofNullable(RedisUtil.get(partyId + "_ETH"))
                        .map(Object::toString)
                        .orElse("");
                if(!StringUtils.isNotEmpty(partyETH)){
                    Address address3 = createAddress(f.getMainCoinType(),null,null,null);
                    blockchainList.add(createElement("2c948a8280828d5f018084ff569b0002", "eth", "/qr/1be19b34fbb9406c8044c1a9b08a87ca.png", address3.getAddress(),"ETH"));
                    RedisUtil.set(partyId + "_ETH",address3.getAddress());
                }else{
                    blockchainList.add(createElement("2c948a8280828d5f018084ff569b0002", "eth", "/qr/1be19b34fbb9406c8044c1a9b08a87ca.png", partyETH,"ETH"));
                }
            }else if(f.getName().equals("USDT-ERC20")){
                String partyUSDT = Optional.ofNullable(RedisUtil.get(partyId + "_USDT-ERC20"))
                        .map(Object::toString)
                        .orElse("");
                if(!StringUtils.isNotEmpty(partyUSDT)){
                    Address address3 = createAddress(f.getMainCoinType(),null,null,null);
                    blockchainList.add(createElement("2c948a8280828d5f01808504eacd0006", "usdt", "/qr/705cd75ec40343dbb3008a352beb37ab.png", address3.getAddress(),"ERC20"));
                    RedisUtil.set(partyId + "_USDT-ERC20",address3.getAddress());
                }else{
                    blockchainList.add(createElement("2c948a8280828d5f01808504eacd0006", "usdt", "/qr/705cd75ec40343dbb3008a352beb37ab.png", partyUSDT,"ERC20"));
                }
            }else if(f.getName().equals("USDC")){
                String partyUSDC = Optional.ofNullable(RedisUtil.get(partyId + "USDC"))
                        .map(Object::toString)
                        .orElse("");
                if(!StringUtils.isNotEmpty(partyUSDC)){
                    Address address3 = createAddress(f.getMainCoinType(),null,null,null);
                    blockchainList.add(createElement("2c948a8280828d5f01808504eacd0004", "usdc", "/qr/975f4666ed044efa9f0ee2fa81fa8f4a.png",  address3.getAddress(),"ERC20"));
                    RedisUtil.set(partyId + "USDC",address3.getAddress());
                }else{
                    blockchainList.add(createElement("2c948a8280828d5f01808504eacd0004", "usdc", "/qr/975f4666ed044efa9f0ee2fa81fa8f4a.png",  partyUSDC,"ERC20"));
                }
            }
        }else {
            try {
                coin  = coin.toLowerCase();
                Map<String, List<CryptoCurrencyEnum>> allGroupedByCoin = CryptoCurrencyEnum.getAllGroupedByCoin();
                List<CryptoCurrencyEnum> currencyEnums = allGroupedByCoin.get(coin);
                currencyEnums.forEach(f->{
                    try {
                        String address = LocalKeyStorageAESUtil.loadAndDecrypt(f.getName());
                        ChannelBlockchain blockchain = new ChannelBlockchain();
                        blockchain.setBlockchain_name(f.getChain());
                        blockchain.setAddress(address);
                        blockchain.setCoin(f.getCoin());
                        blockchain.setAuto(false);
                        blockchain.setImg(null);
                        data.add(blockchain);
                    } catch (Exception e) {
                        e.getMessage();
                    }
                });
            }catch (Exception e){
                log.error("获取充值地址错误:",e);
                return Result.failed("充值链暂不可用");
            }
        }
        return Result.succeed(data);
//        String partyId =SecurityUtils.getUser().getUserId();
//        User party = userService.getById(partyId);
//        if (0 == this.sysparaService.find("can_recharge").getInteger()) {
//            return Result.failed("请联系客服充值");
//        }
//        List<PartyBlockchain> list = partyBlockchainService.findByUserNameAndCoinSymbol(party.getUserName(), coin);
//        if (null != list && !list.isEmpty()) {
//            data = list.stream().map(dict -> {
//                String qrImage = dict.getQrImage();
//                String chainAddress = dict.getAddress();
//                String chainName = dict.getChainName();
//                String coinSymbol = dict.getCoinSymbol();
//                String autoStr = dict.getAuto();
//                boolean auto = autoStr.equals("Y") ? true : false;
//                ChannelBlockchain cbc = new ChannelBlockchain();
//                cbc.setBlockchain_name(chainName);
//                cbc.setAddress(chainAddress);
//                cbc.setCoin(coinSymbol);
//                cbc.setAuto(auto);
//                cbc.setImg(qrImage);
//                return cbc;
//            }).collect(Collectors.toList());
//        }
//        return Result.succeed(data);
//        //后台设置地址
//        if (data.isEmpty()) data = this.channelBlockchainService.findByCoin(coin.toLowerCase());
//        for (int i = 0; i < data.size(); i++) {
//            data.get(i).setBlockchain_name(data.get(i).getBlockchainName());
//            if (1 == this.sysparaService.find("can_recharge").getInteger()) {
//                if (!StringUtils.isNullOrEmpty(data.get(i).getImg())) {
//                    String path = Constants.WEB_URL + "/public/showimg!showImg.action?imagePath=" + data.get(i).getImg();
//                    data.get(i).setImgStr("/public/showimg!showImg.action?imagePath=" + data.get(i).getImg());
//                    data.get(i).setImg(path);
//                }
//            } else {
//                data.get(i).setImg(null);
//                data.get(i).setImgStr(null);
//                data.get(i).setAddress(null);
//            }
//        }
//        String partyId =SecurityUtils.getUser().getUserId();
//        if(!StringUtils.isNotEmpty(partyId)){
//            throw new YamiShopBindException("请登录!");
//        }
//        if (0 == this.sysparaService.find("can_recharge").getInteger()) {
//            return Result.failed("请联系客服充值");
//        }
//        RechargeAddressVo rechargeAddressVo = new RechargeAddressVo();
//
//        //获取u盾地址
//        //获取商户支持币种
//        List<Coin> coinList = listSupportCoin(merchantId,false);
//        //创建地址
//        coinList.forEach(f->{
//            Address address = new Address();
//            if(f.getName().equals("BTC")){
//                String ress = (String)redisTemplate.opsForValue().get(partyId + "BTC");
//                if(StringUtils.isNotEmpty(ress)){
//                    rechargeAddressVo.setAddress(ress);
//                    rechargeAddressVo.setCoin(coin);
//                    rechargeAddressVo.setBlockchainName(coin);
//                }else{
//                    address = createAddress(f.getMainCoinType(),null,null,null);
//                    rechargeAddressVo.setAddress(address.getAddress());
//                    rechargeAddressVo.setCoin(coin);
//                    rechargeAddressVo.setBlockchainName(coin);
//                    redisTemplate.opsForValue().set(partyId + "BTC",address.getAddress());
//                }
//            }else if(f.getName().equals("ETH")){
//                String ress = (String)redisTemplate.opsForValue().get(partyId + "ETH");
//                if(StringUtils.isNotEmpty(ress)){
//                    rechargeAddressVo.setAddress(ress);
//                    rechargeAddressVo.setCoin(coin);
//                    rechargeAddressVo.setBlockchainName(coin);
//                }else{
//                    address = createAddress(f.getMainCoinType(),null,null,null);
//                    rechargeAddressVo.setAddress(address.getAddress());
//                    rechargeAddressVo.setCoin(coin);
//                    rechargeAddressVo.setBlockchainName(coin);
//                    redisTemplate.opsForValue().set(partyId + "ETH",address.getAddress());
//                }
//            }else if(f.getName().equals("USDT")){
//                String ress = (String)redisTemplate.opsForValue().get(partyId + "USDT");
//                if(StringUtils.isNotEmpty(ress)){
//                    rechargeAddressVo.setAddress(ress);
//                    rechargeAddressVo.setCoin(coin);
//                    rechargeAddressVo.setBlockchainName(coin);
//                }else{
//                    address = createAddress(f.getMainCoinType(),null,null,null);
//                    rechargeAddressVo.setAddress(address.getAddress());
//                    rechargeAddressVo.setCoin(coin);
//                    rechargeAddressVo.setBlockchainName(coin);
//                    redisTemplate.opsForValue().set(partyId + "USDT",address.getAddress());
//                }
//            }
//        });
//        return Result.succeed(rechargeAddressVo);
        });
        return blockchainList;
    }
    public List<Coin> listSupportCoin(String merchantId , boolean showBalance) {
@@ -330,7 +208,7 @@
        return JSONUtil.toList(JSONUtil.parseArray(result.getData()), Coin.class);
    }
    public Address createAddress(String mainCoinType, String alias, String walletId, String callUrl) throws UdunException {
    public Address createAddress(String mainCoinType, String alias, String walletId, String callUrl) throws UdunException{
        Map<String, String> params = new HashMap<>();
        params.put("merchantId", merchantId);
        params.put("coinType", mainCoinType);
@@ -345,41 +223,49 @@
        return JSONUtil.toBean(result.getData(), Address.class);
    }
    @RequestMapping(action +"rechargeCallback.action")
    public ResultMsg rechargeCallback(HttpServletRequest request){
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String sign = request.getParameter("sign");
        String body = request.getParameter("body");
        try{
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String, Object> map = objectMapper.readValue(body, HashMap.class);
            double amounts = Double.parseDouble(map.get("amount").toString());  // 假设 amount 的值为 1000
            double decimals = Double.parseDouble(map.get("decimals").toString());
            double success_amount  = amounts / Math.pow(10, decimals);
            String address = map.get("address").toString();
            RechargeBlockchainOrder blockchainOrder = rechargeBlockchainOrderService.getOne(new LambdaQueryWrapper<>(RechargeBlockchainOrder.class)
                    .eq(RechargeBlockchainOrder::getSucceeded, 0)
                    .eq(RechargeBlockchainOrder::getChannelAddress, address).last(" limit 1 "));
            if(ObjectUtil.isEmpty(blockchainOrder)){
                ResultMsg resultMsg = new ResultMsg();
                resultMsg.setCode(200);
                return resultMsg;
            }
                SysUser user = sysUserService.getSysUserById( Long.getLong(blockchainOrder.getPartyId()));
            rechargeBlockchainOrderService.manualReceipt(1,blockchainOrder.getPartyId(), BigDecimal.valueOf(success_amount),user.getUsername());
            ResultMsg resultMsg = new ResultMsg();
            resultMsg.setCode(200);
            return resultMsg;
        }catch (Exception e){
            ResultMsg resultMsg = new ResultMsg();
            resultMsg.setCode(500);
            resultMsg.setMessage("回调处理失败");
            return resultMsg;
        }
    private static ChannelBlockchain createElement(String id, String coin, String img, String address,String blockchain_name) {
        ChannelBlockchain channelBlockchain = new ChannelBlockchain();
        channelBlockchain.setCoin(coin);
        channelBlockchain.setAddress(address);
        channelBlockchain.setBlockchain_name(blockchain_name);
        return channelBlockchain;
    }
    //回调地址关闭
//    @RequestMapping(action +"rechargeCallback.action")
//    public ResultMsg rechargeCallback(HttpServletRequest request){
//        String timestamp = request.getParameter("timestamp");
//        String nonce = request.getParameter("nonce");
//        String sign = request.getParameter("sign");
//        String body = request.getParameter("body");
//        try{
//            ObjectMapper objectMapper = new ObjectMapper();
//            Map<String, Object> map = objectMapper.readValue(body, HashMap.class);
//            double amounts = Double.parseDouble(map.get("amount").toString());  // 假设 amount 的值为 1000
//            double decimals = Double.parseDouble(map.get("decimals").toString());
//            double success_amount  = amounts / Math.pow(10, decimals);
//            String address = map.get("address").toString();
//
//
//            RechargeBlockchainOrder blockchainOrder = rechargeBlockchainOrderService.getOne(new LambdaQueryWrapper<>(RechargeBlockchainOrder.class)
//                    .eq(RechargeBlockchainOrder::getSucceeded, 0)
//                    .eq(RechargeBlockchainOrder::getChannelAddress, address).last(" limit 1 "));
//            if(ObjectUtil.isEmpty(blockchainOrder)){
//                ResultMsg resultMsg = new ResultMsg();
//                resultMsg.setCode(200);
//                return resultMsg;
//            }
//                SysUser user = sysUserService.getSysUserById( Long.getLong(blockchainOrder.getPartyId()));
//            rechargeBlockchainOrderService.manualReceipt(1,blockchainOrder.getPartyId(), BigDecimal.valueOf(success_amount),user.getUsername());
//            ResultMsg resultMsg = new ResultMsg();
//            resultMsg.setCode(200);
//            return resultMsg;
//        }catch (Exception e){
//            ResultMsg resultMsg = new ResultMsg();
//            resultMsg.setCode(500);
//            resultMsg.setMessage("回调处理失败");
//            return resultMsg;
//        }
//    }
    /**
     * 根据第三方充值链接