From e7e81651c353b4135d8d5a7601f0a037aee46646 Mon Sep 17 00:00:00 2001 From: syruan <1555146157@163.com> Date: Wed, 6 Dec 2023 15:30:56 +0800 Subject: [PATCH] =?UTF-8?q?=E8=85=BE=E8=AE=AF=E4=BA=91=E7=9F=AD=E4=BF=A1SM?= =?UTF-8?q?S=E9=85=8D=E7=BD=AE=E5=8F=8A=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/bootstrap.yml | 1 + zlpt-modules/zlpt-system/pom.xml | 9 +- .../zlpt/system/config/SmsComponent.java | 108 ++++++++++++++++++ .../system/controller/SmsSendController.java | 35 ++++++ .../zlpt/system/service/ISmsService.java | 24 ++++ .../system/service/impl/ISmsServiceImpl.java | 87 ++++++++++++++ .../src/main/resources/bootstrap.yml | 13 +++ 7 files changed, 276 insertions(+), 1 deletion(-) create mode 100644 zlpt-modules/zlpt-system/src/main/java/com/bonus/zlpt/system/config/SmsComponent.java create mode 100644 zlpt-modules/zlpt-system/src/main/java/com/bonus/zlpt/system/controller/SmsSendController.java create mode 100644 zlpt-modules/zlpt-system/src/main/java/com/bonus/zlpt/system/service/ISmsService.java create mode 100644 zlpt-modules/zlpt-system/src/main/java/com/bonus/zlpt/system/service/impl/ISmsServiceImpl.java diff --git a/zlpt-modules/zlpt-home/src/main/resources/bootstrap.yml b/zlpt-modules/zlpt-home/src/main/resources/bootstrap.yml index 434f11f..3181f3f 100644 --- a/zlpt-modules/zlpt-home/src/main/resources/bootstrap.yml +++ b/zlpt-modules/zlpt-home/src/main/resources/bootstrap.yml @@ -2,6 +2,7 @@ server: port: 9301 + # Spring spring: application: diff --git a/zlpt-modules/zlpt-system/pom.xml b/zlpt-modules/zlpt-system/pom.xml index 0e5dbff..e9a7dc1 100644 --- a/zlpt-modules/zlpt-system/pom.xml +++ b/zlpt-modules/zlpt-system/pom.xml @@ -16,7 +16,14 @@ - + + + + com.tencentcloudapi + tencentcloud-sdk-java + 3.1.918 + + com.alibaba.cloud diff --git a/zlpt-modules/zlpt-system/src/main/java/com/bonus/zlpt/system/config/SmsComponent.java b/zlpt-modules/zlpt-system/src/main/java/com/bonus/zlpt/system/config/SmsComponent.java new file mode 100644 index 0000000..38e03ee --- /dev/null +++ b/zlpt-modules/zlpt-system/src/main/java/com/bonus/zlpt/system/config/SmsComponent.java @@ -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("腾讯云短信发送失败"); + } + } + + + +} diff --git a/zlpt-modules/zlpt-system/src/main/java/com/bonus/zlpt/system/controller/SmsSendController.java b/zlpt-modules/zlpt-system/src/main/java/com/bonus/zlpt/system/controller/SmsSendController.java new file mode 100644 index 0000000..51b3fc9 --- /dev/null +++ b/zlpt-modules/zlpt-system/src/main/java/com/bonus/zlpt/system/controller/SmsSendController.java @@ -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()); + } + } + +} diff --git a/zlpt-modules/zlpt-system/src/main/java/com/bonus/zlpt/system/service/ISmsService.java b/zlpt-modules/zlpt-system/src/main/java/com/bonus/zlpt/system/service/ISmsService.java new file mode 100644 index 0000000..dd83e31 --- /dev/null +++ b/zlpt-modules/zlpt-system/src/main/java/com/bonus/zlpt/system/service/ISmsService.java @@ -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); + +} diff --git a/zlpt-modules/zlpt-system/src/main/java/com/bonus/zlpt/system/service/impl/ISmsServiceImpl.java b/zlpt-modules/zlpt-system/src/main/java/com/bonus/zlpt/system/service/impl/ISmsServiceImpl.java new file mode 100644 index 0000000..5885aa4 --- /dev/null +++ b/zlpt-modules/zlpt-system/src/main/java/com/bonus/zlpt/system/service/impl/ISmsServiceImpl.java @@ -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; + // } + } +} diff --git a/zlpt-modules/zlpt-system/src/main/resources/bootstrap.yml b/zlpt-modules/zlpt-system/src/main/resources/bootstrap.yml index 269d9af..56f4ffe 100644 --- a/zlpt-modules/zlpt-system/src/main/resources/bootstrap.yml +++ b/zlpt-modules/zlpt-system/src/main/resources/bootstrap.yml @@ -2,6 +2,19 @@ server: port: 9201 +# 腾讯云短信 +tencent: + sms: + # 腾讯云账户API访问密钥 + secretId: AKIDbF1pTyU6iHn6EH9uc9O6kJgrrfI2rV04 + keysecret: zGPRJ9z68AGZVMAoHYyDFDAxhR2Xs0Qf + # 短信应用ID + smsSdkAppId: 564559 + # 短信签名内容 + signName: 作业智慧管控系统小程序 + # 正文模板ID + templateId: 2010811 + # Spring spring: application: