package com.ruoyi.web.utils;
|
|
import cn.hutool.core.util.RandomUtil;
|
import com.aliyun.dysmsapi20170525.Client;
|
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
|
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
import com.aliyun.tea.TeaException;
|
import com.aliyun.teaopenapi.models.Config;
|
import com.aliyun.teautil.models.RuntimeOptions;
|
import com.google.gson.Gson;
|
|
/**
|
* 短信发送工具类
|
*/
|
public class SMSUtils {
|
public static final String ALIBABA_CLOUD_ACCESS_KEY_ID = "LTAI5tBX45VYcobybXcoKX1F";
|
public static final String ALIBABA_CLOUD_ACCESS_KEY_SECRET = "jS4UwR2pB3ucKzlnDO6y0uwv2qHggT";
|
|
/**
|
* 使用AK&SK初始化Client
|
* 使用客户端去发送短信
|
*
|
* @param accessKeyId
|
* @param accessKeySecret
|
* @return
|
* @throws Exception
|
*/
|
public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
|
Config config = new Config()
|
// 必填,您的 AccessKey ID
|
.setAccessKeyId(accessKeyId)
|
// 必填,您的 AccessKey Secret
|
.setAccessKeySecret(accessKeySecret);
|
// 访问的域名 固定写法
|
config.endpoint = "dysmsapi.aliyuncs.com";
|
return new Client(config);
|
}
|
|
/**
|
* 向指定手机号发送短信
|
*
|
* @param signName 签名
|
* @param templateCode 模板
|
* @param phoneNumbers 手机号
|
* @param param 参数 ---验证码
|
*/
|
public static void sendMessage(String signName, String templateCode, String phoneNumbers, String param) throws Exception {
|
// 获取客户端对象
|
Client client = createClient(ALIBABA_CLOUD_ACCESS_KEY_ID, ALIBABA_CLOUD_ACCESS_KEY_SECRET);
|
// 获取发送短信对象 设置发送短信的 签名,模板,手机号,验证码
|
SendSmsRequest request = new SendSmsRequest();
|
request.setSignName(signName);
|
request.setTemplateCode(templateCode);
|
request.setPhoneNumbers(phoneNumbers);
|
request.setTemplateParam("{\"code\":\"" + param + "\"}");
|
// 运行时行为 例如最大尝试次数,超时时间等等
|
RuntimeOptions runtime = new RuntimeOptions();
|
try {
|
// 根据信息发送短信
|
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(request, runtime);
|
System.out.println(new Gson().toJson(sendSmsResponse.body));
|
} catch (TeaException error) {
|
// 如有需要,请打印 error
|
System.out.println(error);
|
com.aliyun.teautil.Common.assertAsString(error.message);
|
} catch (Exception _error) {
|
TeaException error = new TeaException(_error.getMessage(), _error);
|
// 如有需要,请打印 error
|
com.aliyun.teautil.Common.assertAsString(error.message);
|
}
|
}
|
|
|
/**
|
* 随机数
|
*/
|
public static String randomNum() {
|
// 获得四位的随机整数
|
return RandomUtil.randomNumbers(4);
|
}
|
|
public static void main(String[] args) throws Exception {
|
String s = randomNum();
|
System.out.println(s);
|
sendMessage("深圳本我","SMS_274905251","18258070687",s);
|
}
|
}
|