新版仿ok交易所-后端
zyy
2025-09-13 c51d40152063cd014fa7d444148960f6f9c3282c
trading-order-admin/src/main/java/com/yami/trading/api/controller/ApiWithdrawController.java
@@ -1,12 +1,21 @@
package com.yami.trading.api.controller;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yami.trading.api.UD.*;
import com.yami.trading.bean.model.CapitaltWallet;
import com.yami.trading.bean.model.RechargeBlockchainOrder;
import com.yami.trading.bean.model.Withdraw;
import com.yami.trading.common.constants.Constants;
import com.yami.trading.common.domain.Result;
import com.yami.trading.common.exception.BusinessException;
import com.yami.trading.common.exception.YamiShopBindException;
import com.yami.trading.common.util.Arith;
import com.yami.trading.common.util.C2cLock;
import com.yami.trading.common.util.DateUtils;
import com.yami.trading.common.util.StringUtils;
import com.yami.trading.security.common.util.SecurityUtils;
@@ -16,12 +25,14 @@
import com.yami.trading.service.syspara.SysparaService;
import com.yami.trading.service.user.UserService;
import com.yami.trading.service.user.WalletLogService;
import com.yami.trading.sys.model.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
@@ -50,6 +61,9 @@
    @Autowired
    CapitaltWalletService capitaltWalletService;
    @Autowired
    UdunClient udunClient;
    /**
     * 首次进入页面,传递session_token
     */
@@ -77,18 +91,20 @@
    public Result apply(String session_token, String safeword,
                                String amount, String from, String currency,
                                String channel){
        String partyId = SecurityUtils.getUser().getUserId();
        String error = this.verif(amount);
        if (!StringUtils.isNullOrEmpty(error)) {
            throw new YamiShopBindException(error);
        }
        double amount_double = Double.valueOf(amount).doubleValue();
        Result resultObject=new Result();
        try {
            String partyId = SecurityUtils.getUser().getUserId();
            String error = this.verif(amount);
            if (!StringUtils.isNullOrEmpty(error)) {
                throw new YamiShopBindException(error);
            }
            double amount_double = Double.valueOf(amount).doubleValue();
        // 交易所提现是否需要资金密码
        String exchange_withdraw_need_safeword = this.sysparaService.find("exchange_withdraw_need_safeword").getSvalue();
        if(StringUtils.isEmptyString(exchange_withdraw_need_safeword)) {
            throw new YamiShopBindException("系统参数错误");
        }
            // 交易所提现是否需要资金密码
            String exchange_withdraw_need_safeword = this.sysparaService.find("exchange_withdraw_need_safeword").getSvalue();
            if(StringUtils.isEmptyString(exchange_withdraw_need_safeword)) {
                throw new YamiShopBindException("系统参数错误");
            }
//        if ("true".equals(exchange_withdraw_need_safeword)) {
//
@@ -104,25 +120,89 @@
//            }
//        }
        // 获取资金账户(capital)
        CapitaltWallet capitaltWallet = capitaltWalletService.getOne(new LambdaQueryWrapper<>(CapitaltWallet.class)
                .eq(CapitaltWallet::getUserId, partyId).last(" limit 1 "));
        if(capitaltWallet.getMoney().compareTo(new BigDecimal(amount)) < 0){
            throw new YamiShopBindException("Insufficient available balance for withdrawal!");
        }
            // 获取资金账户(capital)
            CapitaltWallet capitaltWallet = capitaltWalletService.getOne(new LambdaQueryWrapper<>(CapitaltWallet.class)
                    .eq(CapitaltWallet::getUserId, partyId).last(" limit 1 "));
            if(capitaltWallet.getMoney().compareTo(new BigDecimal(amount)) < 0){
                throw new YamiShopBindException("Insufficient available balance for withdrawal!");
            }
        this.sessionTokenService.del(session_token);
        Withdraw withdraw = new Withdraw();
        withdraw.setUserId(partyId);
        withdraw.setVolume(new BigDecimal(amount_double));
        withdraw.setAddress(from);
        withdraw.setCurrency(currency);
        withdraw.setTx("");
        // 保存
        this.withdrawService.saveApply(withdraw, channel, null);
        return Result.succeed(null);
            this.sessionTokenService.del(session_token);
            Withdraw withdraw = new Withdraw();
            withdraw.setUserId(partyId);
            withdraw.setVolume(new BigDecimal(amount_double));
            withdraw.setAddress(from);
            withdraw.setCurrency(currency);
            withdraw.setTx("");
            //获取商户支持币种
            List<Coin> coinList = udunClient.listSupportCoin(false);
            Coin coin = coinList.stream().filter(x -> x.getName().equals(channel)).findFirst().orElse(null);
            if (coin == null) {
                throw new YamiShopBindException("不支持的提现币种");
            }
            // 保存
            this.withdrawService.saveApply(withdraw, channel, null);
            ResultMsg resultMsg = udunClient.withdraw(from, withdraw.getVolume(), coin.getMainCoinType(),
                    coin.getCoinType(), withdraw.getOrderNo(), null);
            if (resultMsg.getCode() != HttpStatus.HTTP_OK) {
                log.error("withdraw:{}", JSONUtil.toJsonStr(resultMsg));
                throw new UdunException(resultMsg.getCode(), resultMsg.getMessage());
            }
            resultObject.setCode(0);
        } catch (UdunException e) {
            resultObject.setCode(1);
            resultObject.setMsg(e.getMessage());
            log.error("error:" + e.getMessage());
            throw e;
        } catch (Throwable t) {
            resultObject.setCode(1);
            resultObject.setMsg(t.getMessage());
            log.error("error: {}", t.getMessage());
            throw new RuntimeException(t);
        }
        return resultObject;
    }
    @PostMapping("withdrawCallback.action")
    public ResultMsg withdrawCallback(HttpServletRequest request){
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String sign = request.getParameter("sign");
        String body = request.getParameter("body");
        ResultMsg resultMsg = new ResultMsg();
        try{
            boolean flag = udunClient.checkSign(timestamp, nonce, body, sign);
            log.info("===withdrawCallback===sign:{}", flag);
            if (!flag){
                resultMsg.setCode(406);
                resultMsg.setMessage("提现回调验签失败");
                return resultMsg;
            }
            ObjectMapper objectMapper = new ObjectMapper();
            Map<String, Object> map = objectMapper.readValue(body, HashMap.class);
            String address = map.get("address").toString();
            String order_no = map.get("businessId").toString();
            Withdraw withdraw = withdrawService.getOne(new LambdaQueryWrapper<>(Withdraw.class)
                    .eq(Withdraw::getOrderNo, order_no).last(" limit 1 "));
            if(ObjectUtil.isEmpty(withdraw) && withdraw.getStatus() != 0 && !withdraw.getAddress().equals(address)){
                log.info("withdraw failed:{}", withdraw);
                resultMsg.setCode(200);
                return resultMsg;
            }
            withdrawService.examineOk(order_no, Long.valueOf(withdraw.getUserId()));
            resultMsg.setCode(200);
        }catch (Exception e){
            resultMsg.setCode(500);
            resultMsg.setMessage("回调处理失败");
        }
        return resultMsg;
    }
    /**
     * 提现订单详情
     * <p>