Commit 960c7164 authored by 王亮's avatar 王亮

temp commit(SMS).

parent f7456829
......@@ -2,6 +2,7 @@ package cn.quantgroup.xyqb.controller.req.v2;
import lombok.Data;
import javax.validation.constraints.Max;
import javax.validation.constraints.Size;
@Data
......@@ -9,6 +10,7 @@ public class SMSReq {
/**
* 0:是短信,1是语音,默认值是0
*/
@Max(1)
private Integer type = 0;
/**
......
......@@ -2,6 +2,8 @@ package cn.quantgroup.xyqb.controller.v2;
import cn.quantgroup.xyqb.controller.req.v2.SMSReq;
import cn.quantgroup.xyqb.controller.req.v2.SMSVerifyReq;
import cn.quantgroup.xyqb.exception.BizException;
import cn.quantgroup.xyqb.exception.BizExceptionEnum;
import cn.quantgroup.xyqb.model.JsonResult;
import cn.quantgroup.xyqb.model.SMSCodeBean;
import cn.quantgroup.xyqb.service.v2.VerificationCodeContext;
......@@ -10,6 +12,8 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
@RestController
@RequestMapping("/v2/sms")
public class SMSV2controller {
......@@ -22,23 +26,32 @@ public class SMSV2controller {
/**
* 统一获取短信验证码
*
* @return JsonResult<SMSCodeBean>
* @see <a href="http://yapi.quantgroups.com/project/17/interface/api/65709">统一获取短信验证码</a>
* @see <a href="http://yapi.quantgroups.com/project/17/interface/api/65709">统一获取短信验证码</a>
*/
@PostMapping("/code")
public JsonResult<SMSCodeBean> getCode(@RequestBody SMSReq smsReq){
public JsonResult<SMSCodeBean> getCode(@Valid @RequestBody SMSReq smsReq) {
if (smsReq.getCodeLength() != null && (smsReq.getCodeLength() < 4 || smsReq.getCodeLength() > 10)) {
throw new BizException(BizExceptionEnum.ERROR_PARAM, "密码长度只能在4到10位之间");
}
if(smsReq.getSceneType()!=null &&(smsReq.getSceneType()<0||smsReq.getSceneType()>2)){
throw new BizException(BizExceptionEnum.ERROR_PARAM, "请输入有效的场景值");
}
return JsonResult.buildSuccessResultGeneric(verificationCodeContext.send(smsReq));
}
/**
* 统一验证短信验证码
*
* @return JsonResult<Boolean>
* @see <a href="http://yapi.quantgroups.com/project/17/interface/api/65829">...</a>
*/
@PostMapping("/verify")
public JsonResult<Boolean> verify(@RequestBody SMSVerifyReq smsVerifyReq){
public JsonResult<Boolean> verify(@Valid @RequestBody SMSVerifyReq smsVerifyReq) {
return JsonResult.buildSuccessResultGeneric(verificationCodeContext.verify(smsVerifyReq));
return JsonResult.buildSuccessResultGeneric(verificationCodeContext.verify(smsVerifyReq));
}
......
......@@ -109,17 +109,14 @@ public class SmsServiceImpl implements ISmsService {
// return true;
// }
SessionStruct sessionStruct = XyqbSessionContextHolder.getXSession();
String key;
String key = Constants.REDIS_PREFIX_VERIFICATION_CODE + smsVerifyReq.getPhoneNo();
if (sessionStruct == null) {
throw new BizException(BizExceptionEnum.UN_EXIT_TENANT_ID);
}
if (UserConstant.defaultTenantId.equals(sessionStruct.getTenantId())) {
key = Constants.REDIS_PREFIX_VERIFICATION_CODE + smsVerifyReq.getPhoneNo();
} else {
if (!UserConstant.defaultTenantId.equals(sessionStruct.getTenantId())) {
key = Constants.REDIS_PREFIX_VERIFICATION_CODE + sessionStruct.getTenantId() + "_" + smsVerifyReq.getPhoneNo();
}
String randomCode = stringRedisTemplate.opsForValue().get(key);
if (StringUtils.isBlank(randomCode)) {
return false;
......
......@@ -93,20 +93,38 @@ public class SMSVerificationCodeStrategy implements VerificationCodeStrategy {
private boolean sendVerificationCode2New(String phoneNo, String randomCode, String smsMerchant) {
SessionStruct sessionStruct = XyqbSessionContextHolder.getXSession();
String clientIp = sessionStruct.getIp();
String getPhoneVerificationCountKey = Constants.REDIS_SMS_CODE_COUNT;
if (!UserConstant.defaultTenantId.equals(sessionStruct.getTenantId())) {
getPhoneVerificationCountKey = Constants.REDIS_SMS_CODE_COUNT + sessionStruct.getTenantId() + ":";
}
// 手机号计数器
Long getPhoneVerificationCount = redisTemplate.opsForHash().increment(Constants.REDIS_SMS_CODE_COUNT, phoneNo, 1);
redisTemplate.expire(Constants.REDIS_SMS_CODE_COUNT, DateUtils.getSeconds(), TimeUnit.SECONDS);
Long getPhoneVerificationCount = redisTemplate.opsForHash().increment(getPhoneVerificationCountKey, phoneNo, 1);
redisTemplate.expire(getPhoneVerificationCountKey, DateUtils.getSeconds(), TimeUnit.SECONDS);
// 设备号计数器
String getDeviceVerificationCountKey = Constants.REDIS_SMS_DEVICE_COUNT;
if (!UserConstant.defaultTenantId.equals(sessionStruct.getTenantId())) {
getDeviceVerificationCountKey = Constants.REDIS_SMS_DEVICE_COUNT + sessionStruct.getTenantId() + ":";
}
Long getDeviceVerificationCount = 0L;
if (StringUtils.isNotBlank(sessionStruct.getScDeviceId())) {
getDeviceVerificationCount = redisTemplate.opsForHash().increment(Constants.REDIS_SMS_DEVICE_COUNT, sessionStruct.getScDeviceId(), 1);
redisTemplate.expire(Constants.REDIS_SMS_DEVICE_COUNT, DateUtils.getSeconds(), TimeUnit.SECONDS);
getDeviceVerificationCount = redisTemplate.opsForHash().increment(getDeviceVerificationCountKey, sessionStruct.getScDeviceId(), 1);
redisTemplate.expire(getDeviceVerificationCountKey, DateUtils.getSeconds(), TimeUnit.SECONDS);
}
// IP计数器
String getIPVerificationCountKey = Constants.REDIS_SMS_IP_COUNT;
if (!UserConstant.defaultTenantId.equals(sessionStruct.getTenantId())) {
getIPVerificationCountKey = Constants.REDIS_SMS_IP_COUNT + sessionStruct.getTenantId() + ":";
}
Long getIPVerificationCount = 0L;
if (StringUtils.isNotBlank(clientIp)) {
getIPVerificationCount = redisTemplate.opsForHash().increment(Constants.REDIS_SMS_IP_COUNT, clientIp, 1);
redisTemplate.expire(Constants.REDIS_SMS_IP_COUNT, DateUtils.getSeconds(), TimeUnit.SECONDS);
getIPVerificationCount = redisTemplate.opsForHash().increment(getIPVerificationCountKey, clientIp, 1);
redisTemplate.expire(getIPVerificationCountKey, DateUtils.getSeconds(), TimeUnit.SECONDS);
}
// 手机号上限检查
if (getPhoneVerificationCount > PHONE_MAX_PER_DAY) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment