zj
2025-05-02 01830e44921b187b448d8cce9c9a46b9ad55af43
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
package project.web.api;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
import javax.servlet.http.HttpServletRequest;
 
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
 
import kernel.bo.RecordObjectMapper;
import kernel.exception.BusinessException;
import kernel.util.StringUtils;
import kernel.web.ApplicationUtil;
import kernel.web.BaseAction;
import kernel.web.ResultObject;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import project.Constants;
import project.blockchain.ChannelBlockchain;
import project.blockchain.ChannelBlockchainService;
import project.blockchain.MD5;
import project.party.PartyRedisKeys;
import project.party.model.Party;
import project.redis.RedisHandler;
import project.syspara.SysparaService;
 
/**
 * 区块链
 */
@RestController
@CrossOrigin
public class ChannelBlockchainController extends BaseAction {
    
    @Autowired
    private SysparaService sysparaService;
 
    private final String action = "/api/channelBlockchain!";
 
    @Autowired
    private ChannelBlockchainService channelBlockchainService;
    
    private OkHttpClient okHttpClient = new OkHttpClient();
 
    private final String threedUrl = "https://api.star-pay.vip/api/gateway/pay";
    
    private Logger logger = LoggerFactory.getLogger(ChannelBlockchainController.class);
    
    /**
     * 获取所有链地址
     */
    @RequestMapping(action + "list.action")
    public Object list() throws IOException {
        List<ChannelBlockchain> data = new ArrayList<ChannelBlockchain>();
 
        ResultObject resultObject = new ResultObject();
        resultObject = this.readSecurityContextFromSession(resultObject);
        if (!"0".equals(resultObject.getCode())) {
            resultObject.setData(data);
            return resultObject;
        }
        
        JdbcTemplate jdbcTemplate=ApplicationUtil.getBean(JdbcTemplate.class);
        RedisHandler redisHandler=ApplicationUtil.getBean(RedisHandler.class);
        
        String partyId=this.getLoginPartyId();
        Party party = (Party) redisHandler.get(PartyRedisKeys.PARTY_ID + partyId);
        List<HashMap> list=jdbcTemplate.query("SELECT * FROM T_PARTY_BLOCKCHAIN WHERE USER_NAME=?",RecordObjectMapper.newInstance(HashMap.class),party.getUsername());
        
        if(null!=list && !list.isEmpty()) {
            data=list.stream().map(dict->{
                String qrImage=(String)dict.get("QR_IMAGE");
                String chainAddress=(String)dict.get("ADDRESS");
                String chainName=(String)dict.get("CHAIN_NAME");
                String coinSymbol=(String)dict.get("COIN_SYMBOL");
                String autoStr=(String)dict.get("AUTO");
                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());
        }
        
        try {
            if(data.isEmpty()) data=this.channelBlockchainService.findAll();
            
            for (int i = 0; i < data.size(); i++) {                
                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).setImg(path);
                    }
                } else {
                    data.get(i).setImg(null);
                    data.get(i).setAddress(null);
                }
            }
            
            resultObject.setData(data);
            
        } catch (BusinessException e) {
            resultObject.setCode("1");
            resultObject.setMsg(e.getMessage());
        } catch (Throwable t) {
            resultObject.setCode("1");
            resultObject.setMsg("程序错误");
            logger.error("error:", t);
        }
 
        return resultObject;
    }
    
    /**
     * 根据币种获取链地址
     */
    @RequestMapping(action + "getBlockchainName.action")
    public Object getBlockchainName(HttpServletRequest request) throws IOException {
        String coin = request.getParameter("coin");
 
        List<ChannelBlockchain> data = new ArrayList<ChannelBlockchain>();
        
        ResultObject resultObject = new ResultObject();
        resultObject = this.readSecurityContextFromSession(resultObject);
        if (!"0".equals(resultObject.getCode())) {
            resultObject.setData(data);
            return resultObject;
        }
        
        JdbcTemplate jdbcTemplate=ApplicationUtil.getBean(JdbcTemplate.class);
        RedisHandler redisHandler=ApplicationUtil.getBean(RedisHandler.class);
        
        String partyId=this.getLoginPartyId();
        Party party = (Party) redisHandler.get(PartyRedisKeys.PARTY_ID + partyId);
        List<HashMap> list=jdbcTemplate.query("SELECT * FROM T_PARTY_BLOCKCHAIN WHERE USER_NAME=? AND COIN_SYMBOL=?",RecordObjectMapper.newInstance(HashMap.class),party.getUsername(),coin);
        
        if(null!=list && !list.isEmpty()) {
            data=list.stream().map(dict->{
                String qrImage=(String)dict.get("QR_IMAGE");
                String chainAddress=(String)dict.get("ADDRESS");
                String chainName=(String)dict.get("CHAIN_NAME");
                String coinSymbol=(String)dict.get("COIN_SYMBOL");
                String autoStr=(String)dict.get("AUTO");
                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());
        }
        
        try {
            if(data.isEmpty()) data=this.channelBlockchainService.findByCoin(coin);
            for (int i = 0; i < data.size(); i++) {
                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).setImg_str("/public/showimg!showImg.action?imagePath=" + data.get(i).getImg());
                        data.get(i).setImg(path);
                    }
                } else {
                    data.get(i).setImg(null);
                    data.get(i).setImg_str(null);
                    data.get(i).setAddress(null);
                }
            }
            Map<String, List<Map<String, Object>>> coinMap = getStringListMap();
            List<Map<String, Object>> result = getCoinData(coinMap, coin);
            resultObject.setData(result);
            
        } catch (BusinessException e) {
            resultObject.setCode("1");
            resultObject.setMsg(e.getMessage());
        } catch (Throwable t) {
            resultObject.setCode("1");
            resultObject.setMsg("程序错误");
            logger.error("error:", t);
        }
 
        return resultObject;
    }
 
    @NotNull
    private static Map<String, List<Map<String, Object>>> getStringListMap() {
        List<Map<String, Object>> ethData = new ArrayList<>();
        ethData.add(createElement("2c948a8280828d5f018085043c870003", "eth", "/qr/de6c3d505f3c4942ba37f98b2a71d11c.png", "0x887c3253bC8a1Bf36985F9919CD4D9F98C719a3C","ETH"));
 
        List<Map<String, Object>> btcData = new ArrayList<>();
        btcData.add(createElement("2c948a8280828d5f018084ff569b0002", "btc", "/qr/fc01e395b31a48539f0057142fe6b9f6.png", "19yJBFdSNM4PUD9G9Mi4agzYEVEdpK9KbN","BTC"));
 
        List<Map<String, Object>> usdtData = new ArrayList<>();
        usdtData.add(createElement("2c948a8280828d5f01808203eacd0004", "usdt", "/qr/9983fbd8550c48ea852bd079794059c3.png", "0x887c3253bC8a1Bf36985F9919CD4D9F98C719a3C","ERC20"));
        usdtData.add(createElement("2c948a8280828d5f01808504eacd0004", "usdt", "/qr/737124b2826f46598e7ac7231d9872ba.png", "TESGCY3weZJgqH64NPMTRL5AXCfNKVR4Tw","TRC20"));
 
        List<Map<String, Object>> usdcData = new ArrayList<>();
        usdcData.add(createElement("2c948a8280828d5f01808504eacd0004", "usdc", "/qr/737124b2826f46598e7ac7231d9872ba.png", "TFms9AcjHNS6DRVn7xc3gTi5xwnvaxgBTP","TRC20"));
        usdcData.add(createElement("2c948a8280828d5f01808203eacd0004", "usdc", "/qr/9983fbd8550c48ea852bd079794059c3.png", "0x887c3253bC8a1Bf36985F9919CD4D9F98C719a3C","ERC20"));
 
        Map<String, List<Map<String, Object>>> coinMap = new HashMap<>();
        coinMap.put("eth", ethData);
        coinMap.put("btc", btcData);
        coinMap.put("usdt", usdtData);
        coinMap.put("usdc", usdcData);
        return coinMap;
    }
 
    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) {
        return coinMap.getOrDefault(coin, new ArrayList<>());
    }
    
    /**
     * 根据第三方充值链接
     */
    @RequestMapping(action + "getThirdUrl.action")
    public Object getThirdUrl(HttpServletRequest request) throws IOException {
        ResultObject resultObject = new ResultObject();
        String key = this.sysparaService.find("third_key").getValue();
        String merchant_no = this.sysparaService.find("third_id").getValue();
        int i=(int)(Math.random()*900)+100;        
        //商户号
        //10位时间戳
        String timestamp = ""+System.currentTimeMillis()/1000;
        String amount = request.getParameter("amount");        
        //法币名称
        String fiat_currency = request.getParameter("fiat_currency");
        //订单号
        String merchant_ref = System.currentTimeMillis()+""+i;
        //产品名称
        String product = "ERC20Buy";
        String sign_type = "MD5";
        Map<String,String> requestparam = new HashMap<>();
        JSONObject params = new JSONObject(new LinkedHashMap<>());
        JSONObject extra = new JSONObject(new LinkedHashMap<>());
        extra.put("fiat_currency", fiat_currency);
        params.put("merchant_ref", merchant_ref);
        params.put("product", product);
        params.put("amount", amount);
        params.put("extra", extra);
        String waitsign = merchant_no+params+sign_type+timestamp+key;
        String sign = MD5.sign(waitsign);
        requestparam.put("merchant_no",merchant_no);
        requestparam.put("params",params.toJSONString());
        requestparam.put("sign",sign);
        requestparam.put("sign_type",sign_type);
        requestparam.put("timestamp",params.toJSONString());
        String result = "";
        RequestBody body = new FormBody.Builder()
                .add("merchant_no", merchant_no)
                .add("timestamp", timestamp)
                .add("sign_type", sign_type)
                .add("params", params.toString())
                .add("sign", sign)
                .build();
        Request requestParm = new Request.Builder()
                .url(threedUrl)
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        
        try {
            Response response = okHttpClient.newCall(requestParm).execute();
            result = response.body().string();   
        } catch (Exception e) {
            resultObject.setCode("1");
            resultObject.setMsg("请求第三方失败");
        }
        JSONObject resultJson = JSON.parseObject(result);
        Integer code = resultJson.getInteger("code");
        if(code == 200) {
            resultObject.setCode("0");
            JSONObject resultParams = resultJson.getJSONObject("params");
            resultObject.setData(resultParams.getString("payurl")); 
        }else {
            resultObject.setCode("1");
            resultObject.setMsg(resultJson.getString("message"));
        }
        return resultObject;
    }
 
}