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;
|
}
|
}
|