短信验证

This commit is contained in:
huangdeliang
2021-06-09 19:12:48 +08:00
parent 02e5110d2c
commit e99ebfadb7
9 changed files with 346 additions and 193 deletions

View File

@ -0,0 +1,13 @@
package com.stdiet.custom.service;
public interface ISysRedisService {
void set(String key, String value);
String get(String key);
Boolean expire(String key, long expire);
void remove(String key);
Long increment(String key, long delta);
}

View File

@ -0,0 +1,9 @@
package com.stdiet.custom.service;
public interface ISysSmsConfirmServie {
public Integer sendSmsCode(String phone);
public Integer checkSmsCode(String phone, String code);
}

View File

@ -0,0 +1,40 @@
package com.stdiet.custom.service.impl;
import com.stdiet.custom.service.ISysRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
@Service
public class SysRedisServiceImp implements ISysRedisService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public void set(String key, String value) {
stringRedisTemplate.opsForValue().set(key, value);
}
@Override
public String get(String key) {
return stringRedisTemplate.opsForValue().get(key);
}
@Override
public Boolean expire(String key, long expire) {
return stringRedisTemplate.expire(key, expire, TimeUnit.SECONDS);
}
@Override
public void remove(String key) {
stringRedisTemplate.delete(key);
}
@Override
public Long increment(String key, long delta) {
return stringRedisTemplate.opsForValue().increment(key, delta);
}
}

View File

@ -0,0 +1,87 @@
package com.stdiet.custom.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.stdiet.common.utils.DateUtils;
import com.stdiet.common.utils.StringUtils;
import com.stdiet.custom.domain.wechat.WxSubscribePostLog;
import com.stdiet.custom.service.ISysRedisService;
import com.stdiet.custom.service.ISysSmsConfirmServie;
import com.stdiet.custom.service.IWxSubscribePostLogService;
import com.stdiet.custom.utils.SmsUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SysSmsConfirmServiceImpl implements ISysSmsConfirmServie {
private static long CODE_EXPIRE_SECONDS = 600; //设置验证码过期时间为600秒
@Autowired
ISysRedisService redisService;
@Autowired
IWxSubscribePostLogService wxSubscribePostLogService;
@Override
public Integer sendSmsCode(String phone) {
int reCode = -1;
try {
String code = RandomStringUtils.randomNumeric(6);
JSONObject paramObj = new JSONObject();
paramObj.put("code", code);
SendSmsResponse response = SmsUtils.sendSms(phone, paramObj.toJSONString(), SmsUtils.SMS_217025172);
if (response.getCode().equals("OK")) {
// 发送成功
redisService.remove(phone);
redisService.set(phone, code);
redisService.expire(phone, CODE_EXPIRE_SECONDS);
reCode = 0;
} else if (response.getCode().equals("isv.MOBILE_NUMBER_ILLEGAL")) {
// 非法手机号
reCode = 1;
}
WxSubscribePostLog postLog = new WxSubscribePostLog();
postLog.setPhone(phone);
JSONObject resultObj = new JSONObject();
resultObj.put("requestId", response.getRequestId());
resultObj.put("bizId", response.getBizId());
resultObj.put("code", response.getCode());
resultObj.put("message", response.getMessage());
postLog.setResult(resultObj);
paramObj.put("phone", phone);
paramObj.put("tmpCode", SmsUtils.SMS_217025172);
paramObj.put("signName", SmsUtils.SMS_SIGN_NAME);
postLog.setData(paramObj);
postLog.setSendTime(DateUtils.getNowDate());
postLog.setType(2);
wxSubscribePostLogService.insertWxSubscribePostLog(postLog);
} catch (ClientException e) {
e.printStackTrace();
}
return reCode;
}
@Override
public Integer checkSmsCode(String phone, String code) {
String cachedCode = redisService.get(phone);
int resCode = -1;
if (StringUtils.isEmpty(cachedCode)) {
// 验证码失效
resCode = 1;
redisService.remove(phone);
} else if (!code.equals(cachedCode)) {
// 验证码错误
resCode = 2;
} else if (code.equals(cachedCode)) {
// 校验成功
resCode = 0;
redisService.remove(phone);
}
return resCode;
}
}

View File

@ -29,9 +29,6 @@ import java.util.concurrent.TimeUnit;
public class WeChartAppletServiceImp implements IWechatAppletService {
static final String WX_TEM_ID = "Ow0j0Jt4OJhjy6GruBstOMLTGjAVagM4hTZRLAaxqJo";
static final String SMS_TEM_ID = "SMS_216839183";
static final String SMS_SIGN_NAME = "胜唐体控";
@Autowired
private RedisCache redisCache;
@ -128,7 +125,9 @@ public class WeChartAppletServiceImp implements IWechatAppletService {
public Integer postSms(Long cusId, Long planId, String plan) {
try {
SysCustomer customer = sysCustomerService.selectSysCustomerById(cusId);
SendSmsResponse response = SmsUtils.sendSms(customer.getPhone(), plan, SMS_TEM_ID, SMS_SIGN_NAME);
JSONObject paramObj = new JSONObject();
paramObj.put("plan", plan);
SendSmsResponse response = SmsUtils.sendSms(customer.getPhone(), paramObj.toJSONString(), SmsUtils.SMS_216839183, SmsUtils.SMS_SIGN_NAME);
WxSubscribePostLog postLog = new WxSubscribePostLog();
postLog.setPhone(customer.getPhone());
@ -142,8 +141,8 @@ public class WeChartAppletServiceImp implements IWechatAppletService {
JSONObject dataParam = new JSONObject();
dataParam.put("phone", customer.getPhone());
dataParam.put("plan", plan);
dataParam.put("tmpCode", SMS_TEM_ID);
dataParam.put("signName", SMS_SIGN_NAME);
dataParam.put("tmpCode", SmsUtils.SMS_216839183);
dataParam.put("signName", SmsUtils.SMS_SIGN_NAME);
postLog.setData(dataParam);
postLog.setSendTime(DateUtils.getNowDate());
postLog.setType(1);

View File

@ -1,6 +1,5 @@
package com.stdiet.custom.utils;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
@ -16,12 +15,42 @@ import java.text.SimpleDateFormat;
import java.util.Date;
public class SmsUtils {
/**
* 身份验证码
*/
public static final String SMS_217025173 = "SMS_217025173";
/**
* 登录确认验证码
*/
public static final String SMS_217025172 = "SMS_217025172";
/**
* 用户注册验证码
*/
public static final String SMS_217025170 = "SMS_217025170";
/**
* 修改密码验证码
*/
public static final String SMS_217025169 = "SMS_217025169";
/**
* 新食谱通知
*/
public static final String SMS_216839183 = "SMS_216839183";
/**
* 签名
*/
public static final String SMS_SIGN_NAME = "胜唐体控";
//产品名称:云通信短信API产品,开发者无需替换
static final String product = "Dysmsapi";
//产品域名,开发者无需替换
static final String domain = "dysmsapi.aliyuncs.com";
public static SendSmsResponse sendSms(String phone, String plan, String tmpCode, String signName) throws ClientException {
public static SendSmsResponse sendSms(String phone, String paramStr, String tmpCode) throws ClientException {
return sendSms(phone, paramStr, tmpCode, SMS_SIGN_NAME);
}
public static SendSmsResponse sendSms(String phone, String paramStr, String tmpCode, String signName) throws ClientException {
//可自助调整超时时间
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
@ -41,9 +70,7 @@ public class SmsUtils {
//必填:短信模板-可在短信控制台中找到
request.setTemplateCode(tmpCode);
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
JSONObject paramObj = new JSONObject();
paramObj.put("plan", plan);
request.setTemplateParam(paramObj.toJSONString());
request.setTemplateParam(paramStr);
//选填-上行短信扩展码(无特殊需求用户请忽略此字段)
//request.setSmsUpExtendCode("90997");