forked from kidgrow-microservices-platform

zhaoxiaohao
2020-05-22 f21c78ae0e3c410c6ba5be77277b5b491aca3af1
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
package com.kidgrow.sms.lanchuang.controller;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kidgrow.common.model.ResultBody;
import com.kidgrow.common.utils.DateUtils;
import com.kidgrow.redis.util.RedisUtils;
import com.kidgrow.sms.util.SmsChuangLanUtils;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.*;
 
@RestController
@RequestMapping("smsChangLan")
@Api(tags = "创蓝短信模块")
public class SmsChuangLanController {
    @Value("${chuanglan.sms.sendUrl}")
    private String CHUANGLAN_SMS_SENDURL;
    @Value("${chuanglan.sms.account}")
    private String CHUANGLAN_SMS_ACCOUNT;
    @Value("${chuanglan.sms.password}")
    private String CHUANGLAN_SMS_PASSWORD;
    @Value("${chuanglan.sms.num}")
    private Integer CHUANGLAN_SMS_NUM;
    @Value("${chuanglan.sms.timeLimit}")
    private Integer CHUANGLAN_SMS_TIMELIMIT;
    @Autowired
    private RedisUtils redisUtils;
 
    /**
     * type  为类型, "type":"LOGIN_SMS"  为登录,"type":"PASSWORD_SMS" 为密码找回,"type":"REGISTER_SMS" 为
     * @param map
     * @return
     */
    @PostMapping("send")
    public ResultBody sendVerificationCode(@RequestBody Map map) {
        if(CHUANGLAN_SMS_NUM==null){
            CHUANGLAN_SMS_NUM=5;
        }
        Object phone = map.get("phone");
        if (map.get("phone")==null||"".equals(map.get("phone"))){
            return ResultBody.failed("请输入手机号");
        }
        Object type = map.get("type");
        if (map.get("type")==null||"".equals(map.get("type"))){
            return ResultBody.failed("请输入保存的类型");
        }
        String verificationCode="";
        Random random=new Random();
        for (int i = 0; i < 6; i++) {
            int i1 = random.nextInt(9);
            verificationCode+=i1;
        }
        try {
            //组装发送消息的内容
            map.put("account", CHUANGLAN_SMS_ACCOUNT);//API账号
            map.put("password", CHUANGLAN_SMS_PASSWORD);//API密码
            map.put("report","false");
            map.put("msg","您好,您的验证码是"+verificationCode);
            //在Redis中获取
            Object hget = redisUtils.hget(map.get("type").toString(), map.get("phone").toString());
            JSONObject jsonObject =new JSONObject();
            Map<String,Object> cunMap=new HashMap<>();
            cunMap.put("verificationCode",verificationCode);
            cunMap.put("count",CHUANGLAN_SMS_NUM);//总共
            if(hget==null){
                String send = SmsChuangLanUtils.send(map, CHUANGLAN_SMS_SENDURL);
                jsonObject=JSON.parseObject(send);
                String s1 = DateUtils.formatCurrentDateTime();
                cunMap.put("date",s1);
                cunMap.put("remnant",CHUANGLAN_SMS_NUM-1);//剩余
                Date date2 = DateUtils.addMilliseconds(DateUtils.parseDate(s1), CHUANGLAN_SMS_TIMELIMIT);
                cunMap.put("endTime",DateUtils.formatDateTime(date2));
            }else {
                JSONObject redisJson = JSON.parseObject(JSON.toJSONString(hget));
                Object remnantObject = redisJson.get("remnant");
                if(remnantObject instanceof Integer){
                    Integer remnant=(Integer)remnantObject;
                    if(remnant>0){
                        String send = SmsChuangLanUtils.send(map, CHUANGLAN_SMS_SENDURL);
                        jsonObject=JSON.parseObject(send);
                        String s1 = DateUtils.formatCurrentDateTime();
                        cunMap.put("date",DateUtils.formatCurrentDateTime());
                        cunMap.put("remnant",remnant-1);//剩余
                        Date date2 = DateUtils.addMilliseconds(DateUtils.parseDate(s1), CHUANGLAN_SMS_TIMELIMIT);
                        cunMap.put("endTime",DateUtils.formatDateTime(date2));
                    }else{
                        return ResultBody.failed().data("每个手机号限用5次");
                    }
                }else {
                    return ResultBody.failed();
                }
            }
            if ("0".equals(jsonObject.get("code"))) {
                redisUtils.hset(map.get("type").toString(), map.get("phone").toString(),cunMap,getSecondsNextEarlyMorning());
                return ResultBody.ok().data(verificationCode);
            } else {
                return ResultBody.failed();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBody.failed();
        }
    }
    @PostMapping("verification")
    public ResultBody verification(@RequestBody Map<String,Object> map){
        Object phone = map.get("phone");
        if (map.get("phone")==null||"".equals(map.get("phone"))){
            return ResultBody.failed("请输入手机号");
        }
        Object verificationCode = map.get("verificationCode");
        if (map.get("verificationCode")==null||"".equals(map.get("verificationCode"))){
            return ResultBody.failed("请输入保存的类型");
        }
        Object type = map.get("type");
        if (map.get("type")==null||"".equals(map.get("type"))){
            return ResultBody.failed("请输入保存的类型");
        }
        Object hget = redisUtils.hget(map.get("type").toString(), map.get("phone").toString());
        if(hget!=null){
            JSONObject redisJson = JSON.parseObject(JSON.toJSONString(hget));
            Object verificationCodeObject = redisJson.get("verificationCode");
            if(verificationCode.equals(verificationCodeObject)){
                Object date = redisJson.get("date");
                long time = DateUtils.addMilliseconds(DateUtils.parseDate(date.toString()), CHUANGLAN_SMS_TIMELIMIT).getTime();
                Date dateNow=new Date();
                long timeNow = dateNow.getTime();
                if(timeNow<=time){
                    return ResultBody.ok();
                }else {
                    return ResultBody.failed("验证码超时");
                }
            }else {
                return ResultBody.failed("验证码错误");
            }
        }else {
            return ResultBody.failed("该手机号没有验证码");
        }
    }
    //获取 存放 Redis的时间
    public Long getSecondsNextEarlyMorning() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, 1);
        // 改成这样就好了
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
    }
}