This commit is contained in:
sxu 2023-12-09 11:24:41 +08:00
commit b576d16c85
7 changed files with 34 additions and 16 deletions

View File

@ -126,13 +126,15 @@ public class SysLoginService
{
checkCode(phonenumber,code);
if (StringUtils.isAnyBlank(phonenumber,code,username, password))
{
throw new ServiceException("用户/密码必须填写");
}
checkCode(phonenumber,code);
String mobileRegEx = "^1[3,4,5,6,7,8,9][0-9]{9}$";
Pattern pattern = Pattern.compile(mobileRegEx);
Matcher matcher = pattern.matcher(phonenumber);

View File

@ -12,16 +12,18 @@ spring:
active: zlpt_cloud_dev
cloud:
nacos:
username: nacos
password: Bonus@admin123!
# username: nacos
# password: Bonus@admin123!
discovery:
# 服务注册地址
server-addr: 192.168.1.3:8848
# namespace: zlpt_cloud_dev
# server-addr: 192.168.1.3:8848
server-addr: 10.40.92.74:8848
namespace: zlpt_cloud_dev
config:
# 配置中心地址
server-addr: 192.168.1.3:8848
# namespace: zlpt_cloud_dev
# server-addr: 192.168.1.3:8848
server-addr: 10.40.92.74:8848
namespace: zlpt_cloud_dev
# 配置文件格式
file-extension: yml
# 共享配置

View File

@ -13,6 +13,8 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* Description: 腾讯云短信配置
*
@ -39,7 +41,7 @@ public class SmsComponent {
private String signName;
@Value("${tencent.sms.templateId}")
private String templateCodeId;
private Map<Integer,String> templateCodeIds;
@Value("${tencent.sms.timeout}")
private Integer timeout;
@ -92,11 +94,11 @@ public class SmsComponent {
* @param code 验证码
* @param param2 分钟参数可为空
*/
public void sendCode(String phone, String code, String param2) {
public void sendCode(String phone, String code, String param2,Integer type) {
// 返回的resp是一个SendSmsResponse的实例与请求对象对应
SendSmsResponse resp;
try {
resp = getClient().SendSms(getReqTwo(phone, code,param2 ,templateCodeId)); // 模板id是自己设置好的
resp = getClient().SendSms(getReqTwo(phone, code,param2 ,templateCodeIds.get(type))); // 模板id是自己设置好的
log.info(SendSmsResponse.toJsonString(resp)); // 把返回信息输入到日志中
} catch (TencentCloudSDKException e) {
log.error("腾讯云短信发送失败");

View File

@ -24,9 +24,9 @@ public class SmsSendController extends BaseController {
private ISmsService smsService;
@PostMapping("send")
public AjaxResult send(@RequestParam("phone") String phone){
public AjaxResult send(@RequestParam("phone") String phone,@RequestParam("type") Integer type){
try {
String msg = smsService.sendCode(phone, 5 * 60 * 1000);
String msg = smsService.sendCode(phone, 5 * 60 * 1000,type);
return success(msg);
} catch (Exception e) {
return error(e.getMessage());

View File

@ -14,7 +14,7 @@ public interface ISmsService {
* @param leastTime 短信有效时间
* @return 结果
*/
public String sendCode( String phone,int leastTime);
public String sendCode(String phone,int leastTime,Integer type);
/**
* 校验验证码

View File

@ -11,6 +11,8 @@ import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Description: 短信发送接口实现
@ -35,11 +37,19 @@ public class ISmsServiceImpl implements ISmsService {
* @throws ServiceException 异常信息
*/
@Override
public String sendCode(String phone, int leastTime) {
public String sendCode(String phone, int leastTime,Integer type) {
if (phone == null || phone.isEmpty()) {
throw new ServiceException("手机号为空");
}
String mobileRegEx = "^1[3,4,5,6,7,8,9][0-9]{9}$";
Pattern pattern = Pattern.compile(mobileRegEx);
Matcher matcher = pattern.matcher(phone);
if (!matcher.matches()) {
throw new ServiceException("手机号格式错误");
}
//从redis获取有效时间 如果相差小于1分钟不可以重新发送
long expire = redisCache.getExpire(RedisConstants.REDIS_PHONE_CODE_KEY + phone);
@ -51,7 +61,7 @@ public class ISmsServiceImpl implements ISmsService {
String code = getSixBitCode(); // 生成新的验证码
//存储 phone -> code到Redis中
redisCache.setCacheObject(RedisConstants.REDIS_PHONE_CODE_KEY + phone, code, Long.parseLong(leastTime+""), TimeUnit.MILLISECONDS);
smsComponent.sendCode(phone, code,"");
smsComponent.sendCode(phone, code,"",type);
return "已发送验证码 " + phone;
}

View File

@ -13,7 +13,9 @@ tencent:
# 短信签名内容
signName: 作业智慧管控系统小程序
# 正文模板ID
templateId: 2014173
templateCodeIds:
1: 2014173
2: 2014506
# 超时时间
timeout: 1000