From 6b800ad69ea5588dd906d555060d8c1524b75544 Mon Sep 17 00:00:00 2001 From: 15856 <15856818120@163.com> Date: Wed, 3 Apr 2024 16:40:38 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9F=AD=E4=BF=A1=E9=80=9A=E7=9F=A5=E6=8D=A2?= =?UTF-8?q?=E6=88=90=E8=85=BE=E8=AE=AFsms?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sgzb/system/config/TencentSmsConfig.java | 41 ++++++ .../system/controller/SysSmsController.java | 18 +++ .../service/impl/SysSmsServiceImpl.java | 119 ++++++++++++++++-- .../src/main/resources/bootstrap.yml | 18 ++- 4 files changed, 187 insertions(+), 9 deletions(-) create mode 100644 sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/config/TencentSmsConfig.java diff --git a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/config/TencentSmsConfig.java b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/config/TencentSmsConfig.java new file mode 100644 index 00000000..6c3f6b90 --- /dev/null +++ b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/config/TencentSmsConfig.java @@ -0,0 +1,41 @@ +package com.bonus.sgzb.system.config; + +import lombok.Data; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +import java.util.List; + +/** + * + */ +@Data +@Configuration +@ConfigurationProperties(prefix = "tencent.sms") +public class TencentSmsConfig{ + + //api秘钥标识 + private String accessKeyId; + + //api秘钥 + private String accessKeySecret; + + //请求域名 + private String endpoint; + + //所属区域 + private String region; + + //腾讯云申请应用id + private String sdkAppId; + + //签名 + private String smsSign; + + //云平台模板id + private List templateId; + + + +} diff --git a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/controller/SysSmsController.java b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/controller/SysSmsController.java index 30c89738..fbf4bb9e 100644 --- a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/controller/SysSmsController.java +++ b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/controller/SysSmsController.java @@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; +import java.util.Date; /** * Description: 短信控制器 @@ -26,6 +27,11 @@ public class SysSmsController extends BaseController { @Resource private ISysSmsService smsService; + /** + * 登录短信验证码 + * @param phone + * @return + */ @PostMapping("codeLogin") public AjaxResult codeLogin(@RequestParam(value = "phone") String phone){ try { @@ -35,6 +41,12 @@ public class SysSmsController extends BaseController { } } + /** + * 短信验证码校验 + * @param phone + * @param code + * @return + */ @PostMapping("checkCode") public AjaxResult codeLogin(@RequestParam(value = "phone") String phone, @RequestParam(value = "code") String code){ try { @@ -44,6 +56,12 @@ public class SysSmsController extends BaseController { } } + /** + * 验收通知短信 + * @param phone + * @param msg + * @return + */ @PostMapping("send") public AjaxResult send(@RequestParam(value = "phone") String phone, @RequestParam(value = "msg",required = false) String msg){ try { diff --git a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysSmsServiceImpl.java b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysSmsServiceImpl.java index dde5db05..5d44e70e 100644 --- a/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysSmsServiceImpl.java +++ b/sgzb-modules/sgzb-system/src/main/java/com/bonus/sgzb/system/service/impl/SysSmsServiceImpl.java @@ -1,16 +1,29 @@ package com.bonus.sgzb.system.service.impl; -import cn.hutool.http.HttpRequest; import com.alibaba.druid.util.StringUtils; +import com.alibaba.fastjson.JSONObject; import com.bonus.sgzb.common.core.constant.UserConstants; import com.bonus.sgzb.common.core.exception.ServiceException; import com.bonus.sgzb.common.core.utils.GlobalConstants; import com.bonus.sgzb.common.core.web.domain.AjaxResult; import com.bonus.sgzb.common.redis.service.RedisService; +import com.bonus.sgzb.system.config.TencentSmsConfig; import com.bonus.sgzb.system.service.ISysSmsService; +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 com.tencentcloudapi.sms.v20210111.models.SendStatus; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import java.util.Arrays; +import java.util.List; import java.util.Random; import java.util.concurrent.TimeUnit; @@ -24,17 +37,27 @@ import static com.bonus.sgzb.common.core.web.domain.AjaxResult.success; * @Version 1.0 */ @Service +@Slf4j public class SysSmsServiceImpl implements ISysSmsService { @Resource private RedisService redisService; + @Resource + private TencentSmsConfig tencentSmsConfig; + /** * 短信URL */ private static final String URL = "http://api.ktsms.cn/sms_token?ddtkey=bonus&secretkey=KtyBns@Admin2023!"; + /** + * 验收通知短信 + * @param phone 手机号 + * @param msg 内容 + * @return + */ @Override public AjaxResult sendSms(String phone, String msg) { if (phone == null || StringUtils.isEmpty(msg)) { @@ -43,13 +66,34 @@ public class SysSmsServiceImpl implements ISysSmsService { if (phone.length() != UserConstants.PHONE_DEFAULT_LENGTH_LOGIN) { return AjaxResult.error("手机号格式不正确"); } - return sendMsgByPhone(phone, msg); + try { + String[] args = msg.split(","); + String body = sendMessageNew(phone,tencentSmsConfig.getTemplateId().get(0),args); + return success("发送手机号码:" + phone + ",内容:" + msg + ",返回结果:" + body); + } catch (Exception e) { + return AjaxResult.error("发送失败:" + e.getMessage()); + } } + /** + * 登录短信验证码 + * @param phone 手机号 + * @return + */ @Override public AjaxResult codeLogin(String phone) { - // 校验手机号码 - return sendCodeByPhone(phone, null); + + try { + //获取六位验证码 + String code = getSixBitCode(); + //调用发送短信的方法 + String body = sendMessageNew(phone,tencentSmsConfig.getTemplateId().get(1),code); + // 存储验证码至Redis中,键值为:code_15588886157 , 有效期5,时间颗粒度为MINUTES:分钟 + redisService.setCacheObject("code_" + phone, code, 5L, TimeUnit.MINUTES); + return success("手机号:" + phone + ",用户登录验证码:" + code + ",返回结果:" + body); + } catch (Exception e) { + return AjaxResult.error("发送失败:" + e.getMessage()); + } } @@ -58,7 +102,7 @@ public class SysSmsServiceImpl implements ISysSmsService { * @param phone 手机号码 * @return AjaxResult对象 */ - private AjaxResult sendMsgByPhone(String phone, String msg) { +/* private AjaxResult sendMsgByPhone(String phone, String msg) { // 校验手机号码 if (phone == null || phone.length() != UserConstants.PHONE_DEFAULT_LENGTH_LOGIN) { return AjaxResult.error("手机号码不正确"); @@ -76,7 +120,7 @@ public class SysSmsServiceImpl implements ISysSmsService { } catch (Exception e) { return AjaxResult.error("发送失败:" + e.getMessage()); } - } + }*/ /** @@ -84,7 +128,7 @@ public class SysSmsServiceImpl implements ISysSmsService { * @param phone 手机号码 * @return AjaxResult对象 */ - private AjaxResult sendCodeByPhone(String phone, String msg) { +/* private AjaxResult sendCodeByPhone(String phone, String msg) { // 校验手机号码 if (phone == null || phone.length() != UserConstants.PHONE_DEFAULT_LENGTH_LOGIN) { return AjaxResult.error("手机号格式错误,请输入11位数字号码"); @@ -108,7 +152,7 @@ public class SysSmsServiceImpl implements ISysSmsService { } catch (Exception e) { return AjaxResult.error("发送失败:" + e.getMessage()); } - } + }*/ /** @@ -146,6 +190,65 @@ public class SysSmsServiceImpl implements ISysSmsService { return String.valueOf(random.nextInt(900000) + 100000); } + /** + * 腾讯sms短信 + * @param mobilePhone + * @param templateId + * @param args + * @return + * @throws Exception + */ + public String sendMessageNew(String mobilePhone,String templateId, String... args) throws Exception { + try{ + // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密 + Credential cred = new Credential(tencentSmsConfig.getAccessKeyId(), tencentSmsConfig.getAccessKeySecret()); + // 实例化一个http选项,可选的,没有特殊需求可以跳过 + HttpProfile httpProfile = new HttpProfile(); + httpProfile.setEndpoint(tencentSmsConfig.getEndpoint()); + // 实例化一个client选项,可选的,没有特殊需求可以跳过 + ClientProfile clientProfile = new ClientProfile(); + clientProfile.setHttpProfile(httpProfile); + // 实例化要请求产品的client对象,clientProfile是可选的 第二个参数是地域信息 + SmsClient client = new SmsClient(cred, tencentSmsConfig.getRegion(), clientProfile); + // 实例化一个请求对象,每个接口都会对应一个request对象 + SendSmsRequest req = new SendSmsRequest(); + //设置固定的参数 + req.setSmsSdkAppId(tencentSmsConfig.getSdkAppId());// 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId + req.setSignName(tencentSmsConfig.getSmsSign());//短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 + req.setTemplateId(templateId);//模板 ID: 必须填写已审核通过的模板 ID + //设置发送相关的参数 + //发送对象(最多200个) + String[] phoneNumberSet1 = mobilePhone.split(","); + for (int i = 0; i < phoneNumberSet1.length; i++) { + phoneNumberSet1[i] = "+86" + phoneNumberSet1[i]; + } + req.setPhoneNumberSet(phoneNumberSet1);//发送的手机号 + if(null != args && args.length > 0 && Arrays.stream(args) + .noneMatch(s -> s == null || s.trim().isEmpty())) { + String[] templateParamSet1 = args;//模板的参数 + req.setTemplateParamSet(templateParamSet1);//发送验证码 + } + // 返回的resp是一个SendSmsResponse的实例,与请求对象对应 + log.info("腾讯云平台短信发送请求参数:{}", JSONObject.toJSONString(req)); + SendSmsResponse resp = client.SendSms(req); + // 输出json格式的字符串回包 + log.info("腾讯云平台短信发送响应结果:{}", JSONObject.toJSONString(resp)); + SendStatus[] sendStatusSet = resp.getSendStatusSet(); + List sendStatuses = Arrays.asList(sendStatusSet); + if (CollectionUtils.isNotEmpty(sendStatuses)){ + for (SendStatus sendStatus : sendStatuses) { + if (!"OK".equalsIgnoreCase(sendStatus.getCode())){ + throw new ServiceException(sendStatus.getMessage(),Integer.valueOf(sendStatus.getCode())); + } + } + } + return resp.getRequestId(); + } catch (TencentCloudSDKException e) { + e.printStackTrace(); + log.error("短信发送失败:{}", e.getMessage()); + throw new ServiceException(e.getMessage(),Integer.valueOf(e.getErrorCode())); + } + } } diff --git a/sgzb-modules/sgzb-system/src/main/resources/bootstrap.yml b/sgzb-modules/sgzb-system/src/main/resources/bootstrap.yml index b158a4e4..d67b9907 100644 --- a/sgzb-modules/sgzb-system/src/main/resources/bootstrap.yml +++ b/sgzb-modules/sgzb-system/src/main/resources/bootstrap.yml @@ -36,7 +36,23 @@ spring: # 共享配置 shared-configs: - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} - +#腾讯云sms +tencent: + sms: + #api秘钥标识 + accessKeyId: AKIDrreCVaRKDtMcgfU5QW9iEfv67tMfldJn + #api秘钥 + accessKeySecret: OXUgeMo0yhBRTGo6sVu3yiFX4rQtAzc3 + #请求域名 + endpoint: sms.tencentcloudapi.com + #所属区域 + region: ap-guangzhou + #腾讯云申请应用id + sdkAppId: 1400494336 + #签名 + smsSign: 南方电网互联网 + #云平台模板id 2116937-验收通知 2115503-登录验证 + templateId: 2116937,2115503