腾讯云短信SMS配置及接口
This commit is contained in:
parent
96c6604543
commit
e7e81651c3
|
|
@ -2,6 +2,7 @@
|
|||
server:
|
||||
port: 9301
|
||||
|
||||
|
||||
# Spring
|
||||
spring:
|
||||
application:
|
||||
|
|
|
|||
|
|
@ -16,7 +16,14 @@
|
|||
</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
|
||||
<!-- 腾讯云短信 SMS -->
|
||||
<dependency>
|
||||
<groupId>com.tencentcloudapi</groupId>
|
||||
<artifactId>tencentcloud-sdk-java</artifactId>
|
||||
<version>3.1.918</version>
|
||||
</dependency>
|
||||
|
||||
<!-- SpringCloud Alibaba Nacos -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,108 @@
|
|||
package com.bonus.zlpt.system.config;
|
||||
|
||||
import com.tencentcloudapi.common.Credential;
|
||||
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
|
||||
import com.tencentcloudapi.common.profile.ClientProfile;
|
||||
import com.tencentcloudapi.common.profile.HttpProfile;
|
||||
import com.tencentcloudapi.sms.v20210111.SmsClient;
|
||||
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
|
||||
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
|
||||
import lombok.Data;
|
||||
import lombok.ToString;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Description: 腾讯云短信配置
|
||||
*
|
||||
* @Author 阮世耀
|
||||
* @Create 2023/12/6 14:56
|
||||
* @Version 1.0
|
||||
*/
|
||||
@ToString
|
||||
@Data
|
||||
@Component
|
||||
@Slf4j
|
||||
public class SmsComponent {
|
||||
|
||||
@Value("${sms.sdkAppId}") // 注入参数值
|
||||
private String sdkAppId;
|
||||
|
||||
@Value("${sms.secretId}")
|
||||
private String secretId;
|
||||
|
||||
@Value("${sms.secretKey}")
|
||||
private String secretKey;
|
||||
|
||||
@Value("${sms.signName}")
|
||||
private String signName;
|
||||
|
||||
@Value("${sms.templateCodeId}")
|
||||
private String templateCodeId;
|
||||
|
||||
@Value("${sms.timeout}")
|
||||
private Integer timeout;
|
||||
|
||||
/**
|
||||
* 获取SmsClient客户端
|
||||
* @return
|
||||
*/
|
||||
public SmsClient getClient() {
|
||||
|
||||
// 实例化认证对象,入参需要传入腾讯云账户secretId,secretKey
|
||||
Credential cred = new Credential(secretId, secretKey);
|
||||
// 实例化http选项
|
||||
HttpProfile httpProfile = new HttpProfile();
|
||||
httpProfile.setEndpoint("sms.tencentcloudapi.com");
|
||||
// 实例化client选项
|
||||
ClientProfile clientProfile = new ClientProfile();
|
||||
clientProfile.setHttpProfile(httpProfile);
|
||||
// 实例化要请求产品的client对象,clientProfile是可选的
|
||||
return new SmsClient(cred, "ap-beijing", clientProfile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取req请求 2个参数,可根据具体情况改变
|
||||
*
|
||||
* @param phone 手机号
|
||||
* @param code 参数1,这里是验证码,param2为参数2这里为分钟
|
||||
* @param templateId 短信模板id
|
||||
* @return 请求参数对象
|
||||
*/
|
||||
public SendSmsRequest getReqTwo(String phone, String code, String param2, String templateId) {
|
||||
SendSmsRequest req = new SendSmsRequest();
|
||||
String[] phoneNumberSet = {"+86" + phone};
|
||||
req.setSmsSdkAppId(sdkAppId); // 设置参数
|
||||
req.setPhoneNumberSet(phoneNumberSet);
|
||||
req.setSignName(signName);
|
||||
req.setTemplateId(templateId);
|
||||
//模板内容的参数有几个就设置几个,我这里是两个
|
||||
String[] templateParamSetOne = {code};
|
||||
String[] templateParamSetTwo = {code,param2};
|
||||
req.setTemplateParamSet(templateParamSetOne);
|
||||
return req; // 返回请求参数内容
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送验证码
|
||||
*
|
||||
* @param phone 手机号码
|
||||
* @param code 验证码
|
||||
* @param param2 分钟参数(可为空)
|
||||
*/
|
||||
public void sendCode(String phone, String code, String param2) {
|
||||
// 返回的resp是一个SendSmsResponse的实例,与请求对象对应
|
||||
SendSmsResponse resp;
|
||||
try {
|
||||
resp = getClient().SendSms(getReqTwo(phone, code,param2 ,templateCodeId)); // 模板id是自己设置好的
|
||||
log.info(SendSmsResponse.toJsonString(resp)); // 把返回信息输入到日志中
|
||||
} catch (TencentCloudSDKException e) {
|
||||
log.error("腾讯云短信发送失败");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
package com.bonus.zlpt.system.controller;
|
||||
|
||||
import com.bonus.zlpt.common.core.web.controller.BaseController;
|
||||
import com.bonus.zlpt.common.core.web.domain.AjaxResult;
|
||||
import com.bonus.zlpt.system.service.ISmsService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* Description: 短信发送接口
|
||||
*
|
||||
* @Author 阮世耀
|
||||
* @Create 2023/12/6 15:13
|
||||
* @Version 1.0
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/sms")
|
||||
public class SmsSendController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private ISmsService smsService;
|
||||
|
||||
@RequestMapping("/send")
|
||||
public AjaxResult send(@RequestParam("phone") String phone){
|
||||
try {
|
||||
String msg = smsService.sendCode(phone, 5 * 60 * 1000);
|
||||
return success(msg);
|
||||
} catch (Exception e) {
|
||||
return error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package com.bonus.zlpt.system.service;
|
||||
|
||||
/**
|
||||
* Description: 短信发送接口
|
||||
*
|
||||
* @Author 阮世耀
|
||||
* @Create 2023/12/6 15:04
|
||||
* @Version 1.0
|
||||
*/
|
||||
public interface ISmsService {
|
||||
|
||||
/**
|
||||
* @param phone 给手机号发送验证码
|
||||
* @param leastTime 短信有效时间
|
||||
* @return 结果
|
||||
*/
|
||||
public String sendCode( String phone,int leastTime);
|
||||
|
||||
/**
|
||||
* 校验验证码
|
||||
*/
|
||||
boolean checkCode(String phone, String code);
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,87 @@
|
|||
package com.bonus.zlpt.system.service.impl;
|
||||
|
||||
import com.bonus.zlpt.common.core.exception.ServiceException;
|
||||
import com.bonus.zlpt.system.config.SmsComponent;
|
||||
import com.bonus.zlpt.system.service.ISmsService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Description: 短信发送接口实现
|
||||
*
|
||||
* @Author 阮世耀
|
||||
* @Create 2023/12/6 15:05
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Service
|
||||
public class ISmsServiceImpl implements ISmsService {
|
||||
|
||||
@Resource
|
||||
SmsComponent smsComponent;
|
||||
|
||||
/**
|
||||
* @param phone 给手机号发送验证码
|
||||
* @param leastTime 短信有效时间
|
||||
* @return 消息
|
||||
* @throws ServiceException 异常信息
|
||||
*/
|
||||
@Override
|
||||
public String sendCode(String phone, int leastTime) {
|
||||
if (phone == null || phone.isEmpty()) {
|
||||
throw new ServiceException("手机号为空");
|
||||
}
|
||||
|
||||
// 判断是否已经发送过 (需要从Redis缓存中获取记录来进行判断,此处暂时去除)
|
||||
// if (!StringUtils.isEmpty(redisCode)) {
|
||||
// long time = Long.parseLong(redisCode.split("_")[1]);
|
||||
// if (System.currentTimeMillis() - time < leastTime) {
|
||||
// throw new ServiceException("发送频率过高");
|
||||
// }
|
||||
// }
|
||||
|
||||
String code = getSixBitCode(); // 生成新的验证码
|
||||
//存储 phone -> code到Redis中
|
||||
// redisCache.setCacheObject(Constants.HEAD + phone, code + "_" + System.currentTimeMillis(), smsComponent.getTimeout(), TimeUnit.MINUTES);
|
||||
int minute = leastTime / 60 / 1000; //分钟
|
||||
smsComponent.sendCode(phone, code, Integer.toString(minute));
|
||||
return "已发送验证码 " + phone;
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机生成6位验证码
|
||||
*/
|
||||
private String getSixBitCode() {
|
||||
//随机数
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Random rand = new Random();
|
||||
for (int i = 0; i < 6; i++) {
|
||||
sb.append(rand.nextInt(10));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 判断验证码是否存在Redis中
|
||||
*
|
||||
* @param phone 手机号码
|
||||
* @param code 验证码
|
||||
* @return 校验结果
|
||||
* @throws ServiceException 异常信息
|
||||
*/
|
||||
@Override
|
||||
public boolean checkCode(String phone, String code) {
|
||||
// String redisCode = redisCache.getCacheObject(Constants.HEAD + phone);
|
||||
// if (StringUtils.isEmpty(redisCode)) {
|
||||
// throw new ServiceException("验证码失效");
|
||||
// }
|
||||
// if (!StringUtils.equals(redisCode.split("_")[0], code)) {
|
||||
// throw new ServiceException("验证码错误");
|
||||
// } else {
|
||||
// redisCache.deleteObject(Constants.HEAD + phone);
|
||||
return true;
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,19 @@
|
|||
server:
|
||||
port: 9201
|
||||
|
||||
# 腾讯云短信
|
||||
tencent:
|
||||
sms:
|
||||
# 腾讯云账户API访问密钥
|
||||
secretId: AKIDbF1pTyU6iHn6EH9uc9O6kJgrrfI2rV04
|
||||
keysecret: zGPRJ9z68AGZVMAoHYyDFDAxhR2Xs0Qf
|
||||
# 短信应用ID
|
||||
smsSdkAppId: 564559
|
||||
# 短信签名内容
|
||||
signName: 作业智慧管控系统小程序
|
||||
# 正文模板ID
|
||||
templateId: 2010811
|
||||
|
||||
# Spring
|
||||
spring:
|
||||
application:
|
||||
|
|
|
|||
Loading…
Reference in New Issue