fangzhen 11 годин тому
батько
коміт
5c1ea961da

+ 5 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java

@@ -11,6 +11,7 @@ import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.sign.Base64;
 import com.ruoyi.common.utils.uuid.IdUtils;
 import com.ruoyi.generator.util.SendSms;
+import com.ruoyi.generator.util.SendSmsTencent;
 import com.ruoyi.system.service.ISysConfigService;
 import org.apache.logging.log4j.util.Strings;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -46,6 +47,9 @@ public class CaptchaController {
     @Autowired
     private ISysConfigService configService;
 
+    @Autowired
+    private SendSmsTencent sendSmsTencent;
+
     /**
      * 生成验证码
      */
@@ -129,6 +133,7 @@ public class CaptchaController {
 
         //2.发送验证码
         SendSms.sendMsg("次元时代", "SMS_491395317", phoneNumber, String.valueOf(code));
+        //sendSmsTencent.sendMsg("次元时代", "SMS_491395317", phoneNumber, String.valueOf(code));
         //3.存入redis
 
         redisCache.setCacheObject(verifyKey, code, 120, TimeUnit.MINUTES);

+ 5 - 2
ruoyi-admin/src/main/resources/i18n/messages.properties

@@ -14,12 +14,15 @@ login.blocked=很遗憾,访问IP已被列入系统黑名单
 user.logout.success=退出成功
 project.error=程序异常
 
+#短信异常
+Sms.send.error=短信发送失败
+
 
 length.not.valid=长度必须在{min}到{max}个字符之间
 
 user.username.not.valid=* 2到20个汉字、字母、数字或下划线组成,且必须以非数字开头
 user.password.not.valid=* 5-50个字符
- 
+
 user.email.not.valid=邮箱格式错误
 user.mobile.phone.number.not.valid=手机号格式错误
 user.login.success=登录成功
@@ -51,4 +54,4 @@ upload.oos.bucketName=cysd
 ##文章敏感词
 article.title.error=文章标题包含敏感文字,请检查!
 article.content.error=文章内容包含敏感文字,请检查!
-article.user.comment=评论的内容包含敏感词汇哟!
+article.user.comment=评论的内容包含敏感词汇哟!

+ 18 - 0
ruoyi-common/src/main/java/com/ruoyi/common/exception/sms/SmsCodeException.java

@@ -0,0 +1,18 @@
+package com.ruoyi.common.exception.sms;
+
+import com.ruoyi.common.exception.user.UserException;
+
+/**
+ * 用户密码不正确或不符合规范异常类
+ *
+ * @author ruoyi
+ */
+public class SmsCodeException extends UserException
+{
+    private static final long serialVersionUID = 1L;
+
+    public SmsCodeException()
+    {
+        super("Sms.send.error", null);
+    }
+}

+ 18 - 0
ruoyi-common/src/main/java/com/ruoyi/common/exception/sms/SmsException.java

@@ -0,0 +1,18 @@
+package com.ruoyi.common.exception.sms;
+
+import com.ruoyi.common.exception.base.BaseException;
+
+/**
+ * 用户信息异常类
+ *
+ * @author ruoyi
+ */
+public class SmsException extends BaseException
+{
+    private static final long serialVersionUID = 1L;
+
+    public SmsException(String code, Object[] args)
+    {
+        super("sms", code, args, null);
+    }
+}

+ 7 - 0
ruoyi-generator/pom.xml

@@ -72,6 +72,13 @@
             <version>0.12.0</version>
         </dependency>
 
+        <!-- https://mvnrepository.com/artifact/com.tencentcloudapi/tencentcloud-sdk-java-sms -->
+        <dependency>
+            <groupId>com.tencentcloudapi</groupId>
+            <artifactId>tencentcloud-sdk-java-sms</artifactId>
+            <version>3.1.1281</version>
+        </dependency>
+
     </dependencies>
 
 </project>

+ 29 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/config/SmsTencentConfig.java

@@ -0,0 +1,29 @@
+package com.ruoyi.generator.config;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+/**
+ * 读取项目相关配置
+ *
+ * @author ruoyi
+ */
+@Component
+@ConfigurationProperties(prefix = "tencent")
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class SmsTencentConfig {
+
+    /** AppId */
+    private String appId;
+    /** secretKey */
+    private String templateId;
+    /** secretId */
+    private String secretId;
+    /** secretKey */
+    private String secretKey;
+}

+ 137 - 0
ruoyi-generator/src/main/java/com/ruoyi/generator/util/SendSmsTencent.java

@@ -0,0 +1,137 @@
+package com.ruoyi.generator.util;
+
+
+import com.ruoyi.common.exception.sms.SmsCodeException;
+import com.ruoyi.generator.config.SmsTencentConfig;
+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 org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.util.concurrent.ExecutionException;
+
+@Component
+public class SendSmsTencent {
+
+    @Resource
+    private SmsTencentConfig smsTencentConfig;
+
+    /**
+     * 阿里云短信
+     *
+     * @param signName     签名
+     * @param templateCode 模板
+     * @param phoneNumbers 手机号
+     * @param code         验证码
+     * @throws ExecutionException   异常
+     * @throws InterruptedException 异常
+     */
+    public void sendMsg(String signName, String templateCode, String phoneNumbers, String code) {
+        try {
+            // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId,SecretKey。
+            // 为了保护密钥安全,建议将密钥设置在环境变量中或者配置文件中,请参考凭证管理 https://github.com/TencentCloud/tencentcloud-sdk-java?tab=readme-ov-file#%E5%87%AD%E8%AF%81%E7%AE%A1%E7%90%86。
+            // 硬编码密钥到代码中有可能随代码泄露而暴露,有安全隐患,并不推荐。
+            // SecretId、SecretKey 查询: https://console.cloud.tencent.com/cam/capi
+            // Credential cred = new Credential("SecretId", "SecretKey");
+            Credential cred = new Credential(smsTencentConfig.getSecretId(), smsTencentConfig.getSecretKey());
+
+            // 实例化一个http选项,可选的,没有特殊需求可以跳过
+            HttpProfile httpProfile = new HttpProfile();
+            // 从3.0.96版本开始, 单独设置 HTTP 代理(无需要直接忽略)
+            // httpProfile.setProxyHost("真实代理ip");
+            // httpProfile.setProxyPort(真实代理端口);
+            httpProfile.setReqMethod("GET"); // get请求(默认为post请求)
+            httpProfile.setConnTimeout(10); // 请求连接超时时间,单位为秒(默认60秒)
+            httpProfile.setWriteTimeout(10);  // 设置写入超时时间,单位为秒(默认0秒)
+            httpProfile.setReadTimeout(10);  // 设置读取超时时间,单位为秒(默认0秒)
+
+            /* 指定接入地域域名,默认就近地域接入域名为 sms.tencentcloudapi.com ,也支持指定地域域名访问,例如广州地域的域名为 sms.ap-guangzhou.tencentcloudapi.com */
+            httpProfile.setEndpoint("sms.tencentcloudapi.com");
+
+            /* 非必要步骤:
+             * 实例化一个客户端配置对象,可以指定超时时间等配置 */
+            ClientProfile clientProfile = new ClientProfile();
+            /* SDK默认用TC3-HMAC-SHA256进行签名
+             * 非必要请不要修改这个字段 */
+            clientProfile.setSignMethod("HmacSHA256");
+            clientProfile.setHttpProfile(httpProfile);
+            /* 实例化要请求产品(以sms为例)的client对象
+             * 第二个参数是地域信息,可以直接填写字符串ap-guangzhou,支持的地域列表参考 https://cloud.tencent.com/document/api/382/52071#.E5.9C.B0.E5.9F.9F.E5.88.97.E8.A1.A8 */
+            SmsClient client = new SmsClient(cred, "ap-nanjing", clientProfile);
+            /* 实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数
+             * 您可以直接查询SDK源码确定接口有哪些属性可以设置
+             * 属性可能是基本类型,也可能引用了另一个数据结构
+             * 推荐使用IDE进行开发,可以方便的跳转查阅各个接口和数据结构的文档说明 */
+            SendSmsRequest req = new SendSmsRequest();
+
+            /* 填充请求参数,这里request对象的成员变量即对应接口的入参
+             * 您可以通过官网接口文档或跳转到request对象的定义处查看请求参数的定义
+             * 基本类型的设置:
+             * 帮助链接:
+             * 短信控制台: https://console.cloud.tencent.com/smsv2
+             * 腾讯云短信小助手: https://cloud.tencent.com/document/product/382/3773#.E6.8A.80.E6.9C.AF.E4.BA.A4.E6.B5.81 */
+
+            /* 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId,示例如1400006666 */
+            // 应用 ID 可前往 [短信控制台](https://console.cloud.tencent.com/smsv2/app-manage) 查看
+            String sdkAppId = smsTencentConfig.getAppId();
+            req.setSmsSdkAppId(sdkAppId);
+
+            /* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名 */
+            // 签名信息可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-sign) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-sign) 的签名管理查看
+            req.setSignName(signName);
+
+            /* 模板 ID: 必须填写已审核通过的模板 ID */
+            // 模板 ID 可前往 [国内短信](https://console.cloud.tencent.com/smsv2/csms-template) 或 [国际/港澳台短信](https://console.cloud.tencent.com/smsv2/isms-template) 的正文模板管理查看
+            String templateId = smsTencentConfig.getTemplateId();
+            req.setTemplateId(templateId);
+
+            /* 模板参数: 模板参数的个数需要与 TemplateId 对应模板的变量个数保持一致,若无模板参数,则设置为空 */
+            String[] templateParamSet = {code};
+            req.setTemplateParamSet(templateParamSet);
+
+            /* 下发手机号码,采用 E.164 标准,+[国家或地区码][手机号]
+             * 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号 */
+            String[] phoneNumberSet = {"+86" + phoneNumbers};
+            req.setPhoneNumberSet(phoneNumberSet);
+
+            /* 用户的 session 内容(无需要可忽略): 可以携带用户侧 ID 等上下文信息,server 会原样返回 */
+            String sessionContext = "";
+            req.setSessionContext(sessionContext);
+
+            /* 短信码号扩展号(无需要可忽略): 默认未开通,如需开通请联系 [腾讯云短信小助手] */
+            String extendCode = "";
+            req.setExtendCode(extendCode);
+
+            /* 国内短信无需填写该项;国际/港澳台短信已申请独立 SenderId 需要填写该字段,默认使用公共 SenderId,无需填写该字段。注:月度使用量达到指定量级可申请独立 SenderId 使用,详情请联系 [腾讯云短信小助手](https://cloud.tencent.com/document/product/382/3773#.E6.8A.80.E6.9C.AF.E4.BA.A4.E6.B5.81)。*/
+            String senderid = "";
+            req.setSenderId(senderid);
+
+            /* 通过 client 对象调用 SendSms 方法发起请求。注意请求方法名与请求对象是对应的
+             * 返回的 res 是一个 SendSmsResponse 类的实例,与请求对象对应 */
+            SendSmsResponse res = client.SendSms(req);
+
+            // 输出json格式的字符串回包
+            System.out.println(SendSmsResponse.toJsonString(res));
+
+            // 也可以取出单个值,您可以通过官网接口文档或跳转到response对象的定义处查看返回字段的定义
+            // System.out.println(res.getRequestId());
+
+            /* 当出现以下错误码时,快速解决方案参考
+             * [FailedOperation.SignatureIncorrectOrUnapproved](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Afailedoperation.signatureincorrectorunapproved-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
+             * [FailedOperation.TemplateIncorrectOrUnapproved](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Afailedoperation.templateincorrectorunapproved-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
+             * [UnauthorizedOperation.SmsSdkAppIdVerifyFail](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Aunauthorizedoperation.smssdkappidverifyfail-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
+             * [UnsupportedOperation.ContainDomesticAndInternationalPhoneNumber](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Aunsupportedoperation.containdomesticandinternationalphonenumber-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)
+             * 更多错误,可咨询[腾讯云助手](https://tccc.qcloud.com/web/im/index.html#/chat?webAppId=8fa15978f85cb41f7e2ea36920cb3ae1&title=Sms)
+             */
+
+        } catch (TencentCloudSDKException e) {
+            e.printStackTrace();
+            throw new SmsCodeException();
+        }
+    }
+}