|
@@ -0,0 +1,157 @@
|
|
1
|
+package com.huiju.estateagents.common.smsService;
|
|
2
|
+
|
|
3
|
+import com.alibaba.fastjson.JSONObject;
|
|
4
|
+import lombok.Data;
|
|
5
|
+import lombok.experimental.Accessors;
|
|
6
|
+import lombok.extern.slf4j.Slf4j;
|
|
7
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
8
|
+import org.springframework.beans.factory.annotation.Value;
|
|
9
|
+import org.springframework.scheduling.annotation.EnableScheduling;
|
|
10
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
11
|
+import org.springframework.stereotype.Component;
|
|
12
|
+
|
|
13
|
+import java.time.LocalDateTime;
|
|
14
|
+import java.util.HashMap;
|
|
15
|
+import java.util.Hashtable;
|
|
16
|
+import java.util.Map;
|
|
17
|
+import java.util.Random;
|
|
18
|
+
|
|
19
|
+@Slf4j
|
|
20
|
+@Component
|
|
21
|
+@EnableScheduling
|
|
22
|
+public class Captcha {
|
|
23
|
+ @Value("${sms-service.captcha.code}")
|
|
24
|
+ String code;
|
|
25
|
+
|
|
26
|
+ @Value("${sms-service.captcha.product}")
|
|
27
|
+ String product;
|
|
28
|
+
|
|
29
|
+ @Value("${sms-service.captcha.sign}")
|
|
30
|
+ String sign;
|
|
31
|
+
|
|
32
|
+ @Autowired
|
|
33
|
+ Service service;
|
|
34
|
+
|
|
35
|
+ // 默认超时时间, 单位秒
|
|
36
|
+ Integer expireSec = 5 * 60;
|
|
37
|
+
|
|
38
|
+ // 缓存电话列表
|
|
39
|
+ Hashtable<String, Phone> allPhones = new Hashtable<>();
|
|
40
|
+
|
|
41
|
+ /**
|
|
42
|
+ * 发送验证码
|
|
43
|
+ * @param tel
|
|
44
|
+ * @param captcha
|
|
45
|
+ * @throws Exception
|
|
46
|
+ */
|
|
47
|
+ public void send(String tel, String captcha) throws Exception {
|
|
48
|
+ if (null == captcha) {
|
|
49
|
+ throw new Exception("没有有效的验证码");
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ Map<String, String> content = new HashMap<>();
|
|
53
|
+ content.put("code", captcha);
|
|
54
|
+ content.put("product", product);
|
|
55
|
+
|
|
56
|
+ Phone phone = new Phone()
|
|
57
|
+ .setNumber(tel)
|
|
58
|
+ .setCaptcha(captcha)
|
|
59
|
+ .setExpire(expireTime());
|
|
60
|
+
|
|
61
|
+ Service.Message message = new Service.Message()
|
|
62
|
+ .setCode(code)
|
|
63
|
+ .setSign(sign)
|
|
64
|
+ .setTel(tel)
|
|
65
|
+ .setContent(JSONObject.toJSONString(content));
|
|
66
|
+
|
|
67
|
+ try {
|
|
68
|
+ service.sendMessage(message);
|
|
69
|
+ } catch (Exception e) {
|
|
70
|
+ // 修改友好点的错误
|
|
71
|
+ throw new Exception("发送验证码失败");
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ log.info(String.format("发送验证码 %s 到手机 %s", captcha, tel));
|
|
75
|
+
|
|
76
|
+ // 发送成功, 写入缓存
|
|
77
|
+ toCache(phone);
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ /**
|
|
81
|
+ * 校验验证码
|
|
82
|
+ * @param tel
|
|
83
|
+ * @param captcha
|
|
84
|
+ * @return
|
|
85
|
+ */
|
|
86
|
+ public boolean validate(String tel, String captcha) {
|
|
87
|
+ Phone phone = fromCache(tel);
|
|
88
|
+ if (null == phone) {
|
|
89
|
+ return false;
|
|
90
|
+ }
|
|
91
|
+
|
|
92
|
+ if (null == captcha || !captcha.equals(phone.getCaptcha())) {
|
|
93
|
+ return false;
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ // 过期则清除缓存
|
|
97
|
+ boolean res = checkExpire(phone);
|
|
98
|
+ if (res) {
|
|
99
|
+ clearCache(phone);
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ return !res;
|
|
103
|
+ }
|
|
104
|
+
|
|
105
|
+ /**
|
|
106
|
+ * 辅助方法: 随机生成验证码
|
|
107
|
+ * @param length
|
|
108
|
+ * @return
|
|
109
|
+ */
|
|
110
|
+ public String randCaptcha(int length) {
|
|
111
|
+ Random random = new Random();
|
|
112
|
+ String res = "";
|
|
113
|
+ for (int i = 0; i < length; i ++) {
|
|
114
|
+ int num = random.nextInt(10);
|
|
115
|
+ res += String.valueOf(num);
|
|
116
|
+ }
|
|
117
|
+ return res;
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ /**
|
|
121
|
+ * 定时任务 每半小时 清除一次不用的手机号
|
|
122
|
+ */
|
|
123
|
+ @Scheduled(fixedRate = 1000 * 60 * 30)
|
|
124
|
+ public void clearUnused() {
|
|
125
|
+ for (Phone phone: allPhones.values()) {
|
|
126
|
+ if (checkExpire(phone)) {
|
|
127
|
+ clearCache(phone);
|
|
128
|
+ }
|
|
129
|
+ }
|
|
130
|
+ }
|
|
131
|
+
|
|
132
|
+ private Phone fromCache(String phone) {
|
|
133
|
+ return allPhones.get(phone);
|
|
134
|
+ }
|
|
135
|
+ private void toCache(Phone phone) {
|
|
136
|
+ allPhones.put(phone.getNumber(), phone);
|
|
137
|
+ }
|
|
138
|
+ private void clearCache(Phone phone) { allPhones.remove(phone); }
|
|
139
|
+
|
|
140
|
+ private LocalDateTime expireTime() {
|
|
141
|
+ return LocalDateTime.now().plusSeconds(expireSec);
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ // 过期返回 true
|
|
145
|
+ private boolean checkExpire(Phone phone) {
|
|
146
|
+ return LocalDateTime.now().isAfter(phone.getExpire());
|
|
147
|
+ }
|
|
148
|
+
|
|
149
|
+ @Data
|
|
150
|
+ @Accessors(chain = true)
|
|
151
|
+ public static class Phone {
|
|
152
|
+ String number;
|
|
153
|
+ String captcha;
|
|
154
|
+ LocalDateTime expire;
|
|
155
|
+ }
|
|
156
|
+
|
|
157
|
+}
|