xuhy
2025-02-28 1d7bf167e67ab084e088080e3a2e8e87a70446bf
ruoyi-common/src/main/java/com/ruoyi/common/utils/SmsUtil.java
@@ -13,6 +13,7 @@
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;
@@ -30,14 +31,15 @@
        return smsProperties;
    }
    public SmsClient createSmsClient(SmsProperties properties) {
    @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(properties.getSecretid(), properties.getSecretkey());
        Credential cred = new Credential(smsProperties.getSecretid(), smsProperties.getSecretkey());
        // 实例化一个http选项,可选的,没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        // 指定接入地域域名,默认就近地域接入域名为 sms.tencentcloudapi.com ,也支持指定地域域名访问,例如广州地域的域名为 sms.ap-guangzhou.tencentcloudapi.com
@@ -46,7 +48,7 @@
        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
        return new SmsClient(cred, "ap-guangzhou", clientProfile);
        smsClient =  new SmsClient(cred, "ap-guangzhou", clientProfile);
    }
    public boolean sendSms(String phone,String templateId,String[] param){
@@ -73,17 +75,5 @@
        }
    }
    public static void main(String[] args) {
        SmsProperties properties = new SmsProperties();
        properties.setAppId("1400957506");
        properties.setSign("畅云出行");
        properties.setSecretid("AKIDCF5EF2c0DE1e5JK8r4EGJF4mNsMgp26x");
        properties.setSecretkey("lLl184rUyFOOE0d5KNGC3kmfNsCWk4GU");
        SmsUtil smsUtil = new SmsUtil();
        smsUtil.smsProperties = properties;
        smsUtil.smsClient = smsUtil.createSmsClient(properties);
        boolean isok = smsUtil.sendSms("15826040006", "2369926", new String[]{"526584"});
        System.out.println(isok);
    }
}