forked from kidgrow-microservices-platform

dougang
2021-04-08 6d0fee3cdc8cb213d0388ffa2e7ed7a091a1ea4c
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
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.common.utils.StringUtils;
import com.kidgrow.redis.util.RedisUtils;
import com.kidgrow.sms.util.SmsChuangLanUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
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.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
 
@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("每个手机号限用"+CHUANGLAN_SMS_NUM+"次");
                    }
                }else {
                    return ResultBody.failed();
                }
            }
            if ("0".equals(jsonObject.get("code"))) {
                redisUtils.hset(map.get("type").toString(), map.get("phone").toString(),cunMap,DateUtils.getSecondsNextEarlyMorning());
//                return ResultBody.ok().data(verificationCode);
                return ResultBody.ok();
            } 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("该手机号没有验证码");
        }
    }
 
    /**
     * 可爱高小程序发送短信
     * @param map
     * @return
     */
    @ApiOperation(value = "可爱高小程序发送短信")
    @PostMapping("/sendKagMess")
    public ResultBody sendKagMess(@RequestBody Map map){
        try {
            String phone = MapUtils.getString(map, "phone");
            if (StringUtils.isBlank(phone)) {
                return ResultBody.failed("手机号无效");
            }
            String sendContent = MapUtils.getString(map, "content");
            //region 组装发送消息的内容
            //API账号
            map.put("account", CHUANGLAN_SMS_ACCOUNT);
            //API密码
            map.put("password", CHUANGLAN_SMS_PASSWORD);
            map.put("msg", sendContent);
            //endregion
 
            String send = SmsChuangLanUtils.send(map, CHUANGLAN_SMS_SENDURL);
            JSONObject jsonObject = JSON.parseObject(send);
            if ("0".equals(jsonObject.get("code"))) {
                return ResultBody.ok().msg("发送短信成功");
            }
        }catch (Exception ex){
            return ResultBody.failed().msg("发送短信错误");
        }
        return ResultBody.failed().msg("发送短信失败");
    }
 
    //获取 存放 Redis的时间 秒(到今晚的秒数)
    public Long getSecondsNextEarlyMorning() {
        Date afterDay = DateUtils.getAfterDay(new Date());
        String s = DateUtils.formatDate(afterDay, null);
        Date date = DateUtils.parseDate(s);
        System.out.println(date.getTime()-System.currentTimeMillis());
        long second= (date.getTime()-System.currentTimeMillis())/1000;
        return second;
    }
}