admin
2020-07-14 eec7e789a87863c25d92c10ad5dfc22ad80c448d
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
package com.yeshi.fanli.controller.client.v1;
 
import java.io.PrintWriter;
 
import javax.annotation.Resource;
 
import com.yeshi.fanli.util.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.yeshi.utils.JsonUtil;
 
import com.yeshi.fanli.entity.accept.AcceptData;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.exception.config.SMSException;
import com.yeshi.fanli.service.inter.user.ForbiddenUserIdentifyCodeService;
import com.yeshi.fanli.service.inter.user.SMSService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
import com.yeshi.fanli.util.annotation.RequestSerializableByKey;
 
@Controller
@RequestMapping("api/v1/sms")
public class SMSController {
 
    @Resource
    private SMSService smsService;
 
    @Resource
    private RedisManager redisManager;
 
    @Resource
    private UserInfoService userInfoService;
 
    @Resource
    private ForbiddenUserIdentifyCodeService forbiddenUserIdentifyCodeService;
 
    // 发送短信
 
    @RequestSerializableByKey(key = "#phone+'-'+#uid+'-'+#type")
    @RequestMapping(value = "sendSMS", method = RequestMethod.POST)
    public void sendMSM(AcceptData acceptData, String phone, Long uid, Integer type, PrintWriter out) {
        if (Constant.IS_TEST) {
            out.print(JsonUtil.loadTrueResult("发送成功"));
            return;
        }
        sendMSM(acceptData, phone, uid, type, StringUtil.Md5(phone + "-" + "-" + uid + "-" + type), out);
    }
 
    public void sendMSM(AcceptData acceptData, String phone, Long uid, Integer type, String key, PrintWriter out) {
        try {
            if (phone.contains("**") && uid != null && uid > 0) {
                UserInfo userInfo = userInfoService.getUserById(uid);
                if (userInfo == null) {
                    out.print(JsonUtil.loadFalseResult(2, "用户不存在"));
                    return;
                } else if (StringUtil.isNullOrEmpty(userInfo.getPhone())) {
                    out.print(JsonUtil.loadFalseResult(3, "尚未绑定电话号码"));
                    return;
                }
                phone = userInfo.getPhone();
            }
 
            if (!StringUtil.isMobile(phone)) {
                out.print(JsonUtil.loadFalseResult(4, "电话号码格式不正确"));
                return;
            }
            smsService.sendLoginVCode(phone, 6);
 
            out.print(JsonUtil.loadTrueResult("发送成功"));
        } catch (SMSException e) {
            out.print(JsonUtil.loadFalseResult(e.getCode(), e.getMsg()));
        }
    }
 
    /**
     * 1.5.3 登录发送短信
     * 
     * @param acceptData
     * @param phone
     * @param uid
     * @param out
     */
    @RequestMapping(value = "sendMSMLogin", method = RequestMethod.POST)
    public void sendMSMLogin(AcceptData acceptData, String phone, boolean slideVerify, PrintWriter out) {
        if (phone == null || !StringUtil.isMobile(phone.replaceAll(" ", ""))) {
            out.print(JsonUtil.loadFalseResult(1, "手机号输入有误,请修改"));
            return;
        }
        phone = phone.replaceAll(" ", "");
 
        if (Constant.IS_TEST) {
            out.print(JsonUtil.loadTrueResult("发送成功"));
            return;
        }
 
        sendMSNnew(phone, slideVerify, 1, StringUtil.Md5(phone + "-" + acceptData.getDevice()), out);
    }
 
    /**
     * 1.5.3 绑定发送短信
     * 
     * @param acceptData
     * @param phone
     * @param uid
     * @param out
     */
    @RequestSerializableByKey(key = "#acceptData.device+'-'+#phone")
    @RequestMapping(value = "sendMSMBind", method = RequestMethod.POST)
    public void sendMSMBind(AcceptData acceptData, String phone, boolean slideVerify, Long uid, PrintWriter out) {
        if (phone == null || !StringUtil.isMobile(phone.replaceAll(" ", ""))) {
            out.print(JsonUtil.loadFalseResult(1, "手机号输入有误,请修改"));
            return;
        }
        phone = phone.replaceAll(" ", "");
 
        // 判断手机号码是否被封禁
        // ForbiddenUserIdentifyCode identifyCode1 =
        // forbiddenUserIdentifyCodeService
        // .listByTypeAndIdentifyCode(ForbiddenUserIdentifyCodeTypeEnum.phone,
        // phone);
        // if (identifyCode1 != null && identifyCode1.getEffective() != null &&
        // identifyCode1.getEffective()) {
        // out.print(JsonUtil.loadFalseResult(9001,"该电话号码被占用"));
        // return;
        // }
        //
        // 判断手机号码是否被封禁
        UserInfo phoneUser = userInfoService.getEffectiveUserInfoByPhone(phone, SystemInfoUtil.getSystem(acceptData.getPlatform(), acceptData.getPackages()));
        if (phoneUser != null && uid != null) {
            out.print(JsonUtil.loadFalseResult(9001, "该电话号码被占用"));
            return;
        }
 
        if (Constant.IS_TEST) {
            out.print(JsonUtil.loadTrueResult("发送成功"));
            return;
        }
 
        sendMSNnew(phone, slideVerify, 2, StringUtil.Md5(phone + "-" + acceptData.getDevice()), out);
    }
 
    public void sendMSNnew(String phone, boolean slideVerify, int type, String key, PrintWriter out) {
        try {
            int count = 0;
            String cachekey = null;
            if (type == 1) {
                // 登录验证码
                cachekey = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSLoginCount, phone + "");
            } else if (type == 2) {
                // 绑定验证码
                cachekey = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSBindCount, phone + "");
            }
 
            if (cachekey == null) {
                out.print(JsonUtil.loadFalseResult(1, "发送失败"));
                return;
            }
 
            String cacheValue = redisManager.getCommonString(cachekey);
            if (!StringUtil.isNullOrEmpty(cacheValue)) {
                count = Integer.parseInt(cacheValue);
                // 限制3次
                if (count >= 3) {
                    out.print(JsonUtil.loadFalseResult(3, "验证码次数超限,请稍后再试"));
                    return;
                }
            }
 
            if (count == 2 && !slideVerify) {
                out.print(JsonUtil.loadFalseResult(2, "需要滑动验证"));
                return;
            }
            // 缓存一个小时
            count++;
            redisManager.cacheCommonString(cachekey, count + "", 60 * 60);
 
            if (type == 1) {
                // 登录验证码
                smsService.sendLoginVCode(phone, 4);
            } else if (type == 2) {
                // 绑定验证码
                smsService.sendBindVCode(phone, 4);
            }
 
            out.print(JsonUtil.loadTrueResult("发送成功"));
 
        } catch (SMSException e) {
            out.print(JsonUtil.loadFalseResult(e.getCode(), e.getMsg()));
        } catch (Exception e) {
            out.print(JsonUtil.loadFalseResult(1, "发送失败"));
            e.printStackTrace();
        }
    }
    
    
    
    /**
     * 注销账户短信验证
     * @param acceptData
     * @param uid
     * @param phone
     * @param out
     */
    @RequestMapping(value = "sendMSMRemove")
    public void sendMSMRemove(String callback, AcceptData acceptData, Long uid, String phone, PrintWriter out) {
        if (Constant.IS_TEST) {
            JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("发送成功"));
            return;
        }
        
        if (uid == null || uid <= 0) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(1, "用户未登录"));
            return;
        }
        
        if (phone == null || !StringUtil.isMobile(phone.replaceAll(" ", ""))) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(1, "请输入正确手机号"));
            return;
        }
        phone = phone.replaceAll(" ", "").trim();
        
        UserInfo userInfo = userInfoService.selectAvailableByPrimaryKey(uid);
        if (userInfo == null) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(1, "用户不存在"));
            return;
        }
        
        if (!phone.equals(userInfo.getPhone())) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(1, "请输入正确手机号"));
            return;
        }
        
        try {
            int count = 0;
            String cachekey = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSBindRemove, phone + "");
            String cacheValue = redisManager.getCommonString(cachekey);
            if (!StringUtil.isNullOrEmpty(cacheValue)) {
                count = Integer.parseInt(cacheValue);
                // 限制3次
                if (count >= 3) {
                    JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(1, "验证码次数超限,请稍后再试"));
                    return;
                }
            }
            // 缓存一个小时
            count++;
            redisManager.cacheCommonString(cachekey, count + "", 60 * 60);
            // 发送验证码
            smsService.sendRemoveVCode(phone, 4);
            JsonUtil.printMode(out, callback, JsonUtil.loadTrueResult("发送成功"));
        } catch (SMSException e) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(e.getCode(), e.getMsg()));
        } catch (Exception e) {
            JsonUtil.printMode(out, callback, JsonUtil.loadFalseResult(1, "发送失败"));
            e.printStackTrace();
        }
    }
 
}