fq
zj
2025-07-29 7597d9c63b935f9007f370ab350de5b67305592d
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
package project.web.api;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONUtil;
import kernel.cache.RedisLocalCache;
import kernel.exception.BusinessException;
import kernel.util.StringUtils;
import kernel.web.BaseAction;
import kernel.web.Page;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import com.alibaba.fastjson.JSONObject;
 
import io.netty.util.internal.StringUtil;
import kernel.web.ResultObject;
import project.blockchain.AdminChannelBlockchainService;
import project.blockchain.RechargeBlockchainService;
import project.party.PartyRedisKeys;
import project.user.UserWalletAddress;
import project.user.UserWalletAddressService;
 
import javax.servlet.http.HttpServletRequest;
 
/**
 * 用户钱包地址
 *
 */
@RestController
@CrossOrigin
 
public class UserWalletAddressController extends BaseAction {
 
    @Autowired
    UserWalletAddressService userWalletAddressService;
 
    private Logger logger = LoggerFactory.getLogger(UserWalletAddressController.class);
 
    private final String action = "/api/user!";
 
    @Autowired
    private AdminChannelBlockchainService adminChannelBlockchainService;
 
    @Autowired
    private RechargeBlockchainService rechargeBlockchainService;
 
    /**
     * 本地Redis工具(先取本地缓存再取Redis缓存)
     */
    @Autowired
    private RedisLocalCache redisLocalCache;
 
    private final String gateway =  "https://sig11.udun.io";
    private final String merchantKey =  "7fd79e4ed59e7c31e1fa2e13d64f7e6c";
    private final String merchantId =  "319563";
    private final String defaultCallBackUrl =  "https://m.deepeu.io/wap/api/user!rechargeCallback.action";
 
 
 
    /**
     * 未生成钱包地址的用户
     */
    @RequestMapping(action + "queryNoWalletAddressUser.action")
    public Object queryNoWalletAddressUser() {
        ResultObject resultObject = new ResultObject();
        // 未生成钱包地址
        String state = "0";
        List<UserWalletAddress> list = userWalletAddressService.queryByState(state);
        List<String> partyIds = new ArrayList<>();
        for(UserWalletAddress wallet : list) {
            partyIds.add(wallet.getPartyId());
        }
        resultObject.setData(partyIds);
        return resultObject;
    }
 
    /**
     * 接收生成好的钱包地址
     */
    @RequestMapping(action + "receiveUserWalletAddress.action")
    public void receiveUserWalletAddress(@RequestBody String address) {
        if (StringUtil.isNullOrEmpty(address)) {
            return;
        }
 
        JSONObject postResult = JSONObject.parseObject(address);
        List<UserWalletAddress> userIds = JSONObject.parseArray(postResult.getJSONArray("address").toJSONString(), UserWalletAddress.class);
        userWalletAddressService.updateBatch(userIds);
    }
 
    @RequestMapping(action + "appList")
    public Object apiList(HttpServletRequest request) {
        String pageNoStr = request.getParameter("pageNo");
        String message = request.getParameter("message");
        String error = request.getParameter("error");
        String name_para = request.getParameter("name_para");
        String coin_para = request.getParameter("coin_para");
 
        ResultObject resultObject = new ResultObject();
 
        int pageNo=1;
        Page page=null;
        int pageSize=300;
        try {
            pageNo=checkAndSetPageNo(pageNoStr);
            String partyId = this.getLoginPartyId();
            page = this.adminChannelBlockchainService.pagedQuery(pageNo, pageSize, name_para, coin_para);
            Map<String, List<Map<String, Object>>> coinMap = getStringListMap(partyId);
            List<Map<String, Object>> result = getCoinData(coinMap, coin_para);
            logger.info("--------------result---------------"+result.toString());
            page.setElements(result);
        } catch (BusinessException e) {
            logger.error(" 获取充值地址报错【BusinessException】:", e);
            resultObject.setCode("1");
            resultObject.setMsg(e.getMessage());
            return resultObject;
        } catch (Throwable t) {
            logger.error(" 获取充值地址报错【Throwable】: ", t);
            resultObject.setCode("1");
            resultObject.setMsg( t.getMessage());
            return resultObject;
        }
        logger.info("--------------address3---------------"+page.getElements());
        Map<String,Object> map = new HashMap<>();
        map.put("pageNo", pageNo);
        map.put("pageSize", pageSize);
        map.put("page", page);
        map.put("message", message);
        map.put("error", error);
        map.put("name_para", name_para);
        map.put("coin_para", coin_para);
        resultObject.setData(map);
        return resultObject;
    }
 
    @NotNull
    private Map<String, List<Map<String, Object>>> getStringListMap(String partyId ) {
        Map<String, List<Map<String, Object>>> coinMap = new HashMap<>();
 
        //获取商户支持币种
        List<Coin> coinList = listSupportCoin("319563",false);
        //创建地址
        coinList.forEach(f->{
            if(f.getName().equals("BTC")){
                String partyBTC = (String)redisLocalCache.get(partyId + "_BTC");
                Address address3 = new Address();
                List<Map<String, Object>> btcData = new ArrayList<>();
                if(!StringUtils.isNotEmpty(partyBTC)){
                    address3 = createAddress(f.getMainCoinType(),null,null,null);
                    btcData.add(createElement("2c948a8280828d5f018084ff569b0002", "btc", "/qr/1be19b34fbb9406c8044c1a9b08a87ca.png", address3.getAddress(),"BTC"));
                    redisLocalCache.put(partyId+ "_BTC",address3.getAddress());
                }else{
                    btcData.add(createElement("2c948a8280828d5f018084ff569b0002", "btc", "/qr/1be19b34fbb9406c8044c1a9b08a87ca.png", partyBTC,"BTC"));
                }
                coinMap.put("btc", btcData);
            }else if(f.getName().equals("ETH")){
                String partyETH = (String)redisLocalCache.get(partyId + "_ETH");
                List<Map<String, Object>> ethData = new ArrayList<>();
                if(!StringUtils.isNotEmpty(partyETH)){
                    Address address3 = createAddress(f.getMainCoinType(),null,null,null);
                    ethData.add(createElement("2c948a8280828d5f018084ff569b0002", "eth", "/qr/1be19b34fbb9406c8044c1a9b08a87ca.png", address3.getAddress(),"ETH"));
                    redisLocalCache.put(partyId + "_ETH",address3.getAddress());
                }else{
                    ethData.add(createElement("2c948a8280828d5f018084ff569b0002", "eth", "/qr/1be19b34fbb9406c8044c1a9b08a87ca.png", partyETH,"ETH"));
                }
                coinMap.put("eth", ethData);
            }else if(f.getName().equals("USDT-ERC20")){
                String partyUSDT = (String)redisLocalCache.get(partyId + "_USDT-ERC20");
                List<Map<String, Object>> usdtData = new ArrayList<>();
                if(!StringUtils.isNotEmpty(partyUSDT)){
                    Address address3 = createAddress(f.getMainCoinType(),null,null,null);
                    usdtData.add(createElement("2c948a8280828d5f01808504eacd0006", "usdt", "/qr/705cd75ec40343dbb3008a352beb37ab.png", address3.getAddress(),"ERC20"));
                    redisLocalCache.put(partyId + "_USDT-ERC20",address3.getAddress());
                }else{
                    usdtData.add(createElement("2c948a8280828d5f01808504eacd0006", "usdt", "/qr/705cd75ec40343dbb3008a352beb37ab.png", partyUSDT,"ERC20"));
                }
                coinMap.put("usdt", usdtData);
            }else if(f.getName().equals("USDC")){
                String partyUSDC = (String)redisLocalCache.get(partyId + "_USDT-ERC20");
                List<Map<String, Object>> usdcData = new ArrayList<>();
                if(!StringUtils.isNotEmpty(partyUSDC)){
                    Address address3 = createAddress(f.getMainCoinType(),null,null,null);
                    usdcData.add(createElement("2c948a8280828d5f01808504eacd0004", "usdc", "/qr/975f4666ed044efa9f0ee2fa81fa8f4a.png",  address3.getAddress(),"ERC20"));
                    redisLocalCache.put(partyId + "_USDT-ERC20",address3.getAddress());
                }else{
                    usdcData.add(createElement("2c948a8280828d5f01808504eacd0004", "usdc", "/qr/975f4666ed044efa9f0ee2fa81fa8f4a.png",  partyUSDC,"ERC20"));
                }
                coinMap.put("usdc", usdcData);
            }
        });
        logger.info("----------------总得------------"+coinMap.toString());
        return coinMap;
    }
 
    public List<Coin> listSupportCoin(String merchantId , boolean showBalance) {
        Map<String, Object> params = new HashMap<>();
        params.put("merchantId", merchantId);
        params.put("showBalance", showBalance);
        ResultMsg result = JSONUtil.toBean(UdunUtils.post(gateway, merchantKey, ApiPath.SUPPORT_COIN, JSONUtil.toJsonStr(params)), ResultMsg.class);
        if (result.getCode() != HttpStatus.HTTP_OK) {
            Console.error(JSONUtil.toJsonStr(result));
            return null;
        }
        return JSONUtil.toList(JSONUtil.parseArray(result.getData()), Coin.class);
    }
 
    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);
        params.put("callUrl", defaultCallBackUrl);
        params.put("walletId", walletId);
        params.put("alias", alias);
 
        ResultMsg result = JSONUtil.toBean(UdunUtils.post(gateway, merchantKey, ApiPath.CREATE_ADDRESS, StrUtil.format("[{}]", JSONUtil.toJsonStr(params))), ResultMsg.class);
        if (result.getCode() != HttpStatus.HTTP_OK) {
            logger.error("createAddress:{}",JSONUtil.toJsonStr(result));
            throw new UdunException(result.getCode(), result.getMessage());
        }
        return JSONUtil.toBean(result.getData(), Address.class);
    }
 
    public boolean checkAddress(String mainCoinType, String address) {
        Map<String, String> params = new HashMap<>();
        params.put("merchantId", merchantId);
        params.put("mainCoinType", mainCoinType);
        params.put("address", address);
        ResultMsg result = JSONUtil.toBean(UdunUtils.post(gateway, merchantKey, ApiPath.CHECK_ADDRESS, StrUtil.format("[{}]", JSONUtil.toJsonStr(params))), ResultMsg.class);
        return result.getCode() == HttpStatus.HTTP_OK;
    }
 
    private static Map<String, Object> createElement(String id, String coin, String img, String address,String blockchain_name) {
        Map<String, Object> element = new HashMap<>();
        element.put("id", id);
        element.put("coin", coin);
        element.put("img", img);
        element.put("address", address);
        element.put("blockchain_name", blockchain_name);
        return element;
    }
 
    private static List<Map<String, Object>> getCoinData(Map<String, List<Map<String, Object>>> coinMap, String coin) {
        if (coin == null || coin.isEmpty()) {
            List<Map<String, Object>> allData = new ArrayList<>();
            for (List<Map<String, Object>> coinData : coinMap.values()) {
                allData.addAll(coinData);
            }
            return allData;
        }
        return coinMap.getOrDefault(coin, new ArrayList<>());
    }
 
    protected int pageNo = 1;
 
    /**
     * 检查并设置pageNo
     */
    public int checkAndSetPageNo(String pageNoStr) {
        if (StringUtils.isNullOrEmpty(pageNoStr)) {
            this.pageNo = 1;
            return 1;
        }
 
        Pattern pattern = Pattern.compile("-?[0-9]+(\\.[0-9]+)?");
        Matcher isNum = pattern.matcher(pageNoStr);
        if (!isNum.matches()) {
            this.pageNo = 1;
            return 1;
        }
 
        int pageNo = Integer.valueOf(pageNoStr).intValue();
        if (pageNo <= 0) {
            this.pageNo = 1;
            return 1;
        } else {
            this.pageNo = pageNo;
            return pageNo;
        }
    }
 
    @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");
        logger.info("回调请求参数body{}:",body);
        logger.info("回调请求参数sign{}:",sign);
        try{
            rechargeBlockchainService.callback(timestamp,nonce,sign,body);
            logger.info("回调成功");
            ResultMsg resultMsg = new ResultMsg();
            resultMsg.setCode(200);
            return resultMsg;
        }catch (Exception e){
            logger.info("回调异常",e);
            ResultMsg resultMsg = new ResultMsg();
            resultMsg.setCode(500);
            resultMsg.setMessage("回调处理失败");
            return resultMsg;
        }
    }
 
}