package com.ruoyi.errand.utils.sms;
|
|
import com.aliyuncs.CommonRequest;
|
import com.aliyuncs.CommonResponse;
|
import com.aliyuncs.DefaultAcsClient;
|
import com.aliyuncs.IAcsClient;
|
import com.aliyuncs.exceptions.ClientException;
|
import com.aliyuncs.exceptions.ServerException;
|
import com.aliyuncs.http.MethodType;
|
import com.aliyuncs.profile.DefaultProfile;
|
import org.springframework.stereotype.Service;
|
|
|
public class AliyunSmsService {
|
|
// 阿里云短信服务配置
|
private static final String ACCESS_KEY_ID = "LTAI5tRtwxPMw8aohbLLcjjo";
|
private static final String ACCESS_KEY_SECRET = "NSKeley8OjCsl7AQJnDKxPIh1dIaOu";
|
private static final String SIGN_NAME = "玮儿科技";
|
private static final String TEMPLATE_CODE = "SMS_319401491";
|
// 新增跑腿员接单提醒模板 CODE
|
private static final String TEMPLATE_CODE_ORDER = "SMS_489100144";
|
/**
|
* 发送短信验证码
|
* @param phoneNumber 手机号码
|
* @param code 验证码
|
* @return 是否发送成功
|
*/
|
public static boolean sendVerificationCode(String phoneNumber, String code) {
|
// 初始化客户端
|
DefaultProfile profile = DefaultProfile.getProfile(
|
"cn-chengdu",
|
ACCESS_KEY_ID,
|
ACCESS_KEY_SECRET
|
);
|
IAcsClient client = new DefaultAcsClient(profile);
|
|
// 构建请求
|
CommonRequest request = new CommonRequest();
|
request.setSysMethod(MethodType.POST);
|
request.setSysDomain("dysmsapi.aliyuncs.com");
|
request.setSysVersion("2017-05-25");
|
request.setSysAction("SendSms");
|
|
// 设置请求参数
|
request.putQueryParameter("RegionId", "cn-hangzhou");
|
request.putQueryParameter("PhoneNumbers", phoneNumber);
|
request.putQueryParameter("SignName", SIGN_NAME);
|
request.putQueryParameter("TemplateCode", TEMPLATE_CODE);
|
|
// 模板参数JSON,根据实际模板调整
|
String templateParam = String.format("{\"code\":\"%s\"}", code);
|
request.putQueryParameter("TemplateParam", templateParam);
|
|
try {
|
// 发送请求并获取响应
|
CommonResponse response = client.getCommonResponse(request);
|
System.out.println(response.getData());
|
|
// 解析响应结果
|
return response.getHttpStatus() == 200 &&
|
response.getData().contains("\"Code\":\"OK\"");
|
} catch (ServerException e) {
|
e.printStackTrace();
|
} catch (ClientException e) {
|
e.printStackTrace();
|
}
|
|
return false;
|
}
|
|
/**
|
* 生成随机验证码
|
* @param length 验证码长度
|
* @return 随机验证码
|
*/
|
public static String generateVerificationCode(int length) {
|
StringBuilder sb = new StringBuilder();
|
for (int i = 0; i < length; i++) {
|
sb.append((int)(Math.random() * 10));
|
}
|
return sb.toString();
|
}
|
/**
|
* 发送跑腿员接单提醒短信
|
* @param phoneNumber 跑腿员手机号码
|
* @return 是否发送成功
|
*/
|
public static boolean sendOrderRemindSms(String phoneNumber) {
|
// 初始化客户端(复用已有的配置)
|
DefaultProfile profile = DefaultProfile.getProfile(
|
"cn-chengdu",
|
ACCESS_KEY_ID,
|
ACCESS_KEY_SECRET
|
);
|
IAcsClient client = new DefaultAcsClient(profile);
|
|
// 构建请求
|
CommonRequest request = new CommonRequest();
|
request.setSysMethod(MethodType.POST);
|
request.setSysDomain("dysmsapi.aliyuncs.com");
|
request.setSysVersion("2017-05-25");
|
request.setSysAction("SendSms");
|
|
// 设置请求参数
|
request.putQueryParameter("RegionId", "cn-hangzhou");
|
request.putQueryParameter("PhoneNumbers", phoneNumber);
|
request.putQueryParameter("SignName", SIGN_NAME);
|
request.putQueryParameter("TemplateCode", TEMPLATE_CODE_ORDER);
|
|
// 该模板无自定义变量,传空 JSON(根据模板实际情况,若有变量需按格式填充)
|
request.putQueryParameter("TemplateParam", "{}");
|
|
try {
|
// 发送请求并获取响应
|
CommonResponse response = client.getCommonResponse(request);
|
System.out.println(response.getData());
|
|
// 解析响应结果
|
return response.getHttpStatus() == 200 &&
|
response.getData().contains("\"Code\":\"OK\"");
|
} catch (ServerException e) {
|
e.printStackTrace();
|
} catch (ClientException e) {
|
e.printStackTrace();
|
}
|
|
return false;
|
}
|
|
|
public static void main(String[] args) {
|
/* String phoneNumber = "19923261698";
|
boolean result = sendVerificationCode(phoneNumber,"123123");
|
System.out.println("短信发送结果:" + result);*/
|
// 测试发送接单提醒
|
String phoneNumberOrder = "19923261698"; // 替换为实际跑腿员手机号
|
boolean resultOrder = sendOrderRemindSms(phoneNumberOrder);
|
System.out.println("接单提醒短信发送结果:" + resultOrder);
|
|
}
|
}
|