新版仿ok交易所-后端
zyy
2025-10-11 f620192bcac7f5cb910a99e092edcee00280ce10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
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;
import com.yami.trading.service.CapitaltWalletService;
import com.yami.trading.service.SessionTokenService;
import com.yami.trading.service.WithdrawService;
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;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 提现
 */
@RestController
@RequestMapping("api/withdraw")
@Api(tags = "提现")
@Slf4j
public class ApiWithdrawController {
    @Autowired
    private WithdrawService withdrawService;
    @Autowired
    private UserService userService;
    @Autowired
    private SessionTokenService sessionTokenService;
    @Autowired
    private SysparaService sysparaService;
    @Autowired
    protected WalletLogService walletLogService;
    @Autowired
    CapitaltWalletService capitaltWalletService;
 
    @Autowired
    UdunClient udunClient;
 
    /**
     * 首次进入页面,传递session_token
     */
    @GetMapping("withdrawOpen")
    @ApiOperation("首次进入页面,传递session_token")
    public Result withdrawOpen() {
        String partyId = SecurityUtils.getUser().getUserId();
        String session_token = this.sessionTokenService.savePut(partyId);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("session_token", session_token);
        return Result.succeed(data);
    }
 
    /**
     * 提现申请
     * <p>
     * safeword 资金密码
     * amount 提现金额
     * from 客户转出地址
     * currency 货币 CNY USD
     * channel 渠道 USDT,BTC,ETH
     */
    @ApiOperation("提现申请")
    @PostMapping("apply")
    public Result apply(String session_token, String safeword,
                                String amount, String from, String currency,
                                String channel){
        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("系统参数错误");
            }
 
        if ("true".equals(exchange_withdraw_need_safeword)) {
 
            if (StringUtils.isEmptyString(safeword)) {
                throw new YamiShopBindException("资金密码不能为空");
            }
 
            if (safeword.length() < 6 || safeword.length() > 12) {
                throw new YamiShopBindException("资金密码必须6-12位");
            }
            if (!userService.checkLoginSafeword(SecurityUtils.getUser().getUserId(),safeword)){
                throw new YamiShopBindException("资金密码错误");
            }
        }
 
            // 获取资金账户(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("");
 
            //获取商户支持币种
            /*List<Coin> coinList = udunClient.listSupportCoin(false);
            String channelName = channel.replace("_", "");
            Coin coin = coinList.stream().filter(x -> x.getName().replace("-","").equals(channelName)).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 (YamiShopBindException e) { // 1. 显式捕获业务异常,优先处理
            resultObject.setCode(1);
            resultObject.setMsg(e.getMessage()); // 直接获取纯业务消息
            log.error("业务异常: {}", e.getMessage());
            throw e;
        } 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{
            log.info("===withdrawCallback===:{}", body);
            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;
            }
            Integer status = Integer.valueOf(map.get("status").toString());
            if (status == 3) { //交易成功
                withdrawService.examineOk(withdraw.getUuid(), null);
            } else if(status == 2) {   //驳回
                withdrawService.reject(withdraw.getUuid(), "订单失败:" + status, "withdrawCallback");
            }
            resultMsg.setCode(200);
        }catch (Exception e){
            resultMsg.setCode(500);
            resultMsg.setMessage("回调处理失败");
        }
        return resultMsg;
    }
 
 
    /**
     * 提现订单详情
     * <p>
     * order_no 订单号
     */
    @ApiOperation("提现订单详情")
    @GetMapping("get")
    public Result get(@RequestParam String order_no) throws IOException {
        Withdraw withdraw = this.withdrawService.findByOrderNo(order_no);
        if (withdraw==null){
            throw  new YamiShopBindException("订单不存在!");
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("order_no", withdraw.getOrderNo());
        map.put("volume", withdraw.getVolume());
        map.put("amount", withdraw.getAmount());
        map.put("create_time", DateUtils.format(withdraw.getCreateTime(), DateUtils.DF_yyyyMMddHHmmss));
        map.put("to", withdraw.getAddress());
        map.put("fee", withdraw.getAmountFee().doubleValue());
        map.put("coin_blockchain", withdraw.getMethod());
        map.put("coin",
                withdraw.getMethod().indexOf("BTC") != -1 || withdraw.getMethod().indexOf("ETH") != -1
                        ? withdraw.getMethod()
                        : "USDT");
        map.put("state", withdraw.getStatus());
        map.put("tx", withdraw.getTx());
        map.put("failure_msg", withdraw.getFailureMsg());
        return Result.succeed(map);
    }
 
    /**
     * 提现记录
     */
    @GetMapping("list")
    @ApiOperation("提现记录")
    public Result list(@RequestParam String page_no) {
        if (StringUtils.isNullOrEmpty(page_no)) {
            page_no = "1";
        }
        if (!StringUtils.isInteger(page_no)) {
            throw new YamiShopBindException("页码不是整数");
        }
        if (Integer.valueOf(page_no).intValue() <= 0) {
            throw new YamiShopBindException("页码不能小于等于0");
        }
        int page_no_int = Integer.valueOf(page_no).intValue();
        List<Map<String, Object>> data = this.walletLogService.pagedQueryWithdraw(page_no_int, 10, SecurityUtils.getUser().getUserId(), "1").getRecords();
        for (Map<String, Object> log : data) {
            if (null == log.get("coin") || !StringUtils.isNotEmpty(log.get("coin").toString()))
                log.put("coin", Constants.WALLET);
            else {
                log.put("coin", log.get("coin").toString().toUpperCase());
            }
        }
        return Result.succeed(data);
    }
 
    /**
     * 提现手续费
     * <p>
     * channel 渠道 USDT,OTC
     * amount 提币数量
     */
    @GetMapping("fee")
    @ApiOperation("提现手续费")
    public Result fee(String channel, String amount) {
            String error = this.verif(amount);
            if (!StringUtils.isNullOrEmpty(error)) {
                throw new YamiShopBindException(error);
            }
 
            double amount_double = Double.valueOf(amount).doubleValue();
 
            Map<String, Object> map = new HashMap<String, Object>();
 
            DecimalFormat df = new DecimalFormat("#.########");
 
            double fee = 0;
 
            channel = StringUtils.isEmptyString(channel) ? "USDT" : channel;
            if (channel.indexOf("BTC") != -1 || channel.indexOf("ETH") != -1) {
                map.put("withdraw_fee_type", "rate");
                fee = this.withdrawService.getOtherChannelWithdrawFee(amount_double);
            } else {
                // 手续费(USDT)
 
                // 提现手续费类型,fixed是单笔固定金额,rate是百分比,part是分段
                String withdraw_fee_type = this.sysparaService.find("withdraw_fee_type").getSvalue();
 
                // fixed单笔固定金额 和 rate百分比 的手续费数值
                double withdraw_fee = Double.valueOf(this.sysparaService.find("withdraw_fee").getSvalue());
 
                if ("fixed".equals(withdraw_fee_type)) {
                    fee = withdraw_fee;
                }
 
                if ("rate".equals(withdraw_fee_type)) {
                    withdraw_fee = Arith.div(withdraw_fee, 100);
                    fee = Arith.mul(amount_double, withdraw_fee);
                }
 
                if ("part".equals(withdraw_fee_type)) {
 
                    // 提现手续费part分段的值
                    String withdraw_fee_part = this.sysparaService.find("withdraw_fee_part").getSvalue();
 
                    String[] withdraw_fee_parts = withdraw_fee_part.split(",");
                    for (int i = 0; i < withdraw_fee_parts.length; i++) {
                        double part_amount = Double.valueOf(withdraw_fee_parts[i]);
                        double part_fee = Double.valueOf(withdraw_fee_parts[i + 1]);
                        if (amount_double <= part_amount) {
                            fee = part_fee;
                            break;
                        }
                        i++;
                    }
                }
                map.put("withdraw_fee_type", withdraw_fee_type);
            }
            double volume_last = Arith.sub(amount_double, fee);
            if (volume_last < 0) {
                volume_last = 0;
            }
 
            map.put("fee", fee);
            map.put("volume_last", df.format(volume_last));
 
 
 
        return Result.succeed(map);
    }
 
    /**
     * 提现限额
     * <p>
     * channel 渠道 USDT,OTC
     */
    @GetMapping("limit")
    @ApiOperation("提现限额")
    public Result limit(@RequestParam String channel) {
        Map<String, Object> map = new HashMap<String, Object>();
        channel = StringUtils.isEmptyString(channel) ? "USDT" : channel;
        map.put("limit", this.sysparaService.find("withdraw_limit").getSvalue());
        map.put("limitMax", this.sysparaService.find("withdraw_limit_max").getSvalue());
        return Result.succeed(map);
    }
 
    private String verif(String amount) {
        if (StringUtils.isNullOrEmpty(amount)) {
            return "提币数量必填";
        }
        if (!StringUtils.isDouble(amount)) {
            return "提币数量输入错误,请输入浮点数";
        }
        if (Double.valueOf(amount).doubleValue() <= 0) {
            return "提币数量不能小于等于0";
        }
        return null;
    }
}