2023-08-12 18:20:35 +08:00
|
|
|
|
package com.jwl.driver.server.util;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.aliyuncs.DefaultAcsClient;
|
|
|
|
|
import com.aliyuncs.IAcsClient;
|
|
|
|
|
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
|
|
|
|
|
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
|
|
|
|
|
import com.aliyuncs.exceptions.ClientException;
|
|
|
|
|
import com.aliyuncs.profile.DefaultProfile;
|
|
|
|
|
import com.aliyuncs.profile.IClientProfile;
|
2023-08-13 00:19:00 +08:00
|
|
|
|
import com.jwl.driver.server.constant.Constants;
|
|
|
|
|
import com.jwl.driver.server.exception.BusinessException;
|
|
|
|
|
import jdk.vm.ci.meta.Constant;
|
2023-08-12 18:20:35 +08:00
|
|
|
|
import lombok.Data;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @author zcx
|
|
|
|
|
* 短信工具
|
|
|
|
|
*/
|
|
|
|
|
@Data
|
|
|
|
|
public class SmsUtil {
|
|
|
|
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(SmsUtil.class);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 基础信息
|
|
|
|
|
*/
|
|
|
|
|
private static String product = "Dysmsapi";
|
|
|
|
|
private static String url = "dysmsapi.aliyuncs.com";
|
|
|
|
|
private static String appkey = "LTAI4GH1CqJtaUVpmD1oYEUZ";
|
|
|
|
|
private static String secret = "DFG848ea8U0QaQcdiHAYznUrCWfJzn";
|
|
|
|
|
private static String smsFreeSignName = "对嘛科技";
|
|
|
|
|
|
|
|
|
|
//用于存储短信验证码
|
|
|
|
|
private static Map<String, String> codeMap = new HashMap<>();
|
2023-08-13 00:19:00 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送验证码
|
|
|
|
|
* @param sendPhone
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static String sendMessage(String sendPhone) {
|
|
|
|
|
String code = getMessageCode(sendPhone);
|
|
|
|
|
Map<String, Object> param = new HashMap<>();
|
|
|
|
|
param.put("code", code);
|
|
|
|
|
//发送短信
|
|
|
|
|
currencySendMessage(sendPhone, Constants.MESSAGE_TEMPLATE, JSONObject.toJSONString(param));
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-12 18:20:35 +08:00
|
|
|
|
/**
|
|
|
|
|
* 生成短信验证码并存储
|
|
|
|
|
*/
|
|
|
|
|
public static String getMessageCode(String phone){
|
|
|
|
|
String code = (int)(Math.random()*999999)+ "";
|
|
|
|
|
if (code.length() == 5){
|
|
|
|
|
code = "0" + code;
|
|
|
|
|
}
|
|
|
|
|
codeMap.put(phone, code);
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取6位验证码
|
|
|
|
|
*/
|
|
|
|
|
public static String getCodeByPhone(String phone){
|
|
|
|
|
return codeMap.get(phone);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 通用的发送短信的方法
|
|
|
|
|
*
|
|
|
|
|
* @param sendPhone
|
|
|
|
|
* 要发送的目标号码
|
|
|
|
|
* @param templet
|
|
|
|
|
* 要使用的模板
|
|
|
|
|
* @param jsonText
|
|
|
|
|
* 拼接对应模板的json格式的字符串
|
|
|
|
|
*/
|
|
|
|
|
public static String currencySendMessage(String sendPhone, String templet,
|
|
|
|
|
String jsonText) {
|
|
|
|
|
IAcsClient aliClient = createClient();
|
|
|
|
|
|
|
|
|
|
//组装请求对象
|
|
|
|
|
SendSmsRequest request = new SendSmsRequest();
|
|
|
|
|
//必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为20个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
|
|
|
|
|
request.setPhoneNumbers(sendPhone);
|
|
|
|
|
//必填:短信签名-可在短信控制台中找到
|
|
|
|
|
request.setSignName(smsFreeSignName);
|
|
|
|
|
//必填:短信模板-可在短信控制台中找到
|
|
|
|
|
request.setTemplateCode(templet);
|
|
|
|
|
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
|
|
|
|
|
request.setTemplateParam(jsonText);
|
|
|
|
|
SendSmsResponse sendSmsResponse = null;
|
|
|
|
|
logger.info("发送短信参数内容:"+jsonText+"\n手机号码是:"+sendPhone+"\n模板编号:"+templet);
|
|
|
|
|
try {
|
|
|
|
|
sendSmsResponse = aliClient.getAcsResponse(request);
|
|
|
|
|
} catch (ClientException e) {
|
|
|
|
|
logger.error("发送短信失败:"+e.getErrMsg()+"\n"+e.getMessage());
|
|
|
|
|
e.printStackTrace();
|
2023-08-13 00:19:00 +08:00
|
|
|
|
throw new BusinessException("获取验证码失败");
|
2023-08-12 18:20:35 +08:00
|
|
|
|
}
|
|
|
|
|
if (sendSmsResponse!=null){
|
|
|
|
|
logger.info("message:"+sendSmsResponse.getMessage()+"\n"+
|
|
|
|
|
"code:"+sendSmsResponse.getCode()+"\n");
|
|
|
|
|
}else{
|
|
|
|
|
logger.info("发送短信响应为null");
|
2023-08-13 00:19:00 +08:00
|
|
|
|
throw new BusinessException("获取验证码失败");
|
2023-08-12 18:20:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return JSONObject.toJSONString(sendSmsResponse);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建客户端
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private static IAcsClient createClient() {
|
|
|
|
|
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", appkey,
|
|
|
|
|
secret);
|
|
|
|
|
try {
|
|
|
|
|
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, url);
|
|
|
|
|
} catch (ClientException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
IAcsClient acsClient = new DefaultAcsClient(profile);
|
|
|
|
|
return acsClient;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException {
|
|
|
|
|
Map<String, Object> param = new HashMap<>();
|
2023-08-13 00:19:00 +08:00
|
|
|
|
param.put("code", getMessageCode("18255439337"));
|
|
|
|
|
SmsUtil.currencySendMessage("18255439337", "SMS_198880447", JSONObject.toJSONString(param) );
|
2023-08-12 18:20:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|