新版仿ok交易所-后端
1
zyy
2025-12-22 4363803b4107bb7698391de0867236ce553e8f50
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
package com.yami.trading.service.impl;
 
import com.mysql.cj.util.StringUtils;
import com.yami.trading.bean.log.domain.CodeLog;
import com.yami.trading.common.http.HttpHelper;
import com.yami.trading.common.manager.BlacklistIpTimeWindow;
import com.yami.trading.common.manager.SendCountTimeWindow;
import com.yami.trading.common.manager.sms.BUKASmsUtils;
import com.yami.trading.common.manager.sms.GoToneSmsUtils;
import com.yami.trading.service.EmailSendService;
import com.yami.trading.service.IdentifyingCodeService;
import com.yami.trading.service.IdentifyingCodeTimeWindowService;
import com.yami.trading.service.SmsSendService;
import com.yami.trading.service.syspara.SysparaService;
import com.yami.trading.service.system.CodeLogService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.io.IOException;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
 
@Service
public class IdentifyingCodeServiceImpl  implements IdentifyingCodeService {
 
 
 
    private Logger logger = LoggerFactory.getLogger(IdentifyingCodeServiceImpl.class);
 
    @Autowired
    private SmsSendService smsSendService;
    @Autowired
    private EmailSendService  emailSendService;
 
    @Autowired
    private IdentifyingCodeTimeWindowService identifyingCodeTimeWindowService;
 
    @Autowired
    private SendCountTimeWindow sendCountTimeWindow;
 
 
    @Autowired
    private BlacklistIpTimeWindow blacklistIpTimeWindow;
 
    @Autowired
    private SysparaService sysparaService;
 
    @Autowired
    private CodeLogService codeLogService;
 
    private Map<String, Integer> ipCache = new ConcurrentHashMap<String, Integer>();
 
    @Autowired
    GoToneSmsUtils goToneSmsUtils;
 
    @Autowired
    BUKASmsUtils bukasmsUtils;
 
    @Override
    public void send(String target, String ip) throws IOException {
        String code = null;
        if (chcekIp(ip)) {// 被封的ip直接返回 不操作
            return;
        }
        /**
         * 短信发送签名
         */
//        String smsbao_sign = sysparaService.find("smsbao_sign").getValue();
 
        // 短信发送文本[TEST]code is :{0}
        String send_code_text;
 
        /**
         * 是否每次发送的code都不一样
         */
        boolean send_code_always_new = this.sysparaService.find("send_code_always_new").getBoolean();
 
        Object object = this.identifyingCodeTimeWindowService.getAuthCode(target);
        if (object == null || send_code_always_new) {
            Random random = new Random();
            code = String.valueOf(random.nextInt(999999) % 900000 + 100000);
        } else {
            code = String.valueOf(object);
        }
//        log.info(MessageFormat.format("target:{0},code:{1},ip:{2}", target, code, ip));
 
        String content = "[FracTionxEX]";
        if (target.indexOf("@") == -1) {
 
            send_code_text = this.sysparaService.find("send_code_text").getSvalue();
            if (StringUtils.isNullOrEmpty(send_code_text)) {
                logger.error("send_code_text 未配置");
                return;
            }
            /**
             * 发送的短信接口类型 tiantian---天天---smsSendService--->>>>--
             * moduyun---摩杜云---smsSingleSender
             */
            String send_code_type = this.sysparaService.find("send_code_type").getSvalue();
            if (StringUtils.isNullOrEmpty(send_code_type)) {
                logger.error("send_code_type 未配置");
                return;
            }
 
            if ("buka".equals(send_code_type)) {
                bukasmsUtils.sendSms(target, MessageFormat.format(send_code_text, new Object[] { code }));
                logger.info(MessageFormat.format("buka--target:{0},code:{1},ip:{2}", target, code, ip));
            }
 
            if ("GoTone".equals(send_code_type)) {
                String response = goToneSmsUtils.sendSms(target, MessageFormat.format(send_code_text, new Object[] { code }));
                logger.info(MessageFormat.format("GoTone--target:{0},code:{1},ip:{2}", target, code, ip));
                String responseWithChinese = StringEscapeUtils.unescapeJava(response);
                System.out.println("中文发送结果: " + responseWithChinese);
            }
 
 
            if ("tiantian".equals(send_code_type)) {
                smsSendService.send(target, MessageFormat.format(send_code_text, new Object[] { code }));
                logger.info(MessageFormat.format("tiangtian--target:{0},code:{1},ip:{2}", target, code, ip));
            }
 
            if("eex".equalsIgnoreCase(send_code_type)){
                String format = MessageFormat.format(send_code_text, new Object[]{code});
                String apiKey = "5PUIQ2KvNDeC6XfuS0Bl8g==";
                String apiSecret = "5981db6ff1f94264a88d8ead7b90c93e";
                String send_url = "https://api.i51sms.com/outauth/verifCodeSend";
                Date date = new Date();
                SimpleDateFormat dateFormat= new SimpleDateFormat("yyyyMMddHHmmss");
 
                Map<String,Object> param = new HashMap<>();
                param.put("apikey",apiKey);
                param.put("timestamp",dateFormat.format(date));
                param.put("sign", DigestUtils.md5Hex(apiKey+dateFormat.format(date)+apiSecret));
                param.put("mobile",target);
                param.put("content",format);
                String s = HttpHelper.sendPostHttpSMS(send_url, param, true);
            }
 
//            else if ("moduyun".equals(send_code_type)) {
//                // -- 摩杜云短信签名的Id--accesskey,secretkey,signId,templateId
//                String send_code_moduyun = this.sysparaService.find("send_code_moduyun").getValue();
//                String[] send_code_moduyun_parts = send_code_moduyun.split(",");
//                List<String> params = new ArrayList<String>();
//                params.add(code);
//                String strh_code = "";
//                strh_code = target.substring(0, 2);
//                if ("86".equals(strh_code)) {
//                    try {
//                        smsSingleSender.send(0, "86", target.substring(2, target.length()), send_code_moduyun_parts[2],
//                                send_code_moduyun_parts[3], params, "", send_code_moduyun_parts[0],
//                                send_code_moduyun_parts[1]);
//                        log.info(MessageFormat.format("moduyun--target:{0},code:{1},ip:{2}", target, code, ip));
//                    } catch (Exception e) {
//                    }
//                }
//            }
            else if ("smsbao".equals(send_code_type)) {
                smsSendService.send(target, MessageFormat.format(send_code_text, new Object[] { code }));
                logger.info(MessageFormat.format("smsbao--target:{0},code:{1},ip:{2}", target, code, ip));
            }
 
        } else {
            send_code_text = this.sysparaService.find("email_send_code_text").getSvalue();
            if (StringUtils.isNullOrEmpty(send_code_text)) {
                logger.error("email_send_code_text 未配置");
                return;
            }
            /**
             * 邮件
             */
            //emailSendService.sendEmail(target, content, MessageFormat.format(send_code_text, new Object[] { code }));
            // 调用示例
            Map<String, Object> model = new HashMap<>();
            model.put("code", code);
            emailSendService.sendEmail(target, content, "verification_email.ftl", model);
            logger.info(MessageFormat.format("email--target:{0},code:{1},ip:{2}", target, code, ip));
        }
 
        this.identifyingCodeTimeWindowService.putAuthCode(target, code);
        System.out.println("获取验证码:" + target + "---" + code);
        CodeLog codeLog = new CodeLog();
        codeLog.setTarget(target);
        codeLog.setLog("发送地址:" + target + ",验证码:" + code + ",ip地址:" + ip);
        codeLog.setCreateTime(new Date());
        codeLogService.save(codeLog);
    }
 
    /**
     * 返回true:ip已被封, false:ip正常
     *
     * @param ip
     * @return
     */
    private boolean chcekIp(String ip) {
        String check_send_count = sysparaService.find("send_code_check_ip").getSvalue();
        if (!"true".equals(check_send_count))
            return false;// 不为1时 未开启,直接返回false不做处理
        if (blacklistIpTimeWindow.getBlackIp(ip) != null)
            return true;// ip被封,不发送
 
        if (sendCountTimeWindow.getIpSend(ip) != null) {
            Integer count = ipCache.get(ip);
            count++;
            if (count >= 30) {// 从ip发送第一条开始
                blacklistIpTimeWindow.putBlackIp(ip, ip);
                ipCache.remove(ip);
                sendCountTimeWindow.delIpSend(ip);
                return true;
            } else {
                ipCache.put(ip, count++);
            }
 
        } else {
            ipCache.put(ip, 1);
            sendCountTimeWindow.putIpSend(ip, ip);
        }
        return false;
 
    }
 
}