1
zj
2026-03-07 420b832c4a7f55b0af636c94828220fd8e99ffaf
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
package com.yami.trading.api.controller;
 
import cn.hutool.core.util.StrUtil;
import com.yami.trading.api.util.ServletUtil;
import com.yami.trading.bean.model.User;
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.YamiShopBindException;
import com.yami.trading.common.util.Arith;
import com.yami.trading.common.util.DateTimeTools;
import com.yami.trading.common.util.GoogleAuthenticator;
import com.yami.trading.common.util.StringUtils;
import com.yami.trading.security.common.util.SecurityUtils;
import com.yami.trading.service.IdentifyingCodeTimeWindowService;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
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.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 提现
 */
@RestController
@CrossOrigin
@RequestMapping("api/withdraw")
@Api(tags = "提现")
@Slf4j
public class ApiWithdrawController implements InitializingBean {
    @Autowired
    private WithdrawService withdrawService;
    @Autowired
    private UserService userService;
    @Autowired
    private SessionTokenService sessionTokenService;
    @Autowired
    private SysparaService sysparaService;
    @Autowired
    protected WalletLogService walletLogService;
    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired
    private IdentifyingCodeTimeWindowService identifyingCodeTimeWindowService;
 
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.printf(passwordEncoder.encode("000000"));
    }
 
    /**
     * 首次进入页面,传递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);
        // 返回给前端用户绑定的验证方式(谷歌、短信、邮箱)
        List<Map<String, String>> typeList = userService.getTypeListById(partyId);
        data.put("verifcode_type_list", typeList);
        return Result.succeed(data);
    }
 
    /**
     * 提现申请
     * <p>
     * safeword 资金密码
     * amount 提现金额
     * from 客户转出地址
     * currency 货币 CNY USD
     * channel 渠道 USDT,BTC,ETH
     * verifcode_type 验证方式
     * verifcode_value 验证的值
     */
    @ApiOperation("提现申请")
    @PostMapping("apply")
    public Result apply(HttpServletRequest request, String session_token, String safeword,
                        String amount, String from, String currency,
                        String channel, String language, String verifcode_type, String verifcode_value) {
        String partyId = SecurityUtils.getUser().getUserId();
        User currentUser = userService.getById(partyId);
        if (currentUser != null && currentUser.getAccountType() != null && currentUser.getAccountType() == 1) {
            throw new YamiShopBindException("模拟账户不支持提现");
        }
        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("资金密码错误");
            }
            if (StringUtils.isNotEmpty(verifcode_type)) {
                // 校验用户的验证码
                userService.checkCode(partyId, verifcode_type, verifcode_value);
            }
        }
        Object object = this.sessionTokenService.cacheGet(session_token);
        this.sessionTokenService.del(session_token);
//        if (null == object || !SecurityUtils.getUser().getUserId().equals((String) object)) {
//            throw new YamiShopBindException("请稍后再试");
//        }
        Withdraw withdraw = new Withdraw();
        withdraw.setUserId(partyId);
        withdraw.setVolume(new BigDecimal(amount_double));
        withdraw.setAddress(from);
        withdraw.setCurrency(currency);
        withdraw.setTx("");
        withdraw.setDeviceIp(ServletUtil.getIp(request));
        // 保存
        this.withdrawService.saveApply(withdraw, channel, null, language);
        return Result.succeed(null);
    }
 
    /**
     * 提现订单详情
     * <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", withdraw.getCreateTime());
        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());
            }
 
            // createtime createTimeTs TODO
            String createtime = (String)log.get("createtime");
            Integer createTimeTs = (Integer) log.get("createTimeTs");
            if (StrUtil.isNotBlank(createtime)) {
                Date oriCreateTime = DateTimeTools.readQueryTime(createtime, "yyyy-MM-dd HH:mm:ss", ZoneId.systemDefault());
                log.put("createtime", oriCreateTime);
            }
            if (createTimeTs != null && createTimeTs > 0) {
                long showCreateTimeTs = DateTimeTools.transferShowTimeToClientTime(createTimeTs.longValue());
                log.put("createTimeTs", showCreateTimeTs);
            }
        }
 
        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(value = "channel") String channel) {
        return Result.succeed(withdrawService.getWithdrawLimitBySymbol(channel, true));
    }
 
    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;
    }
}