From 0ab9dfd8f122195e4e9f09bd50c59e0a47450bec Mon Sep 17 00:00:00 2001 From: mitao <2763622819@qq.com> Date: 星期三, 19 三月 2025 15:50:03 +0800 Subject: [PATCH] fix: resolve merge conflicts in .gitignore --- ruoyi-common/src/main/java/com/ruoyi/common/utils/SmsUtil.java | 34 ++++++++++++++++++++++++++++++---- 1 files changed, 30 insertions(+), 4 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/SmsUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/SmsUtil.java index 7129612..c558969 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/SmsUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/SmsUtil.java @@ -1,14 +1,19 @@ package com.ruoyi.common.utils; +import com.alibaba.fastjson2.JSON; import com.ruoyi.common.config.SmsProperties; import com.ruoyi.common.exception.ServiceException; +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.v20190711.SmsClient; import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest; import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; +import javax.annotation.PostConstruct; import javax.annotation.Resource; import java.util.Arrays; import java.util.List; @@ -19,15 +24,35 @@ @Resource SmsProperties smsProperties; - @Resource - SmsClient smsClient; + static SmsClient smsClient; - public SmsProperties getPro(){ + public SmsProperties getPro() { return smsProperties; } + @PostConstruct + public void createSmsClient() { + // 实例化一个认证对象,入参需要传入腾讯云账户 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(smsProperties.getSecretid(), smsProperties.getSecretkey()); + // 实例化一个http选项,可选的,没有特殊需求可以跳过 + HttpProfile httpProfile = new HttpProfile(); + // 指定接入地域域名,默认就近地域接入域名为 sms.tencentcloudapi.com ,也支持指定地域域名访问,例如广州地域的域名为 sms.ap-guangzhou.tencentcloudapi.com + httpProfile.setEndpoint("sms.tencentcloudapi.com"); + // 实例化一个客户端配置对象 + ClientProfile clientProfile = new ClientProfile(); + 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 = new SmsClient(cred, "ap-guangzhou", clientProfile); + } + public boolean sendSms(String phone,String templateId,String[] param){ + phone = phone.startsWith("+86")?phone:("+86"+phone); SendSmsRequest req = new SendSmsRequest(); req.setSmsSdkAppid(smsProperties.getAppId()); req.setPhoneNumberSet(new String[]{phone}); @@ -38,7 +63,8 @@ req.setSessionContext(""); req.setExtendCode(""); try { - smsClient.SendSms(req); + SendSmsResponse sendSmsResponse = smsClient.SendSms(req); + System.out.println(JSON.toJSONString(sendSmsResponse)); return true; } catch (TencentCloudSDKException e) { log.error("发送短信失败,{},{}",phone,param,e); -- Gitblit v1.7.1